Skip to content

Commit

Permalink
fix(ecmascript/transforms): Fix dce (#1301)
Browse files Browse the repository at this point in the history
swc_bunder:
 - Fix `keywords` pass.

swc_ecma_codegen:
 - Ensure that the code generator handles unicode characters properly. (denoland/deno#8925)

swc_ecma_parser:
 - Ensure that the parser handles unicode characters properly. (denoland/deno#8925)

swc_ecma_transforms:
 - Fix dce.
  • Loading branch information
kdy1 authored Jan 5, 2021
1 parent 5d88e8b commit 842b6f9
Show file tree
Hide file tree
Showing 19 changed files with 495 additions and 62 deletions.
4 changes: 2 additions & 2 deletions bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_bundler"
repository = "https://github.com/swc-project/swc.git"
version = "0.19.0"
version = "0.19.1"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
Expand Down Expand Up @@ -42,7 +42,7 @@ hex = "0.4"
ntest = "0.7.2"
reqwest = {version = "0.10.8", features = ["blocking"]}
sha-1 = "0.9"
swc_ecma_transforms = {version = "0.32.0", path = "../ecmascript/transforms", features = ["react"]}
swc_ecma_transforms = {version = "0.32.0", path = "../ecmascript/transforms", features = ["react", "typescript"]}
tempfile = "3.1.0"
testing = {version = "0.10.0", path = "../testing"}
url = "2.1.1"
Expand Down
5 changes: 3 additions & 2 deletions bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::plan::{DepType, Plan};
use super::plan::DepType;
use super::plan::Plan;
use crate::bundler::chunk::export::inject_export;
use crate::bundler::keywords::KeywordRenamer;
use crate::{
Expand Down Expand Up @@ -1061,7 +1062,7 @@ where
ModuleDecl::ExportDefaultExpr(mut export) => {
vars.push(
VarDeclarator {
span: DUMMY_SP.with_ctxt(injected_ctxt),
span: DUMMY_SP,
name: Pat::Ident(Ident::new(
js_word!("default"),
DUMMY_SP.with_ctxt(info.export_ctxt()),
Expand Down
18 changes: 18 additions & 0 deletions bundler/src/bundler/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,22 @@ impl VisitMut for KeywordRenamer {
n.orig = renamed;
}
}

fn visit_mut_class_prop(&mut self, n: &mut ClassProp) {
if n.computed {
n.key.visit_mut_with(self);
}

n.decorators.visit_mut_with(self);
n.value.visit_mut_with(self);
}

fn visit_mut_private_prop(&mut self, n: &mut PrivateProp) {
if n.computed {
n.key.visit_mut_with(self);
}

n.decorators.visit_mut_with(self);
n.value.visit_mut_with(self);
}
}
6 changes: 6 additions & 0 deletions bundler/tests/deno-exec/.deno-8970/case1/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from "https://esm.sh/react@17.0.1"

console.log(React)
if (!React) {
throw new Error()
}
6 changes: 6 additions & 0 deletions bundler/tests/deno-exec/.deno-8970/case2/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from "https://esm.sh/react@17.0.1"

console.log(React)
if (!React) {
throw new Error()
}
1 change: 1 addition & 0 deletions bundler/tests/deno-exec/deno-8943/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as flags from 'https://deno.land/std@0.83.0/flags/mod.ts';
3 changes: 3 additions & 0 deletions bundler/tests/deno-exec/deno-8943/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { flags } from './deps.ts';

console.log(flags.parse(Deno.args));
1 change: 1 addition & 0 deletions bundler/tests/deno-exec/deno-8952/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'https://deno.land/std@0.82.0/flags/mod.ts';
1 change: 1 addition & 0 deletions bundler/tests/deno-exec/deno-8985/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'https://deno.land/std@0.83.0/log/mod.ts';
8 changes: 8 additions & 0 deletions bundler/tests/deno-exec/deno-8988/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class T {
private throws: number[] = [];
m(): void {
console.log(this.throws.length);
}
}

new T().m();
14 changes: 5 additions & 9 deletions bundler/tests/fixture/deno-8734/output/entry.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
const m = "test";
if (!m) {
throw new Error('b');
}
export class Comparator {
constructor(comp1, optionsOrLoose = {
}){
}
parse(comp) {
const m1 = "another";
if (!m1) {
const m = "another";
if (!m) {
throw new TypeError("Invalid comparator: " + comp);
}
const m11 = m1[1];
console.log(m11);
if (!m1[2]) {
const m1 = m[1];
console.log(m1);
if (!m[2]) {
console.log('other');
}
}
Expand Down
1 change: 0 additions & 1 deletion bundler/tests/fixture/wrapped/export-named/output/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const [a, b, c] = [
3
];
const b1 = b;
const b2 = b1;
const mod = function() {
return {
b: b1
Expand Down
15 changes: 10 additions & 5 deletions ecmascript/codegen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,8 @@ fn dneo_8541_1() {
}

#[test]
fn denoo_8541_2() {
test_from_to(
"React.createElement('span', null, '\\u00b7');",
"React.createElement('span', null, '\\u00b7');",
);
fn deno_8925() {
assert_pretty("const 𝒫 = 2;", "const 𝒫 = 2;");
}

#[test]
Expand Down Expand Up @@ -533,6 +530,14 @@ fn test_escape_without_source() {
es2020("\u{10ffff}", "\\u{10ffff}");
}

#[test]
fn deno_8541_2() {
test_from_to(
"React.createElement('span', null, '\\u00b7');",
"React.createElement('span', null, '\\u00b7');",
);
}

#[derive(Debug, Clone)]
struct Buf(Arc<RwLock<Vec<u8>>>);
impl Write for Buf {
Expand Down
2 changes: 2 additions & 0 deletions ecmascript/parser/tests/typescript/deno-8925/case1/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const 𝒫 = 2;
console.log(𝒫)
116 changes: 116 additions & 0 deletions ecmascript/parser/tests/typescript/deno-8925/case1/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 33,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 15,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 14,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 10,
"ctxt": 0
},
"value": "𝒫",
"typeAnnotation": null,
"optional": false
},
"init": {
"type": "NumericLiteral",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"value": 2.0
},
"definite": false
}
]
},
{
"type": "ExpressionStatement",
"span": {
"start": 16,
"end": 33,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 16,
"end": 33,
"ctxt": 0
},
"callee": {
"type": "MemberExpression",
"span": {
"start": 16,
"end": 27,
"ctxt": 0
},
"object": {
"type": "Identifier",
"span": {
"start": 16,
"end": 23,
"ctxt": 0
},
"value": "console",
"typeAnnotation": null,
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 24,
"end": 27,
"ctxt": 0
},
"value": "log",
"typeAnnotation": null,
"optional": false
},
"computed": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "Identifier",
"span": {
"start": 28,
"end": 32,
"ctxt": 0
},
"value": "𝒫",
"typeAnnotation": null,
"optional": false
}
}
],
"typeArguments": null
}
}
],
"interpreter": null
}
2 changes: 2 additions & 0 deletions ecmascript/parser/tests/typescript/deno-8925/case2/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const 𝒫 = 2;
console.log(𝒫)
Loading

0 comments on commit 842b6f9

Please sign in to comment.