fix(es/utils): Fix wrong apis (#4604)

# API Changes

## `swc_ecma_utils`

### Renames

 - `find_ids` => `find_pat_ids`
 - `prepend` => `prepend_stmt`
 - `as_bool` => `cast_to_bool`
 - `as_number` => `as_pure_number`
 - `as_string` => `cast_to_string`
 - (`as_pure_bool`, `cast_to_number` is not renamed)
 - `UsageFinder` => `IdentUsageFinder`.
 - `IdentFinder` => `IdentRefFinder`.

## Changes

 - Re-export of `HANDLER` from `swc_common::errors` is removed. Use `swc_common::error::HANDLER` instead.
 - Re-export of `Id` from `swc_ecma_ast` is removed. Use `swc_ecma_ast::Id` instead.
 - `ExprCtx` is used for apis related to `Expr`.
 - `preserve_effects` is now a method of `ExprCtx`. It was a top-level function.
 - `extract_side_effects_to` is now a method of `ExprCtx`. It was a top-level function.
 - `IdentUsageFinder` (previously `UsageFinder`) now takes `Id` instead of `Ident`.
 - `IdentRefFinder` (previously `IdentFinder`) now takes `Id` instead of `Ident`.
 - `ExprExt::is_ident_ref_to` is removed.
 - `Expr::is_global_ref_to` is added.
 - `:🆔:id` is removed. Use `Ident::to_id()` instead.
This commit is contained in:
Donny/강동윤 2022-05-11 13:36:22 +09:00 committed by GitHub
parent 74ffa5ab9c
commit 6a97c7bef8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
193 changed files with 1210 additions and 874 deletions

View File

@ -14,6 +14,7 @@ use swc::{
use swc_common::{
chain,
comments::{Comment, SingleThreadedComments},
errors::HANDLER,
BytePos, FileName,
};
use swc_ecma_ast::{EsVersion, *};
@ -22,7 +23,6 @@ use swc_ecma_transforms::{
helpers::{self, Helpers},
pass::noop,
};
use swc_ecma_utils::HANDLER;
use swc_ecma_visit::{Fold, FoldWith};
use testing::{NormalizedOutput, StdErr, Tester};
use walkdir::WalkDir;

View File

@ -1,3 +1,5 @@
class D0 extends Base {
}
class D1 extends getBase() {
constructor(){
super("abc", "def"), this.x = "x", this.y = "y";

View File

@ -1,2 +1,2 @@
var temp = 10;
++temp, --temp, temp++, temp--, --temp, --temp, --temp, --temp, --temp, temp--, temp++, temp--, temp++, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, temp++, temp--, ++temp, --temp, temp++, temp--, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, --temp, --temp, --temp, --temp;
++temp, --temp, temp++, temp--, --temp, --temp, --temp, --temp, --temp, temp--, temp++, temp--, temp++, temp--, temp--, temp--, temp--, temp--, --temp, --temp, --temp, --temp, --temp, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, temp++, temp--, ++temp, --temp, temp++, temp--, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, --temp, --temp, --temp, --temp;

View File

@ -1,2 +1,2 @@
var temp = 10;
++temp, --temp, temp++, temp--, --temp, --temp, --temp, --temp, --temp, temp--, temp++, temp--, temp++, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, --temp, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, temp++, temp--, ++temp, --temp, temp++, temp--, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, --temp, --temp, --temp, --temp;
++temp, --temp, temp++, temp--, --temp, --temp, --temp, --temp, --temp, temp--, temp++, temp--, temp++, temp--, temp--, temp--, temp--, temp--, --temp, --temp, --temp, --temp, --temp, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, temp++, temp--, ++temp, --temp, temp++, temp--, ++temp, ++temp, ++temp, ++temp, ++temp, --temp, --temp, --temp, --temp, --temp;

View File

@ -1,2 +1,2 @@
var t1 = 10;
++t1, t1++, --t1, ++t1, ++t1, t1++, t1++, --t1, --t1, ++t1, t1++, --t1;
++t1, ++t1, --t1, ++t1, ++t1, t1++, ++t1, --t1, --t1, ++t1, ++t1, --t1;

View File

@ -1,2 +1,2 @@
var t1 = 10;
++t1, t1++, --t1, ++t1, ++t1, t1++, t1++, --t1, --t1, ++t1, t1++, --t1;
++t1, ++t1, --t1, ++t1, ++t1, t1++, ++t1, --t1, --t1, ++t1, ++t1, --t1;

View File

@ -1,4 +1,4 @@
var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
var E01, E02, E03, E04, E05, E06, E07, E08, E10, E11, E12, E20;
!function(E01) {
E01[E01.A = 0] = "A";
}(E01 || (E01 = {})), function(E02) {
@ -15,7 +15,7 @@ var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07.C = "hi", E07[E07.D = 10] = "D", E07[E07.E = 11] = "E", E07.F = "bye";
}(E07 || (E07 = {})), function(E08) {
E08[E08.A = 10] = "A", E08.B = "hello", E08[E08.C = 10] = "C", E08.D = "hello", E08[E08.E = 10] = "E";
}(E08 || (E08 = {})), function(E11) {
}(E08 || (E08 = {})), E10 || (E10 = {}), function(E11) {
E11[E11.A = 0] = "A", E11[E11.B = 1] = "B", E11[E11.C = 2] = "C";
}(E11 || (E11 = {})), function(E12) {
E12[E12.A = 1] = "A", E12[E12.B = 2] = "B", E12[E12.C = 4] = "C";

View File

@ -1,4 +1,4 @@
var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
var E01, E02, E03, E04, E05, E06, E07, E08, E10, E11, E12, E20;
!function(E01) {
E01[E01.A = 0] = "A";
}(E01 || (E01 = {})), function(E02) {
@ -15,7 +15,7 @@ var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07.C = "hi", E07[E07.D = 10] = "D", E07[E07.E = 11] = "E", E07.F = "bye";
}(E07 || (E07 = {})), function(E08) {
E08[E08.A = 10] = "A", E08.B = "hello", E08[E08.C = 10] = "C", E08.D = "hello", E08[E08.E = 10] = "E";
}(E08 || (E08 = {})), function(E11) {
}(E08 || (E08 = {})), E10 || (E10 = {}), function(E11) {
E11[E11.A = 0] = "A", E11[E11.B = 1] = "B", E11[E11.C = 2] = "C";
}(E11 || (E11 = {})), function(E12) {
E12[E12.A = 1] = "A", E12[E12.B = 2] = "B", E12[E12.C = 4] = "C";

View File

@ -0,0 +1,2 @@
class My extends Set {
}

View File

@ -1,3 +1,4 @@
var Tag1, Tag2;
ab.kind;
const x1 = {
a: 'foo',
@ -6,4 +7,4 @@ const x1 = {
a: 'foo',
b: !0
};
x1[k] = 'bar', x2[k] = 'bar', s2 = s1 = s2, t2 = t1 = t2;
x1[k] = 'bar', x2[k] = 'bar', Tag1 || (Tag1 = {}), Tag2 || (Tag2 = {}), s2 = s1 = s2, t2 = t1 = t2;

View File

@ -1,9 +1,9 @@
ab.kind;
var x1 = {
var Tag1, Tag2, x1 = {
a: "foo",
b: 42
}, x2 = {
a: "foo",
b: !0
};
x1[k] = "bar", x2[k] = "bar", s2 = s1 = s2, t2 = t1 = t2;
x1[k] = "bar", x2[k] = "bar", Tag1 || (Tag1 = {}), Tag2 || (Tag2 = {}), s2 = s1 = s2, t2 = t1 = t2;

View File

@ -1,3 +1,4 @@
var Tag1, Tag2;
ab.kind, f10(a1), f10(a2);
const x1 = {
a: 'foo',
@ -6,4 +7,4 @@ const x1 = {
a: 'foo',
b: !0
};
x1[k] = 'bar', x2[k] = 'bar', s2 = s1 = s2, t2 = t1 = t2;
x1[k] = 'bar', x2[k] = 'bar', Tag1 || (Tag1 = {}), Tag2 || (Tag2 = {}), s2 = s1 = s2, t2 = t1 = t2;

View File

@ -1,9 +1,9 @@
ab.kind, f10(a1), f10(a2);
var x1 = {
var Tag1, Tag2, x1 = {
a: "foo",
b: 42
}, x2 = {
a: "foo",
b: !0
};
x1[k] = "bar", x2[k] = "bar", s2 = s1 = s2, t2 = t1 = t2;
x1[k] = "bar", x2[k] = "bar", Tag1 || (Tag1 = {}), Tag2 || (Tag2 = {}), s2 = s1 = s2, t2 = t1 = t2;

View File

@ -1,15 +1,15 @@
var C, DD;
export var A;
!function(A) {}(A || (A = {}));
A || (A = {});
export var B;
!function(B) {
B[B.Member = 0] = "Member";
}(B || (B = {})), C = {};
!function(DD) {}(DD || (DD = {}));
}(B || (B = {})), C || (C = {});
DD || (DD = {});
export var E;
!function(E) {}(E || (E = {}));
E || (E = {});
export var F;
!function(F) {}(F || (F = {}));
F || (F = {});
export var G;
!function(G) {
G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C";

View File

@ -1,15 +1,15 @@
var C, DD;
export var A;
!function(A) {}(A || (A = {}));
A || (A = {});
export var B;
!function(B) {
B[B.Member = 0] = "Member";
}(B || (B = {})), C = {};
!function(DD) {}(DD || (DD = {}));
}(B || (B = {})), C || (C = {});
DD || (DD = {});
export var E;
!function(E) {}(E || (E = {}));
E || (E = {});
export var F;
!function(F) {}(F || (F = {}));
F || (F = {});
export var G;
!function(G) {
G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C";

View File

@ -1,4 +1,4 @@
var ENUM;
var ENUM, ENUM1;
!function(ENUM) {
ENUM[ENUM.A = 0] = "A", ENUM[ENUM.B = 1] = "B", ENUM[ENUM.C = 2] = "C";
}(ENUM || (ENUM = {})), ENUM.B, ENUM.B, ENUM.C, ENUM.B, ENUM.C, ENUM.B;
}(ENUM || (ENUM = {})), ENUM1 || (ENUM1 = {}), ENUM.B, ENUM.B, ENUM.C, ENUM.B, ENUM.C, ENUM.B;

View File

@ -1,4 +1,4 @@
var ENUM;
var ENUM, ENUM1;
!function(ENUM) {
ENUM[ENUM.A = 0] = "A", ENUM[ENUM.B = 1] = "B", ENUM[ENUM.C = 2] = "C";
}(ENUM || (ENUM = {})), ENUM.B, ENUM.B, ENUM.C, ENUM.B, ENUM.C, ENUM.B;
}(ENUM || (ENUM = {})), ENUM1 || (ENUM1 = {}), ENUM.B, ENUM.B, ENUM.C, ENUM.B, ENUM.C, ENUM.B;

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.B, ENUM1.B, ENUM1[""], ENUM1.B;

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.B, ENUM1.B, ENUM1[""], ENUM1.B;

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -3,4 +3,4 @@ var E, C = function() {
"use strict";
swcHelpers.classCallCheck(this, C);
};
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -2,4 +2,4 @@ var m;
!function(m1) {
var x;
m1.x = x;
}(m || (m = {})), m || (m = {}), x;
}(m || (m = {})), m || (m = {}), x, x;

View File

@ -2,4 +2,4 @@ var m;
!function(m1) {
var x;
m1.x = x;
}(m || (m = {})), m || (m = {}), x;
}(m || (m = {})), m || (m = {}), x, x, x;

View File

@ -2,4 +2,4 @@ var m;
!function(m1) {
var x;
m1.x = x;
}(m || (m = {})), m || (m = {}), x;
}(m || (m = {})), m || (m = {}), x, x;

View File

@ -2,4 +2,4 @@ var m;
!function(m1) {
var x;
m1.x = x;
}(m || (m = {})), m || (m = {}), x;
}(m || (m = {})), m || (m = {}), x, x, x;

View File

@ -2,9 +2,9 @@ import * as swcHelpers from "@swc/helpers";
var C = function() {
"use strict";
function C() {
arguments.length > 0 && void 0 !== arguments[0] && arguments[0], arguments.length > 1 && void 0 !== arguments[1] && arguments[1], swcHelpers.classCallCheck(this, C);
arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : b, arguments.length > 1 && void 0 !== arguments[1] && arguments[1], swcHelpers.classCallCheck(this, C);
}
return C.prototype.method = function() {
arguments.length > 0 && void 0 !== arguments[0] && arguments[0], arguments.length > 1 && void 0 !== arguments[1] && arguments[1];
arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : b, arguments.length > 1 && void 0 !== arguments[1] && arguments[1];
}, C;
}();

View File

@ -1 +1 @@
a, a, a = 1, b = 2;
a, a, a, b, a = 1, b = 2;

View File

@ -1 +1 @@
a, a, a = 1, b = 2;
a, a, a, b, a = 1, b = 2;

View File

@ -1 +1 @@
new Date < A || C;
new Date < A ? B : C;

View File

@ -1 +1 @@
new Date < A || C;
new Date < A ? B : C;

View File

@ -1,2 +1,2 @@
export var SignatureFlags;
!function(SignatureFlags) {}(SignatureFlags || (SignatureFlags = {}));
SignatureFlags || (SignatureFlags = {});

View File

@ -1,2 +1,2 @@
export var SignatureFlags;
!function(SignatureFlags) {}(SignatureFlags || (SignatureFlags = {}));
SignatureFlags || (SignatureFlags = {});

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -0,0 +1,2 @@
class C extends A {
}

View File

@ -0,0 +1,2 @@
class C extends A {
}

View File

@ -0,0 +1 @@
a, b, c, d;

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM1.B;

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM1.B;

View File

@ -2,7 +2,7 @@ import * as swcHelpers from "@swc/helpers";
export class Reflect {
}
export var Baz;
!function(Baz) {}(Baz || (Baz = {}));
Baz || (Baz = {});
class _class {
}
class C extends B {
@ -23,8 +23,14 @@ C._ = [
})(),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
(()=>{
let Reflect1;
Reflect1 || (Reflect1 = {}), swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
})(),
(()=>{
let Reflect2;
Reflect2 || (Reflect2 = {}), swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
})(),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
void swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C),
@ -39,7 +45,14 @@ C._ = [
null
];
super.w();
})(), super.w(), super.w(), super.w(), super.w(), super.w(), super.w(), super.w(), super.w(), super.w();
})(), super.w(), super.w(), super.w(), (()=>{
let Reflect3;
Reflect3 || (Reflect3 = {}), super.w();
})(), (()=>{
let Reflect4;
Reflect4 || (Reflect4 = {}), super.w();
})(), super.w(), super.w(), super.w(), super.w();
var Reflect = null;
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
@ -55,6 +68,7 @@ var [Reflect] = [
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
var Reflect = null;
class C extends B {
}
super.w();
@ -70,9 +84,21 @@ var [Reflect] = [
class C extends B {
}
super.w();
class Reflect {
}
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
class Reflect {
}
class C extends B {
}
function Reflect() {}
super.w();
class C extends B {
}
function Reflect() {}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
class C extends B {
}
super.w();
@ -81,22 +107,16 @@ class C extends B {
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
class C extends B {
}
super.w();
super.w(), Reflect || (Reflect = {});
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect || (Reflect = {});
class C extends B {
}
super.w();
super.w(), Reflect || (Reflect = {});
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
class C extends B {
}
super.w();
class C extends B {
}
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect || (Reflect = {});
class C extends B {
}
super.w();

View File

@ -6,7 +6,7 @@ export var Reflect = function() {
swcHelpers.classCallCheck(this, Reflect);
};
export var Baz;
!function(Baz) {}(Baz || (Baz = {}));
Baz || (Baz = {});
var _class = function() {
"use strict";
swcHelpers.classCallCheck(this, _class);
@ -184,7 +184,7 @@ var C = function(B) {
}
return C;
}(B);
_superprop_get_w().call(_this), Reflect = {};
_superprop_get_w().call(_this), Reflect || (Reflect = {});
var C = function(B) {
"use strict";
swcHelpers.inherits(C, B);
@ -194,7 +194,7 @@ var C = function(B) {
}
return C;
}(B);
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect = {};
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect || (Reflect = {});
var C = function(B) {
"use strict";
swcHelpers.inherits(C, B);
@ -204,7 +204,7 @@ var C = function(B) {
}
return C;
}(B);
_superprop_get_w().call(_this), Reflect = {};
_superprop_get_w().call(_this), Reflect || (Reflect = {});
var C = function(B) {
"use strict";
swcHelpers.inherits(C, B);
@ -214,7 +214,7 @@ var C = function(B) {
}
return C;
}(B);
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect = {};
C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C), Reflect || (Reflect = {});
var C = function(B) {
"use strict";
swcHelpers.inherits(C, B);

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
E || (E = {});

View File

@ -5,5 +5,5 @@ var M;
}, (M1.S || (M1.S = {})).Bar = class {
};
}(M || (M = {})), function(M2) {
M2.S || (M2.S = {});
}(M || (M = {})), M || (M = {}), S.Bar, S.Bar, M || (M = {});
Foo, Foo, M2.S || (M2.S = {}), Foo, Foo, Bar, Bar;
}(M || (M = {})), M || (M = {}), S.Bar, S.Bar, M || (M = {}), Foo, Foo;

View File

@ -10,5 +10,5 @@ import * as swcHelpers from "@swc/helpers";
swcHelpers.classCallCheck(this, Bar);
}, S.Bar = Bar;
}(M || (M = {})), function(M2) {
M2.S || (M2.S = {});
}(M || (M = {})), M || (M = {}), S.Bar, S.Bar, M || (M = {});
Foo, Foo, M2.S || (M2.S = {}), Foo, Foo, Bar, Bar;
}(M || (M = {})), M || (M = {}), S.Bar, S.Bar, M || (M = {}), Foo, Foo;

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
var E, V;
E || (E = {}), V || (V = {});

View File

@ -1,2 +1,2 @@
var E;
!function(E) {}(E || (E = {}));
var E, V;
E || (E = {}), V || (V = {});

View File

@ -1,2 +1,3 @@
var Tag2;
let value;
isNonBlank(value) ? doThis(value) : doThat(value), isNonBlank2(value) ? doThis2(value) : doThat2(value);
isNonBlank(value) ? doThis(value) : doThat(value), Tag2 || (Tag2 = {}), isNonBlank2(value) ? doThis2(value) : doThat2(value);

View File

@ -1,2 +1,2 @@
var value;
isNonBlank(value) ? doThis(value) : doThat(value), isNonBlank2(value) ? doThis2(value) : doThat2(value);
var value, Tag2;
isNonBlank(value) ? doThis(value) : doThat(value), Tag2 || (Tag2 = {}), isNonBlank2(value) ? doThis2(value) : doThat2(value);

View File

@ -0,0 +1,4 @@
class C extends T {
}
class C2 extends U {
}

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
var ENUM, ENUM1, z;
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM[0], ENUM1.B, ENUM1.B;
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM[0], ENUM1.B, ENUM1.B, z || (z = {});

View File

@ -1,7 +1,7 @@
var ENUM, ENUM1;
var ENUM, ENUM1, z;
import * as swcHelpers from "@swc/helpers";
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), void 0 === ENUM || swcHelpers.typeOf(ENUM), void 0 === ENUM1 || swcHelpers.typeOf(ENUM1), swcHelpers.typeOf(ENUM1.A), swcHelpers.typeOf(ENUM[0] + ENUM1.B), swcHelpers.typeOf(void 0 === ENUM ? "undefined" : swcHelpers.typeOf(ENUM)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(ENUM[0] + ENUM1.B))), void 0 === ENUM || swcHelpers.typeOf(ENUM), void 0 === ENUM1 || swcHelpers.typeOf(ENUM1), swcHelpers.typeOf(ENUM1.B), void 0 === ENUM || swcHelpers.typeOf(ENUM);
}(ENUM1 || (ENUM1 = {})), void 0 === ENUM || swcHelpers.typeOf(ENUM), void 0 === ENUM1 || swcHelpers.typeOf(ENUM1), swcHelpers.typeOf(ENUM1.A), swcHelpers.typeOf(ENUM[0] + ENUM1.B), swcHelpers.typeOf(void 0 === ENUM ? "undefined" : swcHelpers.typeOf(ENUM)), swcHelpers.typeOf(swcHelpers.typeOf(swcHelpers.typeOf(ENUM[0] + ENUM1.B))), void 0 === ENUM || swcHelpers.typeOf(ENUM), void 0 === ENUM1 || swcHelpers.typeOf(ENUM1), swcHelpers.typeOf(ENUM1.B), void 0 === ENUM || swcHelpers.typeOf(ENUM), z || (z = {});
z: void 0 === ENUM || swcHelpers.typeOf(ENUM);
z: void 0 === ENUM1 || swcHelpers.typeOf(ENUM1);

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM[0], ENUM1.B, ENUM1.B;

View File

@ -1,4 +1,4 @@
var ENUM, ENUM1;
!function(ENUM) {}(ENUM || (ENUM = {})), function(ENUM1) {
ENUM || (ENUM = {}), function(ENUM1) {
ENUM1[ENUM1.A = 0] = "A", ENUM1[ENUM1.B = 1] = "B", ENUM1[ENUM1[""] = 2] = "";
}(ENUM1 || (ENUM1 = {})), ENUM1.A, ENUM[0], ENUM1.B, ENUM[0], ENUM1.B, ENUM1.B;

View File

@ -1,4 +1,4 @@
Promise.all(assignAll).then(async function (value) {
Promise.all(assignAll).then(async function (obj) {
let removeQuery = `DELETE FROM "TABLE" WHERE "UUID" IN ( `;
for (let key in obj) {

View File

@ -6,13 +6,13 @@ Promise.all(assignAll).then(function() {
return b.wrap(function(a) {
for(;;)switch(a.prev = a.next){
case 0:
d = 'DELETE FROM "TABLE" WHERE "UUID" IN ( ', a.t0 = regeneratorRuntime.keys(obj);
d = 'DELETE FROM "TABLE" WHERE "UUID" IN ( ', a.t0 = regeneratorRuntime.keys(c);
case 2:
if ((a.t1 = a.t0()).done) {
a.next = 12;
break;
}
return f = obj[e = a.t1.value], d += "'".concat(f.id, "', "), a.next = 8, listOfUser(f.id);
return f = c[e = a.t1.value], d += "'".concat(f.id, "', "), a.next = 8, listOfUser(f.id);
case 8:
(g = a.sent).forEach(function(a) {
insertQuery += 'INSERT INTO "TABLE"("UUID", id, other_ids_here) VALUES (\''.concat(uuidv4(), "', '").concat(f.id, "', now());");

View File

@ -1,8 +1,8 @@
use std::sync::atomic::Ordering;
use std::{collections::HashMap, sync::atomic::Ordering};
use anyhow::Error;
use swc_atoms::js_word;
use swc_common::{Span, SyntaxContext, DUMMY_SP};
use swc_common::{collections::AHashMap, Span, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::{ModuleItem, *};
use swc_ecma_utils::{quote_ident, undefined, ExprFactory};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
@ -68,6 +68,7 @@ where
let stmt = ModuleItem::Stmt(wrap_module(
SyntaxContext::empty(),
SyntaxContext::empty().apply_mark(self.unresolved_mark),
info.local_ctxt(),
load_var,
module.into(),
@ -91,10 +92,22 @@ where
fn wrap_module(
helper_ctxt: SyntaxContext,
unresolved_ctxt: SyntaxContext,
local_ctxt: SyntaxContext,
load_var: Ident,
dep: Module,
mut dep: Module,
) -> Stmt {
{
// Remap syntax context of `module` and `exports`
// Those are unresolved, but it's actually an injected variable.
let mut from = HashMap::default();
from.insert(("module".into(), unresolved_ctxt), local_ctxt);
from.insert(("exports".into(), unresolved_ctxt), local_ctxt);
dep.visit_mut_with(&mut Remapper { vars: from })
}
// ... body of foo
let module_fn = Expr::Fn(FnExpr {
ident: None,
@ -357,3 +370,17 @@ impl VisitMut for DefaultHandler {
}
}
}
struct Remapper {
vars: AHashMap<Id, SyntaxContext>,
}
impl VisitMut for Remapper {
noop_visit_mut_type!();
fn visit_mut_ident(&mut self, i: &mut Ident) {
if let Some(v) = self.vars.get(&i.to_id()).copied() {
i.span.ctxt = v;
}
}
}

View File

@ -4,7 +4,7 @@ use anyhow::{bail, Error};
use swc_atoms::js_word;
use swc_common::{SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{contains_top_level_await, find_ids, private_ident, ExprFactory};
use swc_ecma_utils::{contains_top_level_await, find_pat_ids, private_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, Fold};
use crate::{bundler::chunk::merge::Ctx, modules::Modules, Bundler, Load, ModuleId, Resolve};
@ -222,7 +222,7 @@ impl Fold for ExportToReturn {
self.export_id(ident.clone());
}
Decl::Var(decl) => {
let ids: Vec<Ident> = find_ids(decl);
let ids: Vec<Ident> = find_pat_ids(decl);
ids.into_iter().for_each(|id| self.export_id(id));
}
_ => unreachable!(),

View File

@ -10,7 +10,7 @@ use swc_common::{
FileName, SyntaxContext, DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, prepend, private_ident, quote_ident, ExprFactory};
use swc_ecma_utils::{find_pat_ids, prepend_stmt, private_ident, quote_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};
use EdgeDirection::Outgoing;
@ -237,7 +237,7 @@ where
for (_, stmt) in entry.iter() {
if let ModuleItem::Stmt(Stmt::Decl(Decl::Var(decl))) = stmt {
if decl.span.ctxt == injected_ctxt {
let ids: Vec<Id> = find_ids(decl);
let ids: Vec<Id> = find_pat_ids(decl);
declared_ids.extend(ids);
}
}
@ -288,7 +288,7 @@ where
}
ModuleItem::Stmt(Stmt::Decl(Decl::Var(decl))) => {
let ids: Vec<Id> = find_ids(decl);
let ids: Vec<Id> = find_pat_ids(decl);
for id in ids {
if *id.sym() == js_word!("default") {
@ -320,7 +320,7 @@ where
for (module_id, ctxts) in map.drain() {
for (_, stmt) in entry.iter() {
if let ModuleItem::Stmt(Stmt::Decl(Decl::Var(decl))) = stmt {
let ids: Vec<Id> = find_ids(decl);
let ids: Vec<Id> = find_pat_ids(decl);
for id in ids {
if *id.sym() == js_word!("default") {
@ -826,7 +826,7 @@ where
i
}
Decl::Var(v) => {
let ids: Vec<Ident> = find_ids(&v);
let ids: Vec<Ident> = find_pat_ids(&v);
//
new.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(v))));
@ -1394,7 +1394,7 @@ impl VisitMut for ImportMetaHandler<'_, '_> {
},
) {
Ok(key_value_props) => {
prepend(
prepend_stmt(
&mut n.body,
ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
span: n.span,

View File

@ -2,7 +2,7 @@ use indexmap::IndexMap;
use swc_atoms::{js_word, JsWord};
use swc_common::{FileName, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::find_ids;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use super::{
@ -156,7 +156,7 @@ where
Decl::Class(ref c) => &c.ident,
Decl::Fn(ref f) => &f.ident,
Decl::Var(ref var) => {
let ids: Vec<Id> = find_ids(&var.decls);
let ids: Vec<Id> = find_pat_ids(&var.decls);
for id in ids {
v.push(Specifier::Specific {
local: id,

View File

@ -11,7 +11,7 @@ use swc_ecma_transforms_base::{
helpers::{inject_helpers, HELPERS},
hygiene::hygiene,
};
use swc_ecma_utils::{contains_top_level_await, find_ids, private_ident, ExprFactory};
use swc_ecma_utils::{contains_top_level_await, find_pat_ids, private_ident, ExprFactory};
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith};
use crate::{hash::calc_hash, Bundle, BundleKind, Bundler, Load, ModuleType, Resolve};
@ -180,7 +180,7 @@ where
))));
}
Decl::Var(decl) => {
let ids: Vec<Ident> = find_ids(decl);
let ids: Vec<Ident> = find_pat_ids(decl);
props.extend(
ids.into_iter()
.map(Prop::Shorthand)

View File

@ -7,7 +7,7 @@ use swc_common::{
FileName, Mark, Spanned, SyntaxContext, DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, Id};
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use super::Bundler;
@ -641,7 +641,7 @@ where
}
}
let ids: Vec<Ident> = find_ids(&node.name);
let ids: Vec<Ident> = find_pat_ids(&node.name);
let decl = ImportDecl {
span,

View File

@ -137,9 +137,9 @@ where
data.module.visit_mut_with(&mut ClearMark);
let mut module = data
.module
.fold_with(&mut resolver(local_mark, local_mark, false));
let mut module =
data.module
.fold_with(&mut resolver(self.unresolved_mark, local_mark, false));
// {
// let code = self

View File

@ -86,6 +86,8 @@ where
{
config: Config,
unresolved_mark: Mark,
globals: &'a Globals,
cm: Lrc<SourceMap>,
loader: L,
@ -138,6 +140,7 @@ where
injected_ctxt,
scope: Default::default(),
hook,
unresolved_mark: Mark::new(),
}
})
}

View File

@ -20,10 +20,13 @@ where
node = node.fold_with(&mut constant_propagation())
}
if !self.config.disable_dce {
node = node.fold_with(&mut Repeat::new(dce::dce(dce::Config {
// TODO(kdy1): Apply mark to wrapped esms and use it at here.
module_mark: None,
})));
node = node.fold_with(&mut Repeat::new(dce::dce(
dce::Config {
// TODO(kdy1): Apply mark to wrapped esms and use it at here.
module_mark: None,
},
self.unresolved_mark,
)));
}
node
})

View File

@ -10,7 +10,7 @@ use swc_common::{
SourceMap, Spanned, SyntaxContext,
};
use swc_ecma_ast::*;
use swc_ecma_utils::find_ids;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use super::graph::Required;
@ -591,7 +591,7 @@ impl Visit for RequirementCalculator {
self.in_var_decl = true;
if self.in_weak {
let ids: Vec<Id> = find_ids(&var.name);
let ids: Vec<Id> = find_pat_ids(&var.name);
self.excluded.extend(ids);
}
@ -665,7 +665,7 @@ fn calc_deps(new: &[ModuleItem]) -> StmtDepGraph {
Decl::Var(vars) => {
for var in &vars.decls {
//
let ids: Vec<Id> = find_ids(&var.name);
let ids: Vec<Id> = find_pat_ids(&var.name);
for id in ids {
if var.init.is_none() {
uninitialized_ids.insert(id.clone(), idx);

View File

@ -17,7 +17,7 @@ use swc_ecma_codegen::{
};
use swc_ecma_minifier::option::MangleOptions;
use swc_ecma_transforms_base::{fixer::fixer, resolver};
use swc_ecma_utils::{find_ids, Id};
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{Visit, VisitMutWith, VisitWith};
use testing::assert_eq;
@ -1202,7 +1202,7 @@ impl Visit for ExportCollector {
self.exports.insert(ident.sym.to_string());
}
swc_ecma_ast::Decl::Var(var) => {
let ids: Vec<Id> = find_ids(var);
let ids: Vec<Id> = find_pat_ids(var);
self.exports
.extend(ids.into_iter().map(|v| v.0.to_string()));
}

View File

@ -1,7 +1,7 @@
use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, errors::HANDLER, Span};
use swc_ecma_ast::*;
use swc_ecma_utils::find_ids;
use swc_ecma_utils::find_pat_ids;
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use crate::rule::{visitor_rule, Rule};
@ -41,7 +41,7 @@ impl Visit for NoDupeArgs {
noop_visit_type!();
fn visit_function(&mut self, f: &Function) {
let variables: Vec<(JsWord, Span)> = find_ids(&f.params);
let variables: Vec<(JsWord, Span)> = find_pat_ids(&f.params);
self.check(variables);
@ -49,7 +49,7 @@ impl Visit for NoDupeArgs {
}
fn visit_arrow_expr(&mut self, arrow_fn: &ArrowExpr) {
let variables: Vec<(JsWord, Span)> = find_ids(&arrow_fn.params);
let variables: Vec<(JsWord, Span)> = find_pat_ids(&arrow_fn.params);
self.check(variables);
@ -57,7 +57,7 @@ impl Visit for NoDupeArgs {
}
fn visit_constructor(&mut self, n: &Constructor) {
let variables: Vec<(JsWord, Span)> = find_ids(&n.params);
let variables: Vec<(JsWord, Span)> = find_pat_ids(&n.params);
self.check(variables);

View File

@ -1,6 +1,6 @@
use std::path::PathBuf;
use swc_common::{input::SourceFileInput, Mark, SyntaxContext};
use swc_common::{errors::HANDLER, input::SourceFileInput, Mark, SyntaxContext};
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_lints::{
config::LintConfig,
@ -9,7 +9,6 @@ use swc_ecma_lints::{
};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
use swc_ecma_transforms_base::resolver;
use swc_ecma_utils::HANDLER;
use swc_ecma_visit::VisitMutWith;
#[testing::fixture("tests/pass/**/input.js")]

View File

@ -2,7 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
use swc_atoms::{js_word, JsWord};
use swc_common::{collections::AHashSet, SyntaxContext};
use swc_ecma_ast::*;
use swc_ecma_utils::{collect_decls, find_ids, ident::IdentLike, BindingCollector, Id, IsEmpty};
use swc_ecma_utils::{collect_decls, find_pat_ids, ident::IdentLike, BindingCollector, IsEmpty};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use swc_timer::timer;
@ -583,7 +583,7 @@ where
self.data.var_or_default(f.ident.to_id()).prevent_inline();
}
Decl::Var(v) => {
let ids = find_ids(v);
let ids = find_pat_ids(v);
for id in ids {
self.data.var_or_default(id).prevent_inline();

View File

@ -1,7 +1,6 @@
use swc_atoms::JsWord;
use swc_common::SyntaxContext;
use swc_ecma_ast::*;
use swc_ecma_utils::Id;
use super::{ctx::Ctx, ScopeKind};

View File

@ -2,7 +2,6 @@ use std::collections::hash_map::Entry;
use swc_common::collections::AHashSet;
use swc_ecma_ast::*;
use swc_ecma_utils::Id;
use super::{ScopeDataLike, Storage, VarDataLike};
use crate::analyzer::{ctx::Ctx, ProgramData, ScopeData, ScopeKind, VarUsageInfo};

View File

@ -1,6 +1,6 @@
use swc_common::{collections::AHashSet, pass::Repeated, util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, Id, StmtLike};
use swc_ecma_utils::{find_pat_ids, StmtLike};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
use super::util::drop_invalid_stmts;
@ -51,7 +51,7 @@ impl Hoister<'_> {
Some(stmt) => match stmt {
Stmt::Decl(Decl::Fn(..)) if self.config.hoist_fns => 1,
Stmt::Decl(Decl::Var(var)) if self.config.hoist_vars => {
let ids: Vec<Id> = find_ids(&var.decls);
let ids: Vec<Id> = find_pat_ids(&var.decls);
if ids.iter().any(|id| {
self.data
@ -103,7 +103,7 @@ impl Hoister<'_> {
)) if found_non_var_decl => {
let mut exprs = vec![];
for decl in var.decls {
let ids: Vec<Ident> = find_ids(&decl.name);
let ids: Vec<Ident> = find_pat_ids(&decl.name);
for id in ids {
if done.insert(id.to_id()) {

View File

@ -3,7 +3,7 @@ use std::iter::repeat_with;
use swc_atoms::js_word;
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, private_ident, Id};
use swc_ecma_utils::{find_pat_ids, private_ident};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use super::Optimizer;
@ -89,7 +89,7 @@ where
{
// If a function has a variable named `arguments`, we abort.
let data: Vec<Id> = find_ids(&f.body);
let data: Vec<Id> = find_pat_ids(&f.body);
for id in &data {
if id.0 == js_word!("arguments") {
return;

View File

@ -27,7 +27,7 @@ where
expr: &mut Expr,
is_ret_val_ignored: bool,
) -> bool {
let cost = negate_cost(expr, is_ret_val_ignored, is_ret_val_ignored);
let cost = negate_cost(&self.expr_ctx, expr, is_ret_val_ignored, is_ret_val_ignored);
if cost >= 0 {
return false;
}

View File

@ -30,7 +30,7 @@ where
_ => {}
}
if negate_cost(&stmt.test, true, false) < 0 {
if negate_cost(&self.expr_ctx, &stmt.test, true, false) < 0 {
report_change!("if_return: Negating `cond` of an if statement which has cons and alt");
let ctx = Ctx {
in_bool_ctx: true,
@ -63,7 +63,7 @@ where
_ => return,
};
if !cond.cons.may_have_side_effects() {
if !cond.cons.may_have_side_effects(&self.expr_ctx) {
self.changed = true;
report_change!("conditionals: `cond ? useless : alt` => `cond || alt`");
*e = Expr::Bin(BinExpr {
@ -75,7 +75,7 @@ where
return;
}
if !cond.alt.may_have_side_effects() {
if !cond.alt.may_have_side_effects(&self.expr_ctx) {
self.changed = true;
report_change!("conditionals: `cond ? cons : useless` => `cond && cons`");
*e = Expr::Bin(BinExpr {

View File

@ -169,7 +169,7 @@ where
//
for arg in &*args {
if arg.spread.is_some() || arg.expr.may_have_side_effects() {
if arg.spread.is_some() || arg.expr.may_have_side_effects(&self.expr_ctx) {
return;
}
}
@ -243,7 +243,7 @@ where
return;
}
if let Known(char_code) = args[0].expr.as_number() {
if let Known(char_code) = args[0].expr.as_pure_number(&self.expr_ctx) {
let v = char_code.floor() as u32;
if let Some(v) = char::from_u32(v) {
@ -348,7 +348,7 @@ where
}
if let Expr::Call(..) = e {
if let Some(value) = eval_as_number(e) {
if let Some(value) = eval_as_number(&self.expr_ctx, e) {
self.changed = true;
report_change!("evaluate: Evaluated an expression as `{}`", value);
@ -363,8 +363,8 @@ where
match e {
Expr::Bin(bin @ BinExpr { op: op!("**"), .. }) => {
let l = bin.left.as_number();
let r = bin.right.as_number();
let l = bin.left.as_pure_number(&self.expr_ctx);
let r = bin.right.as_pure_number(&self.expr_ctx);
if let Known(l) = l {
if let Known(r) = r {
@ -382,9 +382,9 @@ where
}
Expr::Bin(bin @ BinExpr { op: op!("/"), .. }) => {
let ln = bin.left.as_number();
let ln = bin.left.as_pure_number(&self.expr_ctx);
let rn = bin.right.as_number();
let rn = bin.right.as_pure_number(&self.expr_ctx);
if let (Known(ln), Known(rn)) = (ln, rn) {
// Prefer `0/0` over NaN.
if ln == 0.0 && rn == 0.0 {
@ -470,7 +470,7 @@ where
}
// Remove rhs of lhs if possible.
let v = left.right.as_pure_bool();
let v = left.right.as_pure_bool(&self.expr_ctx);
if let Known(v) = v {
// As we used as_pure_bool, we can drop it.
if v && e.op == op!("&&") {

View File

@ -1,6 +1,6 @@
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{prepend, undefined, StmtExt, StmtLike};
use swc_ecma_utils::{prepend_stmt, undefined, StmtExt, StmtLike};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use super::Optimizer;
@ -73,7 +73,7 @@ where
match &mut **alt_of_alt {
Stmt::Block(alt_of_alt) => {
prepend(
prepend_stmt(
&mut alt_of_alt.stmts,
Stmt::If(IfStmt {
span: *span_of_alt,
@ -420,7 +420,7 @@ where
if seq
.exprs
.last()
.map(|v| is_pure_undefined(v))
.map(|v| is_pure_undefined(&self.expr_ctx, v))
.unwrap_or(true) =>
{
let expr = self.ignore_return_value(&mut cur);

View File

@ -5,7 +5,7 @@ use swc_atoms::js_word;
use swc_common::{pass::Either, util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{
contains_arguments, contains_this_expr, find_ids, undefined, ExprFactory, Id,
contains_arguments, contains_this_expr, find_pat_ids, undefined, ExprFactory,
};
use swc_ecma_visit::VisitMutWith;
@ -704,7 +704,7 @@ where
for stmt in &body.stmts {
if let Stmt::Decl(Decl::Var(var)) = stmt {
for decl in &var.decls {
let ids: Vec<Id> = find_ids(&decl.name);
let ids: Vec<Id> = find_pat_ids(&decl.name);
remap.extend(ids.into_iter().map(|id| {
(

View File

@ -1,7 +1,7 @@
use swc_atoms::js_word;
use swc_common::{util::take::Take, Spanned};
use swc_ecma_ast::*;
use swc_ecma_utils::{find_ids, ExprExt, UsageFinder};
use swc_ecma_utils::{find_pat_ids, ExprExt, IdentUsageFinder};
use super::Optimizer;
use crate::{
@ -122,7 +122,7 @@ where
// No use => dropped
if usage.ref_count == 0 {
if init.may_have_side_effects() {
if init.may_have_side_effects(&self.expr_ctx) {
// TODO: Inline partially
return;
}
@ -259,7 +259,7 @@ where
// block_scoping pass.
// If the function captures the environment, we
// can't inline it.
let params: Vec<Id> = find_ids(&f.function.params);
let params: Vec<Id> = find_pat_ids(&f.function.params);
if !params.is_empty() {
let captured = idents_captured_by(&f.function.body);
@ -277,7 +277,7 @@ where
}
}
if init.may_have_side_effects() {
if init.may_have_side_effects(&self.expr_ctx) {
return;
}
@ -481,7 +481,7 @@ where
match &f.function.body {
Some(body) => {
if !UsageFinder::find(&i, body)
if !IdentUsageFinder::find(&i.to_id(), body)
&& self.is_fn_body_simple_enough_to_inline(
body,
f.function.params.len(),
@ -542,7 +542,7 @@ where
})
{
if let Decl::Class(ClassDecl { class, .. }) = decl {
if class_has_side_effect(class) {
if class_has_side_effect(&self.expr_ctx, class) {
return;
}
}

View File

@ -73,7 +73,7 @@ where
match stmt {
Stmt::While(w) => {
let (purity, val) = w.test.as_bool();
let (purity, val) = w.test.cast_to_bool(&self.expr_ctx);
if let Known(false) = val {
if purity.is_pure() {
let changed = UnreachableHandler::preserve_vars(stmt);
@ -94,7 +94,7 @@ where
}
Stmt::For(f) => {
if let Some(test) = &mut f.test {
let (purity, val) = test.as_bool();
let (purity, val) = test.cast_to_bool(&self.expr_ctx);
if let Known(false) = val {
let changed = UnreachableHandler::preserve_vars(&mut f.body);
self.changed |= changed;

View File

@ -9,8 +9,8 @@ use swc_common::{
};
use swc_ecma_ast::*;
use swc_ecma_utils::{
prepend_stmts, undefined, ExprExt, ExprFactory, Id, IsEmpty, ModuleItemLike, StmtLike, Type,
Value,
prepend_stmts, undefined, ExprCtx, ExprExt, ExprFactory, IsEmpty, ModuleItemLike, StmtLike,
Type, Value,
};
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith, VisitWith};
use tracing::{debug, span, Level};
@ -71,6 +71,10 @@ where
Optimizer {
marks,
expr_ctx: ExprCtx {
unresolved_ctxt: SyntaxContext::empty().apply_mark(marks.unresolved_mark),
is_unresolved_ref_safe: false,
},
changed: false,
options,
prepend_stmts: Default::default(),
@ -187,6 +191,7 @@ impl Ctx {
struct Optimizer<'a, M> {
marks: Marks,
expr_ctx: ExprCtx,
changed: bool,
options: &'a CompressOptions,
@ -1056,7 +1061,8 @@ where
if exprs.len() <= 1 {
return exprs.pop().map(|v| *v);
} else {
let is_last_undefined = is_pure_undefined(exprs.last().unwrap());
let is_last_undefined =
is_pure_undefined(&self.expr_ctx, exprs.last().unwrap());
// (foo(), void 0) => void foo()
if is_last_undefined {
@ -1202,8 +1208,8 @@ where
_ => return,
}
let lb = cond.cons.as_pure_bool();
let rb = cond.alt.as_pure_bool();
let lb = cond.cons.as_pure_bool(&self.expr_ctx);
let rb = cond.alt.as_pure_bool(&self.expr_ctx);
let lb = match lb {
Value::Known(v) => v,
@ -1523,7 +1529,7 @@ where
n.visit_mut_children_with(self);
if let Some(value) = &n.value {
if is_pure_undefined(value) {
if is_pure_undefined(&self.expr_ctx, value) {
n.value = None;
}
}
@ -2422,7 +2428,7 @@ where
debug_assert_eq!(self.prepend_stmts.len(), len);
if let Stmt::Expr(ExprStmt { expr, .. }) = s {
if is_pure_undefined(expr) {
if is_pure_undefined(&self.expr_ctx, expr) {
*s = Stmt::Empty(EmptyStmt { span: DUMMY_SP });
return;
}
@ -2443,8 +2449,9 @@ where
}
if self.options.unused {
let can_be_removed =
!is_directive && !expr.is_ident() && !expr.may_have_side_effects();
let can_be_removed = !is_directive
&& !expr.is_ident()
&& !expr.may_have_side_effects(&self.expr_ctx);
if can_be_removed {
self.changed = true;
@ -2678,7 +2685,7 @@ where
if n.kind == VarDeclKind::Let {
n.decls.iter_mut().for_each(|var| {
if let Some(e) = &var.init {
if is_pure_undefined(e) {
if is_pure_undefined(&self.expr_ctx, e) {
self.changed = true;
report_change!(
"Dropping explicit initializer which evaluates to `undefined`"
@ -2736,7 +2743,7 @@ where
for v in vars.iter_mut() {
if v.init
.as_deref()
.map(|e| !e.is_ident() && !e.may_have_side_effects())
.map(|e| !e.is_ident() && !e.may_have_side_effects(&self.expr_ctx))
.unwrap_or(true)
{
self.drop_unused_var_declarator(v, &mut None);
@ -2868,7 +2875,7 @@ where
if let Some(arg) = &mut n.arg {
self.compress_undefined(&mut **arg);
if !n.delegate && is_pure_undefined(arg) {
if !n.delegate && is_pure_undefined(&self.expr_ctx, arg) {
n.arg = None;
}
}

View File

@ -97,20 +97,20 @@ where
(lt, rt) if lt != rt => {}
(Type::Obj, Type::Obj) => {}
(Type::Num, Type::Num) => {
let l = n.left.as_number().opt()?;
let r = n.right.as_number().opt()?;
let l = n.left.as_pure_number(&self.expr_ctx).opt()?;
let r = n.right.as_pure_number(&self.expr_ctx).opt()?;
report_change!("Optimizing: literal comparison => num");
return make_lit_bool(l == r);
}
(Type::Str, Type::Str) => {
let l = &n.left.as_string().opt()?;
let r = &n.right.as_string().opt()?;
let l = &n.left.as_pure_string(&self.expr_ctx).opt()?;
let r = &n.right.as_pure_string(&self.expr_ctx).opt()?;
report_change!("Optimizing: literal comparison => str");
return make_lit_bool(l == r);
}
(_, _) => {
let l = n.left.as_pure_bool().opt()?;
let r = n.right.as_pure_bool().opt()?;
let l = n.left.as_pure_bool(&self.expr_ctx).opt()?;
let r = n.right.as_pure_bool(&self.expr_ctx).opt()?;
report_change!("Optimizing: literal comparison => bool");
return make_lit_bool(l == r);
}
@ -166,7 +166,7 @@ where
}
pub(super) fn negate(&mut self, e: &mut Expr, is_ret_val_ignored: bool) {
negate(e, self.ctx.in_bool_ctx, is_ret_val_ignored)
negate(&self.expr_ctx, e, self.ctx.in_bool_ctx, is_ret_val_ignored)
}
/// This method does
@ -282,7 +282,7 @@ where
match bin.op {
op!("&&") => {
let rb = bin.right.as_pure_bool();
let rb = bin.right.as_pure_bool(&self.expr_ctx);
let rb = match rb {
Value::Known(v) => v,
_ => return,
@ -302,7 +302,7 @@ where
}
}
op!("||") => {
let rb = bin.right.as_pure_bool();
let rb = bin.right.as_pure_bool(&self.expr_ctx);
let rb = match rb {
Value::Known(v) => v,
_ => return,

View File

@ -5,8 +5,8 @@ use swc_atoms::js_word;
use swc_common::{util::take::Take, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{
contains_arguments, contains_this_expr, prepend_stmts, undefined, ExprExt, Id, StmtLike,
UsageFinder,
contains_arguments, contains_this_expr, prepend_stmts, undefined, ExprExt, IdentUsageFinder,
StmtLike,
};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use tracing::{span, Level};
@ -439,7 +439,7 @@ where
if right.exprs.len() >= 2
&& right.exprs[..right.exprs.len() - 1]
.iter()
.all(|e| !e.may_have_side_effects())
.all(|e| !e.may_have_side_effects(&self.expr_ctx))
{
return true;
}
@ -464,7 +464,7 @@ where
Expr::Seq(mut right)
if right.exprs[..right.exprs.len() - 1]
.iter()
.all(|e| !e.may_have_side_effects()) =>
.all(|e| !e.may_have_side_effects(&self.expr_ctx)) =>
{
new_exprs.extend(right.exprs.drain(..right.exprs.len() - 1));
new_exprs.push(Box::new(Expr::Assign(AssignExpr {
@ -549,7 +549,7 @@ where
}
if let Some(last) = e.exprs.last() {
if is_pure_undefined(last) {
if is_pure_undefined(&self.expr_ctx, last) {
self.changed = true;
report_change!("sequences: Shifting void");
@ -781,7 +781,7 @@ where
match bv.init.as_deref_mut() {
Some(b_init) => {
if UsageFinder::find(&an.id, b_init) {
if IdentUsageFinder::find(&an.to_id(), b_init) {
log_abort!(
"We can't duplicated binding because initializer \
uses the previous declaration of the variable"
@ -1116,7 +1116,7 @@ where
_ => {}
}
if !e.may_have_side_effects() {
if !e.may_have_side_effects(&self.expr_ctx) {
return true;
}
@ -1248,7 +1248,7 @@ where
return Ok(true);
}
if obj.may_have_side_effects() {
if obj.may_have_side_effects(&self.expr_ctx) {
return Ok(false);
}
@ -1322,7 +1322,7 @@ where
return Ok(false);
}
if UsageFinder::find(&b_left, &b.right) {
if IdentUsageFinder::find(&b_left.to_id(), &b.right) {
return Err(());
}
@ -1700,7 +1700,15 @@ where
}
};
if a_right.is_this() || a_right.is_ident_ref_to(js_word!("arguments")) {
if a_right.is_this()
|| matches!(
&**a_right,
Expr::Ident(Ident {
sym: js_word!("arguments"),
..
})
)
{
return Ok(false);
}
if contains_arguments(&**a_right) {

View File

@ -21,7 +21,10 @@ where
..
}) = e
{
if args.iter().any(|arg| arg.expr.may_have_side_effects()) {
if args
.iter()
.any(|arg| arg.expr.may_have_side_effects(&self.expr_ctx))
{
return;
}
@ -67,7 +70,7 @@ where
_ => {}
}
let value = n.as_string();
let value = n.as_pure_string(&self.expr_ctx);
if let Known(value) = value {
let span = n.span();

View File

@ -1,6 +1,6 @@
use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{prepend, ExprExt, ExprFactory, StmtExt};
use swc_ecma_utils::{prepend_stmt, ExprExt, ExprFactory, StmtExt};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
use super::Optimizer;
@ -33,7 +33,7 @@ where
let discriminant = &mut stmt.discriminant;
let tail = if let Some(e) = is_primitive(tail_expr(discriminant)) {
let tail = if let Some(e) = is_primitive(&self.expr_ctx, tail_expr(discriminant)) {
e
} else {
return;
@ -46,7 +46,7 @@ where
for (idx, case) in stmt.cases.iter_mut().enumerate() {
if let Some(test) = case.test.as_ref() {
if let Some(e) = is_primitive(tail_expr(test)) {
if let Some(e) = is_primitive(&self.expr_ctx, tail_expr(test)) {
if e.eq_ignore_span(tail) {
cases.push(case.take());
@ -94,7 +94,7 @@ where
report_change!("switches: Turn exact match into default");
// so that following pass could turn it into if else
if let Some(test) = last.test.take() {
prepend(&mut last.cons, test.into_stmt())
prepend_stmt(&mut last.cons, test.into_stmt())
}
}
}
@ -199,7 +199,7 @@ where
let has_side_effect = cases.iter().skip(last).rposition(|case| {
case.test
.as_deref()
.map(|test| test.may_have_side_effects())
.map(|test| test.may_have_side_effects(&self.expr_ctx))
.unwrap_or(false)
});
@ -230,7 +230,7 @@ where
let start = cases.iter().enumerate().rposition(|(idx, case)| {
case.test
.as_deref()
.map(|test| test.may_have_side_effects())
.map(|test| test.may_have_side_effects(&self.expr_ctx))
.unwrap_or(false)
|| (idx != end && !case.cons.is_empty())
});
@ -272,7 +272,7 @@ where
cannot_cross_block |= cases[j]
.test
.as_deref()
.map(|test| is_primitive(test).is_none())
.map(|test| is_primitive(&self.expr_ctx, test).is_none())
.unwrap_or(false)
|| !(cases[j].cons.is_empty()
|| cases[j].cons.terminates()

View File

@ -472,7 +472,7 @@ where
}
if let Decl::Class(c) = decl {
if class_has_side_effect(&c.class) {
if class_has_side_effect(&self.expr_ctx, &c.class) {
return;
}
}
@ -754,7 +754,7 @@ where
return;
}
if contains_ident_ref(&f.function.body, f.ident.as_ref().unwrap()) {
if contains_ident_ref(&f.function.body, &f.ident.as_ref().unwrap().to_id()) {
return;
}

Some files were not shown because too many files have changed in this diff Show More