Skip to content

Commit

Permalink
Parse import.meta as a special expression. Closes #1349
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Feb 23, 2023
1 parent c7d844b commit 8a1b240
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/compress/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
AST_Function,
AST_If,
AST_Import,
AST_ImportMeta,
AST_Jump,
AST_LabeledStatement,
AST_Lambda,
Expand Down Expand Up @@ -310,6 +311,7 @@ export function is_nullish(node, compressor) {
|| this.body && this.body.has_side_effects(compressor)
|| this.alternative && this.alternative.has_side_effects(compressor);
});
def_has_side_effects(AST_ImportMeta, return_false);
def_has_side_effects(AST_LabeledStatement, function(compressor) {
return this.body.has_side_effects(compressor);
});
Expand Down Expand Up @@ -413,6 +415,7 @@ export function is_nullish(node, compressor) {
def_may_throw(AST_Lambda, return_false);
def_may_throw(AST_SymbolDeclaration, return_false);
def_may_throw(AST_This, return_false);
def_may_throw(AST_ImportMeta, return_false);

function any(list, compressor) {
for (var i = list.length; --i >= 0;)
Expand Down Expand Up @@ -776,6 +779,11 @@ export function is_lhs(node, parent) {
var name = this.name + suffix;
if (HOP(defines, name)) return to_node(defines[name], this);
});
def_find_defs(AST_ImportMeta, function(compressor, suffix) {
var defines = compressor.option("global_defs");
var name = "import.meta" + suffix;
if (HOP(defines, name)) return to_node(defines[name], this);
});
})(function(node, func) {
node.DEFMETHOD("_find_defs", func);
});
Expand Down
4 changes: 2 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,7 @@ function parse($TEXT, options) {
if (is("operator", "new")) {
return new_(allow_calls);
}
if (is("operator", "import")) {
if (is("name", "import") && is_token(peek(), "punc", ".")) {
return import_meta();
}
var start = S.token;
Expand Down Expand Up @@ -2803,7 +2803,7 @@ function parse($TEXT, options) {

function import_meta() {
var start = S.token;
expect_token("operator", "import");
expect_token("name", "import");
expect_token("punc", ".");
expect_token("name", "meta");
return subscripts(new AST_ImportMeta({
Expand Down
87 changes: 87 additions & 0 deletions test/compress/import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

import_meta_basic: {
options = { defaults: true }
input: {
import.meta
import.meta.something
import.meta?.something
}
expect: {
import.meta,
import.meta.something,
import.meta?.something
}
}

propmangle: {
mangle = {
properties: {
keep_quoted: "strict",
undeclared: true,
debug: true,
}
}
input: {
import.meta
import.meta.prop1
import.meta["kept"]
}
expect: {
import.meta
import.meta._$prop1$_
import.meta["kept"]
}
}

pure_getters: {
options = {
defaults: true,
pure_getters: true,
unused: false,
}
input: {
import.meta
}
expect: {
import.meta
}
}

global_defs: {
options = {
global_defs: {
"import.meta.LIT": 123,
"@import.meta.OBJ": "{'hello': 123}",
"@import.meta.FUNC": "() => false"
}
}
input: {
import.meta;
leak(import.meta.LIT)
leak(import.meta.OBJ)
leak(import.meta.FUNC)
}
expect: {
import.meta;
leak(123);
leak({hello: 123});
leak(() => false);
}
}

global_defs_whole: {
options = {
defaults: true,
global_defs: {
"import.meta": {
url: "http://example.com",
},
}
}
input: {
console.log(import.meta.url);
}
expect: {
console.log("http://example.com");
}
}

0 comments on commit 8a1b240

Please sign in to comment.