Skip to content
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

Closed
tcK1 opened this issue Apr 6, 2023 · 4 comments
Closed

Keys are not enumerable on exports and imports #38

tcK1 opened this issue Apr 6, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@tcK1
Copy link

tcK1 commented Apr 6, 2023

Environment

magicast: 0.2.1
Node: v16.17.0

Reproduction

Here's a Codesandbox with an example.

This is the test snippet:

import { parseModule } from "magicast";

const mod = parseModule(`
import test from './foo'
export default defineConfig({ foo: 'bar' })
`);

console.log(mod.imports);
console.log(Object.keys(mod.imports));
console.log(mod.exports);
console.log(Object.keys(mod.exports));

Describe the bug

Both imports and exports are empty, even when the source string contains both.

Additional context

No response

Logs

No response

@xiaochengzi6
Copy link

xiaochengzi6 commented Apr 6, 2023

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)]
}

@antfu antfu added the bug Something isn't working label Apr 6, 2023
@antfu antfu changed the title Imports and Exports are empty using parseModule Keys are not enumerable on exports and imports Apr 6, 2023
@xiaochengzi6
Copy link

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?

@antfu
Copy link
Member

antfu commented Apr 7, 2023

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.

@antfu antfu closed this as completed in b7d29c0 Apr 9, 2023
@tcK1
Copy link
Author

tcK1 commented Apr 10, 2023

Thanks for the fix, @antfu! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants