Skip to content

Commit d67daa8

Browse files
authored
support string namespace in import & export (#5570)
1 parent f0120e9 commit d67daa8

File tree

9 files changed

+295
-134
lines changed

9 files changed

+295
-134
lines changed

lib/ast.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,34 +1366,29 @@ var AST_ExportDefault = DEFNODE("ExportDefault", "body", {
13661366
},
13671367
}, AST_Statement);
13681368

1369-
var AST_ExportForeign = DEFNODE("ExportForeign", "aliases keys path quote", {
1369+
var AST_ExportForeign = DEFNODE("ExportForeign", "aliases keys path", {
13701370
$documentation: "An `export ... from '...'` statement",
13711371
$propdoc: {
1372-
aliases: "[string*] array of aliases to export",
1373-
keys: "[string*] array of keys to import",
1374-
path: "[string] the path to import module",
1375-
quote: "[string?] the original quote character",
1372+
aliases: "[AST_String*] array of aliases to export",
1373+
keys: "[AST_String*] array of keys to import",
1374+
path: "[AST_String] the path to import module",
13761375
},
13771376
_equals: function(node) {
1378-
return this.path == node.path
1379-
&& list_equals(this.aliases, node.aliases)
1380-
&& list_equals(this.keys, node.keys);
1377+
return this.path.equals(node.path)
1378+
&& all_equals(this.aliases, node.aliases)
1379+
&& all_equals(this.keys, node.keys);
13811380
},
13821381
_validate: function() {
13831382
if (this.aliases.length != this.keys.length) {
13841383
throw new Error("aliases:key length mismatch: " + this.aliases.length + " != " + this.keys.length);
13851384
}
13861385
this.aliases.forEach(function(name) {
1387-
if (typeof name != "string") throw new Error("aliases must contain string");
1386+
if (!(name instanceof AST_String)) throw new Error("aliases must contain AST_String");
13881387
});
13891388
this.keys.forEach(function(name) {
1390-
if (typeof name != "string") throw new Error("keys must contain string");
1389+
if (!(name instanceof AST_String)) throw new Error("keys must contain AST_String");
13911390
});
1392-
if (typeof this.path != "string") throw new Error("path must be string");
1393-
if (this.quote != null) {
1394-
if (typeof this.quote != "string") throw new Error("quote must be string");
1395-
if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
1396-
}
1391+
if (!(this.path instanceof AST_String)) throw new Error("path must be AST_String");
13971392
},
13981393
}, AST_Statement);
13991394

@@ -1420,17 +1415,16 @@ var AST_ExportReferences = DEFNODE("ExportReferences", "properties", {
14201415
},
14211416
}, AST_Statement);
14221417

1423-
var AST_Import = DEFNODE("Import", "all default path properties quote", {
1418+
var AST_Import = DEFNODE("Import", "all default path properties", {
14241419
$documentation: "An `import` statement",
14251420
$propdoc: {
14261421
all: "[AST_SymbolImport?] the imported namespace, or null if not specified",
14271422
default: "[AST_SymbolImport?] the alias for default `export`, or null if not specified",
1428-
path: "[string] the path to import module",
1423+
path: "[AST_String] the path to import module",
14291424
properties: "[(AST_SymbolImport*)?] array of aliases, or null if not specified",
1430-
quote: "[string?] the original quote character",
14311425
},
14321426
_equals: function(node) {
1433-
return this.path == node.path
1427+
return this.path.equals(node.path)
14341428
&& prop_equals(this.all, node.all)
14351429
&& prop_equals(this.default, node.default)
14361430
&& !this.properties == !node.properties
@@ -1453,16 +1447,12 @@ var AST_Import = DEFNODE("Import", "all default path properties quote", {
14531447
}
14541448
if (this.default != null) {
14551449
if (!(this.default instanceof AST_SymbolImport)) throw new Error("default must be AST_SymbolImport");
1456-
if (this.default.key !== "") throw new Error("invalid default key: " + this.default.key);
1450+
if (this.default.key.value !== "") throw new Error("invalid default key: " + this.default.key.value);
14571451
}
1458-
if (typeof this.path != "string") throw new Error("path must be string");
1452+
if (!(this.path instanceof AST_String)) throw new Error("path must be AST_String");
14591453
if (this.properties != null) this.properties.forEach(function(node) {
14601454
if (!(node instanceof AST_SymbolImport)) throw new Error("properties must contain AST_SymbolImport");
14611455
});
1462-
if (this.quote != null) {
1463-
if (typeof this.quote != "string") throw new Error("quote must be string");
1464-
if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
1465-
}
14661456
},
14671457
}, AST_Statement);
14681458

@@ -2005,14 +1995,14 @@ var AST_SymbolConst = DEFNODE("SymbolConst", null, {
20051995
var AST_SymbolImport = DEFNODE("SymbolImport", "key", {
20061996
$documentation: "Symbol defined by an `import` statement",
20071997
$propdoc: {
2008-
key: "[string] the original `export` name",
1998+
key: "[AST_String] the original `export` name",
20091999
},
20102000
_equals: function(node) {
20112001
return this.name == node.name
2012-
&& this.key == node.key;
2002+
&& this.key.equals(node.key);
20132003
},
20142004
_validate: function() {
2015-
if (typeof this.key != "string") throw new Error("key must be string");
2005+
if (!(this.key instanceof AST_String)) throw new Error("key must be AST_String");
20162006
},
20172007
}, AST_SymbolConst);
20182008

@@ -2066,14 +2056,14 @@ var AST_SymbolRef = DEFNODE("SymbolRef", "fixed in_arg redef", {
20662056
var AST_SymbolExport = DEFNODE("SymbolExport", "alias", {
20672057
$documentation: "Reference in an `export` statement",
20682058
$propdoc: {
2069-
alias: "[string] the `export` alias",
2059+
alias: "[AST_String] the `export` alias",
20702060
},
20712061
_equals: function(node) {
20722062
return this.name == node.name
2073-
&& this.alias == node.alias;
2063+
&& this.alias.equals(node.alias);
20742064
},
20752065
_validate: function() {
2076-
if (typeof this.alias != "string") throw new Error("alias must be string");
2066+
if (!(this.alias instanceof AST_String)) throw new Error("alias must be AST_String");
20772067
},
20782068
}, AST_SymbolRef);
20792069

lib/compress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Compressor.prototype.compress = function(node) {
294294
function export_symbol(sym) {
295295
if (!(sym instanceof AST_SymbolDeclaration)) return;
296296
var node = make_node(AST_SymbolExport, sym, sym);
297-
node.alias = node.name;
297+
node.alias = make_node(AST_String, node, { value: node.name });
298298
props.push(node);
299299
}
300300
});

lib/mozilla-ast.js

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,22 @@
316316
});
317317
},
318318
ExportAllDeclaration: function(M) {
319-
var alias = M.exported ? read_name(M.exported) : "*";
319+
var start = my_start_token(M);
320+
var end = my_end_token(M);
320321
return new AST_ExportForeign({
321-
start: my_start_token(M),
322-
end: my_end_token(M),
323-
aliases: [ alias ],
324-
keys: [ "*" ],
325-
path: M.source.value,
322+
start: start,
323+
end: end,
324+
aliases: [ M.exported ? from_moz_alias(M.exported) : new AST_String({
325+
start: start,
326+
value: "*",
327+
end: end,
328+
}) ],
329+
keys: [ new AST_String({
330+
start: start,
331+
value: "*",
332+
end: end,
333+
}) ],
334+
path: from_moz(M.source),
326335
});
327336
},
328337
ExportDefaultDeclaration: function(M) {
@@ -359,54 +368,64 @@
359368
if (M.source) {
360369
var aliases = [], keys = [];
361370
M.specifiers.forEach(function(prop) {
362-
aliases.push(read_name(prop.exported));
363-
keys.push(read_name(prop.local));
371+
aliases.push(from_moz_alias(prop.exported));
372+
keys.push(from_moz_alias(prop.local));
364373
});
365374
return new AST_ExportForeign({
366375
start: my_start_token(M),
367376
end: my_end_token(M),
368377
aliases: aliases,
369378
keys: keys,
370-
path: M.source.value,
379+
path: from_moz(M.source),
371380
});
372381
}
373382
return new AST_ExportReferences({
374383
start: my_start_token(M),
375384
end: my_end_token(M),
376385
properties: M.specifiers.map(function(prop) {
377386
var sym = new AST_SymbolExport(from_moz(prop.local));
378-
sym.alias = read_name(prop.exported);
387+
sym.alias = from_moz_alias(prop.exported);
379388
return sym;
380389
}),
381390
});
382391
},
383392
ImportDeclaration: function(M) {
393+
var start = my_start_token(M);
394+
var end = my_end_token(M);
384395
var all = null, def = null, props = null;
385396
M.specifiers.forEach(function(prop) {
386397
var sym = new AST_SymbolImport(from_moz(prop.local));
387398
switch (prop.type) {
388399
case "ImportDefaultSpecifier":
389400
def = sym;
390-
def.key = "";
401+
def.key = new AST_String({
402+
start: start,
403+
value: "",
404+
end: end,
405+
});
391406
break;
392407
case "ImportNamespaceSpecifier":
393408
all = sym;
394-
all.key = "*";
409+
all.key = new AST_String({
410+
start: start,
411+
value: "*",
412+
end: end,
413+
});
395414
break;
396415
default:
397-
sym.key = prop.imported.name || syn.name;
416+
sym.key = from_moz_alias(prop.imported);
398417
if (!props) props = [];
399418
props.push(sym);
400419
break;
401420
}
402421
});
403422
return new AST_Import({
404-
start: my_start_token(M),
405-
end: my_end_token(M),
423+
start: start,
424+
end: end,
406425
all: all,
407426
default: def,
408427
properties: props,
409-
path: M.source.value,
428+
path: from_moz(M.source),
410429
});
411430
},
412431
ImportExpression: function(M) {
@@ -797,83 +816,68 @@
797816
});
798817

799818
def_to_moz(AST_ExportForeign, function To_Moz_ExportAllDeclaration_ExportNamedDeclaration(M) {
800-
if (M.keys[0] == "*") return {
819+
if (M.keys[0].value == "*") return {
801820
type: "ExportAllDeclaration",
802-
exported: M.aliases[0] == "*" ? null : {
803-
type: "Identifier",
804-
name: M.aliases[0],
805-
},
806-
source: {
807-
type: "Literal",
808-
value: M.path,
809-
},
821+
exported: M.aliases[0].value == "*" ? null : to_moz_alias(M.aliases[0]),
822+
source: to_moz(M.path),
810823
};
811824
var specifiers = [];
812825
for (var i = 0; i < M.aliases.length; i++) {
813-
specifiers.push({
826+
specifiers.push(set_moz_loc({
827+
start: M.keys[i].start,
828+
end: M.aliases[i].end,
829+
}, {
814830
type: "ExportSpecifier",
815-
exported: {
816-
type: "Identifier",
817-
name: M.aliases[i],
818-
},
819-
local: {
820-
type: "Identifier",
821-
name: M.keys[i],
822-
},
823-
});
831+
local: to_moz_alias(M.keys[i]),
832+
exported: to_moz_alias(M.aliases[i]),
833+
}));
824834
}
825835
return {
826836
type: "ExportNamedDeclaration",
827837
specifiers: specifiers,
828-
source: {
829-
type: "Literal",
830-
value: M.path,
831-
},
838+
source: to_moz(M.path),
832839
};
833840
});
834841

835842
def_to_moz(AST_ExportReferences, function To_Moz_ExportNamedDeclaration_specifiers(M) {
836843
return {
837844
type: "ExportNamedDeclaration",
838845
specifiers: M.properties.map(function(prop) {
839-
return {
846+
return set_moz_loc({
847+
start: prop.start,
848+
end: prop.alias.end,
849+
}, {
840850
type: "ExportSpecifier",
841851
local: to_moz(prop),
842-
exported: {
843-
type: "Identifier",
844-
name: prop.alias,
845-
},
846-
};
852+
exported: to_moz_alias(prop.alias),
853+
});
847854
}),
848855
};
849856
});
850857

851858
def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
852859
var specifiers = M.properties ? M.properties.map(function(prop) {
853-
return {
860+
return set_moz_loc({
861+
start: prop.key.start,
862+
end: prop.end,
863+
}, {
854864
type: "ImportSpecifier",
855865
local: to_moz(prop),
856-
imported: {
857-
type: "Identifier",
858-
name: prop.key,
859-
},
860-
};
866+
imported: to_moz_alias(prop.key),
867+
});
861868
}) : [];
862-
if (M.all) specifiers.unshift({
869+
if (M.all) specifiers.unshift(set_moz_loc(M.all, {
863870
type: "ImportNamespaceSpecifier",
864871
local: to_moz(M.all),
865-
});
866-
if (M.default) specifiers.unshift({
872+
}));
873+
if (M.default) specifiers.unshift(set_moz_loc(M.default, {
867874
type: "ImportDefaultSpecifier",
868875
local: to_moz(M.default),
869-
});
876+
}));
870877
return {
871878
type: "ImportDeclaration",
872879
specifiers: specifiers,
873-
source: {
874-
type: "Literal",
875-
value: M.path,
876-
},
880+
source: to_moz(M.path),
877881
};
878882
});
879883

@@ -1220,6 +1224,14 @@
12201224
return node;
12211225
}
12221226

1227+
function from_moz_alias(moz) {
1228+
return new AST_String({
1229+
start: my_start_token(moz),
1230+
value: read_name(moz),
1231+
end: my_end_token(moz),
1232+
});
1233+
}
1234+
12231235
AST_Node.from_mozilla_ast = function(node) {
12241236
var save_stack = FROM_MOZ_STACK;
12251237
FROM_MOZ_STACK = [];
@@ -1271,6 +1283,13 @@
12711283
return node != null ? node.to_mozilla_ast() : null;
12721284
}
12731285

1286+
function to_moz_alias(alias) {
1287+
return is_identifier_string(alias.value) ? set_moz_loc(alias, {
1288+
type: "Identifier",
1289+
name: alias.value,
1290+
}) : to_moz(alias);
1291+
}
1292+
12741293
function to_moz_block(node) {
12751294
return {
12761295
type: "BlockStatement",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy