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

feat: import/export of literal string names #714

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@babel/generator": "^7.14.3",
"@babel/parser": "^7.16.4",
"@babel/preset-env": "^7.14.4",
"acorn": "^8.6.0",
"acorn": "^8.12.1",
"acorn-import-attributes": "^1.9.5",
"astravel": "^0.5.0",
"ava": "^3.15.0",
Expand Down
28 changes: 18 additions & 10 deletions src/astring.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,11 @@ export const GENERATOR = {
state.write('{')
for (;;) {
const specifier = specifiers[i]
const { name } = specifier.imported
state.write(name, specifier)
if (name !== specifier.local.name) {
state.write(' as ' + specifier.local.name)
const { imported, local } = specifier
this[imported.type](imported, state)
if (local.type === 'Literal' || imported.name !== local.name) {
state.write(' as ')
this[local.type](local, state)
}
if (++i < length) {
state.write(', ')
Expand Down Expand Up @@ -626,11 +627,16 @@ export const GENERATOR = {
{ length } = specifiers
if (length > 0) {
for (let i = 0; ; ) {
const specifier = specifiers[i]
const { name } = specifier.local
state.write(name, specifier)
if (name !== specifier.exported.name) {
state.write(' as ' + specifier.exported.name)
const { local, exported } = specifiers[i]
this[local.type](local, state)
if (
local.type !== exported.type
|| (local.type === 'Literal'
? local.value !== exported.value
: local.name !== exported.name)
) {
state.write(' as ')
this[exported.type](exported, state)
}
if (++i < length) {
state.write(', ')
Expand Down Expand Up @@ -660,7 +666,9 @@ export const GENERATOR = {
},
ExportAllDeclaration(node, state) {
if (node.exported != null) {
state.write('export * as ' + node.exported.name + ' from ')
state.write('export * as ')
this[node.exported.type](node.exported, state)
state.write(' from ')
} else {
state.write('export * from ')
}
Expand Down
5 changes: 4 additions & 1 deletion src/tests/fixtures/syntax/export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "module";
export * as m from "module";
export * as module from "module";
export {name} from "module";
export {a as b, c as d} from "module";
let e, g;
Expand All @@ -10,3 +10,6 @@ export var j = 42;
export let k = 42;
export function l() {}
export {val} from '../other/a.json' with { type: "json" };
let m, n, o, p;
export {m as "m"};
export {n as "n", "o" as o, "p"} from "module";
1 change: 1 addition & 0 deletions src/tests/fixtures/syntax/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import * as q from "../other/a.json" with { type: "json" };
import r from "../other/a.json" with { type: "json" };
import("module");
const x = import("module");
import {"s" as t} from "module";