Comments (8)
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.
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.
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.
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.
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.
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.
Great, thanks for verifying, I'm happy we now know it's working well :-)
from parse.
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)
- Fast js detection HOT 1
- What is the advantages of html lexer of this package? HOT 2
- Questions about JS() method and String() method in js ast code HOT 3
- Rule case will be modified when parse css HOT 2
- CSS parser ignoring comments within rulesets HOT 1
- Is pretty-printing JS in-scope? HOT 5
- Convert back to string syntax error after js parse HOT 1
- Adding custom tokens to lexer? HOT 2
- Typescript and jsx,tsx support HOT 2
- Document usage of JS parsers HOT 1
- question about media queries HOT 2
- JS parser doesn't handle \u2028 and \u2029 like modern browsers HOT 1
- Top-level await HOT 12
- Whether there is js AST to json format with scope ? HOT 5
- CSS Nesting HOT 5
- JS parser: get the expression of a declared variable. HOT 3
- Wrong handling of JS numeric separators in some circumstances HOT 1
- v2.5.33 backwards-breaks packages depending on it, including current github.com/tdewolff/minify/v2 HOT 2
- js parser accepts a single backtick as valid js HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parse.