Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/resolver): Use different syntax context for unresolved refs #4436

Merged
merged 95 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
eb44bc0
Rename
kdy1 Apr 25, 2022
0ed5114
Config
kdy1 Apr 25, 2022
c61ccad
fixup
kdy1 Apr 25, 2022
4d7b862
fixup
kdy1 Apr 25, 2022
ca139ec
Oh
kdy1 Apr 25, 2022
7318a60
Add tests
kdy1 Apr 25, 2022
050f3fe
Update test refs
kdy1 Apr 25, 2022
96b8e5a
Update test refs
kdy1 Apr 25, 2022
b86e3b5
Update test refs
kdy1 Apr 26, 2022
6478681
Update test refs
kdy1 Apr 26, 2022
285878f
Update test refs (Will check using github)
kdy1 Apr 26, 2022
d9139a1
We need two arg
kdy1 Apr 26, 2022
6170f3b
Refine api
kdy1 Apr 26, 2022
d77dd5b
`Mark::new`
kdy1 Apr 26, 2022
acda962
Fix some tests
kdy1 Apr 26, 2022
be1af72
Fix `swc_ecma_transforms_base`
kdy1 Apr 26, 2022
f5127ce
Fix minifier
kdy1 Apr 26, 2022
b4aee6e
Fix `swc_ecma_transforms_compat`
kdy1 Apr 26, 2022
6c796f5
Fix `swc_ecma_transforms_typescript`
kdy1 Apr 26, 2022
cc754c4
fixup
kdy1 Apr 26, 2022
4e79716
Add a test
kdy1 Apr 26, 2022
5cd830a
Fix `export default Foo`
kdy1 Apr 26, 2022
606c00d
`try_resolving_as_type`
kdy1 Apr 26, 2022
b209a52
Export named
kdy1 Apr 26, 2022
1acb09e
Handle imports
kdy1 Apr 26, 2022
0701db7
Fix api
kdy1 Apr 26, 2022
b692289
Fix `swc_webpack_ast`
kdy1 Apr 26, 2022
c926ec0
Fix `swc_bundler`
kdy1 Apr 26, 2022
33246c8
Doc
kdy1 Apr 26, 2022
f446e86
Fix compiliation of `swc_ecma_minifier`
kdy1 Apr 26, 2022
014d848
Rename
kdy1 Apr 26, 2022
48e21cd
Rename
kdy1 Apr 26, 2022
e31226e
Rename
kdy1 Apr 26, 2022
678deca
Rename
kdy1 Apr 26, 2022
c05c79e
Fix some
kdy1 Apr 26, 2022
0dd7d95
Rename
kdy1 Apr 26, 2022
d7ab74c
Rename & Doc
kdy1 Apr 26, 2022
aa8b8a1
Some fix for `swc_ecma_transforms_module`
kdy1 Apr 26, 2022
5889169
Rename
kdy1 Apr 26, 2022
640b97f
More fix
kdy1 Apr 26, 2022
e2dc750
Fix
kdy1 Apr 26, 2022
0c9b5e1
Fix system js
kdy1 Apr 26, 2022
a38c9a5
More fix for opt
kdy1 Apr 26, 2022
3ed0628
WIP: Fixing `swc_ecma_lints`
kdy1 Apr 26, 2022
8e2f617
Fix compilation of `swc_ecma_lints`
kdy1 Apr 26, 2022
b51abc7
More work
kdy1 Apr 26, 2022
9ccf4be
fixup
kdy1 Apr 26, 2022
8f23d3d
fixup
kdy1 Apr 26, 2022
85db77a
fixup
kdy1 Apr 26, 2022
b5d0c16
fixup
kdy1 Apr 26, 2022
647686b
fixup
kdy1 Apr 26, 2022
fbf0341
Fix
kdy1 Apr 26, 2022
376fb52
Rename
kdy1 Apr 26, 2022
e24cde8
fixup
kdy1 Apr 26, 2022
cac4d5e
fixup
kdy1 Apr 26, 2022
1319dc6
Doc
kdy1 Apr 26, 2022
932700a
Fix some
kdy1 Apr 26, 2022
1cc895c
fixup
kdy1 Apr 26, 2022
141c939
fixup
kdy1 Apr 26, 2022
6fe16f7
fixup
kdy1 Apr 26, 2022
38ea777
fixup
kdy1 Apr 26, 2022
528a518
Fix `swc_ecma_transforms`
kdy1 Apr 26, 2022
8ad07e3
Fix `swc_webpack_sat`
kdy1 Apr 26, 2022
7c4a8ff
Fix `swc`
kdy1 Apr 26, 2022
6e63d75
More fix
kdy1 Apr 26, 2022
fd5afae
Update test refs
kdy1 Apr 26, 2022
3945b32
Doc
kdy1 Apr 26, 2022
9c37c5a
Doc
kdy1 Apr 26, 2022
f92e173
WIP: Fix minifier
kdy1 Apr 26, 2022
c5f86cd
Fix
kdy1 Apr 26, 2022
f1cf17d
Done
kdy1 Apr 26, 2022
1b34351
This is not required anymore
kdy1 Apr 26, 2022
a600441
log
kdy1 Apr 26, 2022
bf601af
Snapshots
kdy1 Apr 26, 2022
7a5bae3
Update test refs
kdy1 Apr 26, 2022
f103aef
Fix global defs
kdy1 Apr 26, 2022
50861ff
Fix
kdy1 Apr 26, 2022
5d9a78f
Fix `swc_bundler`
kdy1 Apr 26, 2022
ad92868
Fix one
kdy1 Apr 26, 2022
16e6179
fixup
kdy1 Apr 26, 2022
03a1b89
WIP
kdy1 Apr 26, 2022
2af5677
More fix
kdy1 Apr 26, 2022
e05ac42
More fix
kdy1 Apr 26, 2022
52c5bfa
fixup
kdy1 Apr 26, 2022
863f819
More
kdy1 Apr 26, 2022
40242ad
fixup
kdy1 Apr 26, 2022
b06fe3e
Fix
kdy1 Apr 26, 2022
afeb880
Fix react
kdy1 Apr 26, 2022
aa4c89f
Sort methods
kdy1 Apr 26, 2022
81e5fd1
Add a test
kdy1 Apr 26, 2022
e249148
Fix resolving of enum
kdy1 Apr 26, 2022
d02d1c0
Update test refs
kdy1 Apr 26, 2022
103dc27
Update test refs
kdy1 Apr 26, 2022
01c5aa2
Fix resolver
kdy1 Apr 26, 2022
f42928d
Merge branch 'main' into resolver
swc-bot Apr 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 7 additions & 5 deletions crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use swc::config::{Config, IsModule, JscConfig, Options, SourceMapsConfig};
use swc_common::{errors::Handler, FileName, FilePathMapping, Mark, SourceFile, SourceMap};
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_parser::{Syntax, TsConfig};
use swc_ecma_transforms::{fixer, hygiene, resolver, resolver_with_mark, typescript};
use swc_ecma_transforms::{fixer, hygiene, resolver, typescript};
use swc_ecma_visit::FoldWith;

static SOURCE: &str = include_str!("assets/Observable.ts");
Expand Down Expand Up @@ -45,10 +45,12 @@ fn parse(c: &swc::Compiler) -> (Arc<SourceFile>, Program) {

fn as_es(c: &swc::Compiler) -> Program {
let program = parse(c).1;
let mark = Mark::fresh(Mark::root());
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

program
.fold_with(&mut resolver_with_mark(mark))
.fold_with(&mut typescript::strip(mark))
.fold_with(&mut resolver(unresolved_mark, top_level_mark, true))
.fold_with(&mut typescript::strip(top_level_mark))
}

fn base_tr_group(c: &mut Criterion) {
Expand Down Expand Up @@ -83,7 +85,7 @@ fn base_tr_resolver_and_hygiene(b: &mut Bencher) {
black_box(c.run_transform(&handler, true, || {
module
.clone()
.fold_with(&mut resolver())
.fold_with(&mut resolver(Mark::new(), Mark::new(), false))
.fold_with(&mut hygiene())
}))
});
Expand Down
12 changes: 11 additions & 1 deletion crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> {
handler: &'b Handler,
env: Option<swc_ecma_preset_env::Config>,
pass: P,
/// [Mark] for top level bindings and unresolved identifier references.
/// [Mark] for top level bindings .
top_level_mark: Mark,

/// [Mark] for unresolved refernces.
unresolved_mark: Mark,

target: EsVersion,
loose: bool,
assumptions: Assumptions,
Expand All @@ -50,6 +54,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
loose: bool,
assumptions: Assumptions,
top_level_mark: Mark,
unresolved_mark: Mark,
pass: P,
) -> Self {
PassBuilder {
Expand All @@ -58,6 +63,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
env: None,
pass,
top_level_mark,
unresolved_mark,
target: EsVersion::Es5,
loose,
assumptions,
Expand All @@ -80,6 +86,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
env: self.env,
pass,
top_level_mark: self.top_level_mark,
unresolved_mark: self.unresolved_mark,
target: self.target,
loose: self.loose,
assumptions: self.assumptions,
Expand Down Expand Up @@ -318,6 +325,7 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
options: self.minify,
cm: self.cm.clone(),
comments: comments.cloned(),
unresolved_mark: self.unresolved_mark,
top_level_mark: self.top_level_mark,
}),
Optional::new(
Expand All @@ -333,6 +341,7 @@ struct MinifierPass {
options: Option<JsMinifyOptions>,
cm: Lrc<SourceMap>,
comments: Option<SingleThreadedComments>,
unresolved_mark: Mark,
top_level_mark: Mark,
}

Expand Down Expand Up @@ -368,6 +377,7 @@ impl VisitMut for MinifierPass {
None,
&opts,
&swc_ecma_minifier::option::ExtraOptions {
unresolved_mark: self.unresolved_mark,
top_level_mark: self.top_level_mark,
},
)
Expand Down
71 changes: 40 additions & 31 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ use swc_ecma_transforms::{
optimization::{const_modules, json_parse, simplifier},
pass::{noop, Optional},
proposals::{decorators, export_default_from, import_assertions},
react,
resolver::ts_resolver,
resolver_with_mark,
react, resolver,
typescript::{self, TSEnumConfig},
Assumptions,
};
Expand Down Expand Up @@ -166,7 +164,7 @@ pub struct Options {
pub disable_fixer: bool,

#[serde(skip_deserializing, default)]
pub global_mark: Option<Mark>,
pub top_level_mark: Option<Mark>,

#[cfg(not(target_arch = "wasm32"))]
#[serde(default = "default_cwd")]
Expand Down Expand Up @@ -310,9 +308,8 @@ impl Options {
}
});

let top_level_mark = self
.global_mark
.unwrap_or_else(|| Mark::fresh(Mark::root()));
let unresolved_mark = Mark::new();
let top_level_mark = self.top_level_mark.unwrap_or_else(Mark::new);

let es_version = target.unwrap_or_default();

Expand All @@ -329,12 +326,14 @@ impl Options {
if syntax.typescript() {
assumptions.set_class_methods = !transform.use_define_for_class_fields;
assumptions.set_public_class_fields = !transform.use_define_for_class_fields;

program.visit_mut_with(&mut ts_resolver(top_level_mark));
} else {
program.visit_mut_with(&mut resolver_with_mark(top_level_mark));
}

program.visit_mut_with(&mut resolver(
unresolved_mark,
top_level_mark,
syntax.typescript(),
));

if program.is_module() {
js_minify = js_minify.map(|c| {
let compress = c
Expand Down Expand Up @@ -408,6 +407,7 @@ impl Options {
}
};

let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark);
let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark);

let pass = chain!(
Expand All @@ -421,26 +421,34 @@ impl Options {
json_parse_pass
);

let pass = PassBuilder::new(cm, handler, loose, assumptions, top_level_mark, pass)
.target(es_version)
.skip_helper_injection(self.skip_helper_injection)
.minify(js_minify)
.hygiene(if self.disable_hygiene {
None
} else {
Some(hygiene::Config { keep_class_names })
})
.fixer(!self.disable_fixer)
.preset_env(config.env)
.regenerator(regenerator)
.finalize(
base_url,
paths.into_iter().collect(),
base,
syntax,
config.module,
comments,
);
let pass = PassBuilder::new(
cm,
handler,
loose,
assumptions,
top_level_mark,
unresolved_mark,
pass,
)
.target(es_version)
.skip_helper_injection(self.skip_helper_injection)
.minify(js_minify)
.hygiene(if self.disable_hygiene {
None
} else {
Some(hygiene::Config { keep_class_names })
})
.fixer(!self.disable_fixer)
.preset_env(config.env)
.regenerator(regenerator)
.finalize(
base_url,
paths.into_iter().collect(),
base,
syntax,
config.module,
comments,
);

let keep_import_assertions = experimental.keep_import_assertions;

Expand Down Expand Up @@ -511,6 +519,7 @@ impl Options {
program: &program,
lint_config: &lints,
top_level_ctxt,
unresolved_ctxt,
es_version,
source_map: cm.clone(),
})),
Expand Down
13 changes: 9 additions & 4 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ use swc_ecma_transforms::{
hygiene,
modules::path::NodeImportResolver,
pass::noop,
resolver_with_mark,
resolver,
};
use swc_ecma_visit::{noop_visit_type, FoldWith, Visit, VisitMutWith, VisitWith};
pub use swc_error_reporters::handler::{try_with_handler, HandlerOpts};
Expand Down Expand Up @@ -1037,20 +1037,25 @@ impl Compiler {
Default::default()
};

let top_level_mark = Mark::fresh(Mark::root());
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

let is_mangler_enabled = min_opts.mangle.is_some();

let module = self.run_transform(handler, false, || {
let module = module.fold_with(&mut resolver_with_mark(top_level_mark));
let module =
module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false));

let mut module = swc_ecma_minifier::optimize(
module,
self.cm.clone(),
Some(&comments),
None,
&min_opts,
&swc_ecma_minifier::option::ExtraOptions { top_level_mark },
&swc_ecma_minifier::option::ExtraOptions {
unresolved_mark,
top_level_mark,
},
);

if !is_mangler_enabled {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/rust_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn shopify_2_same_opt() {
skip_helper_injection: false,
disable_hygiene: false,
disable_fixer: false,
global_mark: None,
top_level_mark: None,
cwd: "/Users/kdy1/projects/example-swcify".into(),
filename: "/Users/kdy1/projects/example-swcify/src/App/App.tsx".into(),
env_name: "development".into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
M;
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
M;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x, x.y, new (x());
x.y, new (x());
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x, x.y, new (x());
x.y, new (x());
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ var B;
}
B1.D = D;
})(B || (B = {}));
var A;
(function(A1) {
var A1;
(function(A) {
class C {
}
A1.C = C;
A.C = C;
var b = B;
A1.b = b;
})(A || (A = {}));
A.b = b;
})(A1 || (A1 = {}));
var c;
var c = new B.a.C();
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var B, A;
var B, A1;
!function(B1) {
var a = A;
B1.a = a;
class D extends a.C {
}
B1.D = D;
}(B || (B = {})), function(A1) {
A1.C = class {
}(B || (B = {})), function(A) {
A.C = class {
};
var b = B;
A1.b = b;
}(A || (A = {})), new B.a.C();
A.b = b;
}(A1 || (A1 = {})), new B.a.C();
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ var B;
}(a.C);
B1.D = D;
})(B || (B = {}));
var A;
(function(A1) {
var A1;
(function(A) {
var C = function C() {
"use strict";
swcHelpers.classCallCheck(this, C);
};
A1.C = C;
A.C = C;
var b = B;
A1.b = b;
})(A || (A = {}));
A.b = b;
})(A1 || (A1 = {}));
var c;
var c = new B.a.C();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var B, A;
var B, A1;
import * as swcHelpers from "@swc/helpers";
!function(B1) {
var a = A;
Expand All @@ -13,12 +13,12 @@ import * as swcHelpers from "@swc/helpers";
return D;
}(a.C);
B1.D = D;
}(B || (B = {})), function(A1) {
}(B || (B = {})), function(A) {
var C = function() {
"use strict";
swcHelpers.classCallCheck(this, C);
};
A1.C = C;
A.C = C;
var b = B;
A1.b = b;
}(A || (A = {})), new B.a.C();
A.b = b;
}(A1 || (A1 = {})), new B.a.C();
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ var M;
}
M1.D = D;
})(M || (M = {}));
var N;
(function(N1) {
var N1;
(function(N) {
class E extends M.D {
}
N1.E = E;
})(N || (N = {}));
N.E = E;
})(N1 || (N1 = {}));
var O;
(function(O) {
class C2 extends Q.E2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var M, N, O;
var M, N1, O;
class C extends N.E {
}
(M || (M = {})).D = class extends C {
}, function(N1) {
}, function(N) {
class E extends M.D {
}
N1.E = E;
}(N || (N = {})), function(O) {
N.E = E;
}(N1 || (N1 = {})), function(O) {
class C2 extends Q.E2 {
}
let P;
Expand Down
Loading