astring icon indicating copy to clipboard operation
astring copied to clipboard

Does not handle literal module specifier

Open lachrist opened this issue 5 months ago • 1 comments

Motivation

Hi, thanks for the work!

It seems astring does not handle module specifier when they are literals and not identifiers.

import { parse } from "acorn";
import { generate } from "astring";

const root = parse("export { 'foo-bar' as 'qux-baz' } from 'source';", {
  sourceType: "module",
  ecmaVersion: 2024,
});

console.dir(root, { depth: 1 / 0 });

console.log(generate(root));
Node {
  type: 'Program',
  start: 0,
  end: 48,
  body: [
    Node {
      type: 'ExportNamedDeclaration',
      start: 0,
      end: 48,
      declaration: null,
      specifiers: [
        Node {
          type: 'ExportSpecifier',
          start: 9,
          end: 31,
          local: Node {
            type: 'Literal',
            start: 9,
            end: 18,
            value: 'foo-bar',
            raw: "'foo-bar'"
          },
          exported: Node {
            type: 'Literal',
            start: 22,
            end: 31,
            value: 'qux-baz',
            raw: "'qux-baz'"
          }
        }
      ],
      source: Node {
        type: 'Literal',
        start: 39,
        end: 47,
        value: 'source',
        raw: "'source'"
      }
    }
  ],
  sourceType: 'module'
}
export {undefined} from 'source';

Expected behavior

export { 'foo-bar' as 'qux-baz' } from 'source';

Actual behavior

export {undefined} from 'source';

lachrist avatar Sep 13 '24 10:09 lachrist