-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keys are not enumerable on exports
and imports
#38
Comments
Yes, I also found this problem when I tried to use the 'import' or 'export' attribute value because this is actually an empty object. // demo
const value = parseModule(
`export default function a () {}`
)
// value
{
'$ast': Node {
type: 'Program',
start: 0,
end: 31,
loc: SourceLocation {
start: [Object],
end: [Object],
filename: undefined,
identifierName: undefined,
lines: [Lines],
tokens: [Array],
indent: 0
},
sourceType: 'module',
interpreter: null,
body: [ [Node] ],
directives: []
},
'$code': 'export default function a () {}',
'$type': 'module',
// ---- see ----
exports: {},
imports: {},
generate: [Function (anonymous)]
} |
parseModule
exports
and imports
Looking at the project source code, I found that the 'exports' or' imports' field uses Proxy's 'get' to intercept attribute lookup by traversing the ast tree and using'set' to intercept attribute changes to modify the ast node directly. Sounds cool, but it also creates problems. const { parseModule } = require("magicast");
const mod = parseModule(`export default defineConfig({ foo: 'bar' })`);
// I can't get the contents of the object directly
console.log(mod.exports.default) // {}
console.log(mod.exports.default.$args) // {}
console.log(mod.exports.default.$args[0].foo) // bar
mod.exports.default.$args.push('b')
console.log(mod.exports.default.$args) // {}
const result = mod.generate()
console.log(result)
/**
* {
* code: "export default defineConfig({ foo: 'bar' }, 'b');",
* map: undefined
* }
*/ This is very inconvenient in use. If I solve this problem, I may abandon the way of 'Proxy'. Is that what we want? |
It's a known bug, I will find a way to fix it. I think moving away from Proxy is not an ideal solution as we want it to be dynamic reflecting to the ast. |
Thanks for the fix, @antfu! 😊 |
Environment
magicast:
0.2.1
Node:
v16.17.0
Reproduction
Here's a Codesandbox with an example.
This is the test snippet:
Describe the bug
Both
imports
andexports
are empty, even when the source string contains both.Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: