Skip to content

Commit

Permalink
fix(bundler): Fix bugs (#1346)
Browse files Browse the repository at this point in the history
swc_bundler:
 - Fix keywords pass.

swc_ecma_transforms_optimization:
 - Remove `dbg!`.
 - `constant_propagation`: Inline only injected variables.
  • Loading branch information
kdy1 authored Jan 22, 2021
1 parent 0469e3a commit 6a1c3da
Show file tree
Hide file tree
Showing 36 changed files with 2,896 additions and 36 deletions.
58 changes: 51 additions & 7 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ on: [push, pull_request]

env:
CARGO_INCREMENTAL: 0
# To make spack tests reliable
RAYON_NUM_THREADS: 1
CI: "1"

jobs:
Expand All @@ -29,9 +27,59 @@ jobs:
- name: Run cargo fmt
run: cargo fmt --all -- --check

check-all:
name: Compilability
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Ensure that all components all compilable.
- name: Run cargo check for all targets
run: cargo check --color always --all --all-targets

test:
name: test
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
crate:
- ast_node
- enum_kind
- from_variant
- jsdoc
- spack
- string_enum
- swc
- swc_atoms
- swc_bundler
- swc_common
- swc_ecma_ast
- swc_ecma_codegen
- swc_ecma_codegen_macros
- swc_ecma_dep_graph
- swc_ecma_ext_transforms
- swc_ecma_parser
- swc_ecma_preset_env
- swc_ecma_transforms
- swc_ecma_transforms_base
- swc_ecma_transforms_compat
- swc_ecma_transforms_macros
- swc_ecma_transforms_module
- swc_ecma_transforms_optimization
- swc_ecma_transforms_proposal
- swc_ecma_transforms_react
- swc_ecma_transforms_testing
- swc_ecma_transforms_typescript
- swc_ecma_utils
- swc_ecma_visit
- swc_ecmascript
- swc_eq_ignore_macros
- swc_macros_common
- swc_visit
- swc_visit_macros
- testing
- testing_macros

steps:
- uses: actions/checkout@v2

Expand Down Expand Up @@ -60,14 +108,10 @@ jobs:
**/target/
key: ${{ runner.os }}-cargo-test

# Ensure that all components all compilable.
- name: Run cargo check for all targets
run: cargo check --color always --all --all-targets

- name: Run cargo test
run: |
export PATH="$PATH:$HOME/npm/bin"
cargo test --color always --all --exclude node --exclude wasm
cargo test --color always -p ${{ matrix.crate }}
#
deploy-docs:
runs-on: ubuntu-latest
Expand Down
12 changes: 12 additions & 0 deletions bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ where
// );
}

// print_hygiene(
// &format!("processed: {}", info.fm.name),
// &self.cm,
// &module.clone().into(),
// );

if is_entry {
self.replace_import_specifiers(&info, &mut module);
self.finalize_merging_of_entry(ctx, &mut module);
Expand Down Expand Up @@ -1043,6 +1049,12 @@ where

new
});

// print_hygiene(
// &format!("prepared: {}", info.fm.name),
// &self.cm,
// &module.clone().into(),
// );
}

pub(super) fn replace_import_specifiers(&self, info: &TransformedModule, module: &mut Modules) {
Expand Down
33 changes: 29 additions & 4 deletions bundler/src/bundler/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::id::Id;
use crate::util::MapWithMut;
use std::collections::HashMap;
use swc_atoms::js_word;
use swc_ecma_ast::*;
Expand Down Expand Up @@ -65,11 +66,35 @@ impl VisitMut for KeywordRenamer {
}
}

fn visit_mut_assign_pat_prop(&mut self, n: &mut AssignPatProp) {
if let Some(renamed) = self.renamed(&n.key) {
n.key = renamed;
fn visit_mut_object_pat_prop(&mut self, n: &mut ObjectPatProp) {
n.visit_mut_children_with(self);

match n {
ObjectPatProp::Assign(pat) => {
if let Some(renamed) = self.renamed(&pat.key) {
match &mut pat.value {
Some(default) => {
*n = ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(pat.key.take()),
value: Box::new(Pat::Assign(AssignPat {
span: pat.span,
left: Box::new(Pat::Ident(renamed)),
right: default.take(),
type_ann: None,
})),
});
}
None => {
*n = ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(pat.key.take()),
value: Box::new(Pat::Ident(renamed)),
})
}
}
}
}
_ => {}
}
n.value.visit_mut_with(self);
}

fn visit_mut_pat(&mut self, n: &mut Pat) {
Expand Down
3 changes: 3 additions & 0 deletions bundler/tests/deno-exec/.deno-9200/case1/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { isEmail } from "https://deno.land/x/segno@v1.1.0/mod.ts";

isEmail("test@test.com");
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const a1 = 'a';
const a2 = 'a';
const c1 = 'c';
const a1 = a2;
export { a1 as a };
export { c1 as c };
3 changes: 2 additions & 1 deletion bundler/tests/fixture/deno-8978/output/entry.inlined.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const f = ()=>"hello world"
;
export { f as any };
const any1 = f;
export { any1 as any };
9 changes: 9 additions & 0 deletions bundler/tests/fixture/deno-9055/keywords-1/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
}

const { n, t, int } = x

console.log(n, t, int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
};
const { n , t , int: __int } = x;
console.log(n, t, __int);
7 changes: 7 additions & 0 deletions bundler/tests/fixture/deno-9055/keywords-1/output/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
};
const { n , t , int: __int } = x;
console.log(n, t, __int);
9 changes: 9 additions & 0 deletions bundler/tests/fixture/deno-9055/keywords-2/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
}

const { n, t, int = 5 } = x

console.log(n, t, int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
};
const { n , t , int: __int = 5 } = x;
console.log(n, t, __int);
7 changes: 7 additions & 0 deletions bundler/tests/fixture/deno-9055/keywords-2/output/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = {
n: 123,
t: 'text',
int: '==INT=='
};
const { n , t , int: __int = 5 } = x;
console.log(n, t, __int);
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class MyError extends Error {
super("I'm in?");
}
}
function example1() {
function example2() {
throw new MyError();
}
const example1 = example2;
export { example1 as example };
7 changes: 7 additions & 0 deletions bundler/tests/fixture/deno-9200/.case1/input/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as lib from './lib';

// exporting all functions
export * from './lib';

// exporting functions namespaced to segno
export { lib };
2 changes: 2 additions & 0 deletions bundler/tests/fixture/deno-9200/.case1/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { foo } from './deps'
foo()
1 change: 1 addition & 0 deletions bundler/tests/fixture/deno-9200/.case1/input/lib-impl1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function foo() { }
1 change: 1 addition & 0 deletions bundler/tests/fixture/deno-9200/.case1/input/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib-impl1'
7 changes: 7 additions & 0 deletions bundler/tests/fixture/deno-9200/.case2/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as lib from './lib';

// exporting all functions
export * from './lib';

// exporting functions namespaced to segno
export { lib };
1 change: 1 addition & 0 deletions bundler/tests/fixture/deno-9200/.case2/input/lib-impl1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function foo() { }
1 change: 1 addition & 0 deletions bundler/tests/fixture/deno-9200/.case2/input/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib-impl1'
6 changes: 6 additions & 0 deletions bundler/tests/fixture/deno-9212/.case1/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as ReactDom from "https://esm.sh/react-dom@17.0.1"
import * as React from "https://esm.sh/react@17.0.1"

const { document } = window as any

ReactDom.render(React.createElement('p', null, 'hello world!'), document.body)
21 changes: 21 additions & 0 deletions bundler/tests/fixture/deno-9219/case1/input/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { parse } from "https://deno.land/std@0.84.0/flags/mod.ts";

const args = parse(Deno.args, {
boolean: [
"help",
"verbose",
],
alias: {
help: "h",
verbose: "v",
},
default: {
verbose: false,
},
}) as {
_: string[];
help: boolean;
verbose: boolean;
};

console.dir(args);
Loading

0 comments on commit 6a1c3da

Please sign in to comment.