Giter Site home page Giter Site logo

Comments (8)

blixt avatar blixt commented on May 27, 2024

So here's what I got trying to get a list of names exported by a module, iterating its top-level statements and running this function:

func getExportNames(stmt *js.ExportStmt) (names []string, exportsAll bool) {
	if stmt == nil {
		return
	}
	if stmt.Default {
		names = append(names, "default")
		return
	}
	for _, alias := range stmt.List {
		if string(alias.Binding) == "*" {
			exportsAll = true
			continue
		}
		names = append(names, string(alias.Binding))
	}
	switch decl := stmt.Decl.(type) {
	case *js.VarDecl:
		for _, elem := range decl.List {
			switch binding := elem.Binding.(type) {
			case *js.Var:
				names = append(names, string(binding.Data))
			case *js.BindingObject:
				// We're ignoring the Rest property here because it's not possible in exports.
				for _, item := range binding.List {
					v, ok := item.Value.Binding.(*js.Var)
					if !ok {
						// Shouldn't happen in practice.
						continue
					}
					names = append(names, string(v.Name()))
				}
			}
		}
	case *js.FuncDecl:
		names = append(names, string(decl.Name.Name()))
	case *js.ClassDecl:
		names = append(names, string(decl.Name.Name()))
	}
	return
}

As you can see it gets a bit hairy with exceptions and assumptions for *js.BindingObject which is why I created this issue.

from parse.

tdewolff avatar tdewolff commented on May 27, 2024

Are you sure it is a *js.BindingObject? From what I see is that the ExportStmt contains List []Alias, representing the export {name as alias, more} type of syntax you mention. Maybe I'm missing something?

from parse.

blixt avatar blixt commented on May 27, 2024

Yup, it occurs in one of the types of export statements. I copied all of the examples from MDN into this playground with my utility function + debug output so you should be able to find it quickly:
https://play.golang.org/p/0snrm2v8GX0

Try running it and you'll see the all-caps message for one of the statements.

from parse.

blixt avatar blixt commented on May 27, 2024

Ah I realized my example in the title was wrong, this only happens for export const { a: b } = obj and not export { a as b }.

from parse.

blixt avatar blixt commented on May 27, 2024

Alright so I think I just completely misunderstood the limitations of this kind of export. As far as I can tell it does allow rest properties and now I think I misread the spec. This works fine:

const js = `const o = { a: 42, b: "hello" }; export const { a: test, ...rest } = o;`
const url = await URL.createObjectURL(new Blob([js], { type: "text/javascript" }))
const module = await import(url)
console.log(module.test, module.rest) // 42 { b: "hello" }

I think I just have to actually convert the binding into individual variables that are exported in my utility. Feel free to close this issue if you don't think there's anything to be done to help this – export syntax probably just really is this complicated. :)

from parse.

blixt avatar blixt commented on May 27, 2024

Okay I think after more testing I've come to the conclusion that the way it works now is the only way it should work. Basically, something like this should work fine and export the names c, d, f, g, h, i, k, l, m, and n.

export var { a: { b: c, ...d }, e: [f, g, { h, i, j: k, ...l }, ...m], ...n } = o;

from parse.

tdewolff avatar tdewolff commented on May 27, 2024

Great, thanks for verifying, I'm happy we now know it's working well :-)

from parse.

blixt avatar blixt commented on May 27, 2024

Here's the code that behaves as expected based on the above findings if anyone ever runs into this and wants to figure out how to get a complete list of exported names from a JS module:

https://play.golang.org/p/cJUbIuCx4ug

from parse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.