Skip to content

Commit

Permalink
fix(webpack/ast): Adjust acorn options. (swc-project#2908)
Browse files Browse the repository at this point in the history
swc_babel_ast:
 - Fix serialization of class properties.
 - Adjust `acorn` options.
  • Loading branch information
kdy1 committed Nov 29, 2021
1 parent edc0cb0 commit 586ab0c
Show file tree
Hide file tree
Showing 42 changed files with 6,830 additions and 84 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_estree_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_estree_ast"
repository = "https://github.com/swc-project/swc.git"
version = "0.2.0"
version = "0.2.1"

[package.metadata.docs.rs]
all-features = true
Expand Down
85 changes: 83 additions & 2 deletions crates/swc_estree_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ pub struct ClassPrivateMethod {
pub type_parameters: Option<TypeParamDeclOrNoop>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassProperty")]
#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct ClassProperty {
#[serde(flatten)]
pub base: BaseNode,
Expand Down Expand Up @@ -289,6 +288,88 @@ pub struct ClassProperty {
#[serde(default)]
pub readonly: Option<bool>,
}
#[derive(Serialize)]
struct BabelClassProperty<'a> {
#[serde(rename = "type")]
type_: &'static str,
#[serde(flatten)]
pub base: &'a BaseNode,
pub key: &'a ObjectKey,
#[serde(default)]
pub value: Option<&'a Expression>,
#[serde(default)]
pub type_annotation: Option<&'a TypeAnnotOrNoop>,
#[serde(default)]
pub decorators: Option<&'a [Decorator]>,
#[serde(default)]
pub computed: Option<bool>,
#[serde(default, rename = "static")]
pub is_static: Option<bool>,
#[serde(default, rename = "abstract")]
pub is_abstract: Option<bool>,
#[serde(default)]
pub accessibility: Option<&'a Access>,
#[serde(default)]
pub declare: Option<bool>,
#[serde(default)]
pub definite: Option<bool>,
#[serde(default)]
pub optional: Option<bool>,
#[serde(default)]
pub readonly: Option<bool>,
}

impl Serialize for ClassProperty {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match Flavor::current() {
Flavor::Babel => {
let actual = BabelClassProperty {
type_: "ClassProperty",
base: &self.base,
key: &self.key,
value: self.value.as_deref(),
type_annotation: self.type_annotation.as_deref(),
decorators: self.decorators.as_deref(),
computed: self.computed,
is_static: self.is_static,
is_abstract: self.is_abstract,
accessibility: self.accessibility.as_ref(),
declare: self.declare,
definite: self.definite,
optional: self.optional,
readonly: self.readonly,
};
actual.serialize(serializer)
}
Flavor::Acorn => {
let mut s = serializer.serialize_map(None)?;

{
// TODO(kdy1): This is bad.
self.base
.serialize(serde::__private::ser::FlatMapSerializer(&mut s))?;
}

s.serialize_entry("type", "PropertyDefinition")?;
s.serialize_entry("static", &self.is_static)?;
s.serialize_entry("key", &self.key)?;
s.serialize_entry("value", &self.value)?;
s.serialize_entry("computed", &self.computed)?;
if let Some(decorators) = &self.decorators {
if !decorators.is_empty() {
s.serialize_entry("decorators", decorators)?;
}
}
s.serialize_entry("computed", &self.computed)?;

s.end()
}
}
}
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("StaticBlock")]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_estree_ast/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct BaseNode {
#[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_range")]
pub range: Option<[usize; 2]>,

#[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_loc")]
#[serde(default)]
pub loc: Option<Loc>,
}

Expand Down
7 changes: 1 addition & 6 deletions crates/swc_estree_ast/src/flavor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::Loc;
use scoped_tls::scoped_thread_local;

scoped_thread_local!(static FLAVOR: Flavor);
Expand Down Expand Up @@ -33,17 +32,13 @@ impl Flavor {
}

pub fn emit_loc(&self) -> bool {
matches!(Self::current(), Flavor::Babel)
true
}

pub(crate) fn skip_range(_: &Option<[usize; 2]>) -> bool {
matches!(Self::current(), Flavor::Babel)
}

pub(crate) fn skip_loc(_: &Option<Loc>) -> bool {
!Self::current().emit_loc()
}

pub(crate) fn skip_empty<T>(v: &T) -> bool
where
T: IsEmpty,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_estree_compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_estree_compat"
repository = "https://github.com/swc-project/swc.git"
version = "0.4.0"
version = "0.4.1"

[package.metadata.docs.rs]
all-features = true
Expand All @@ -24,7 +24,7 @@ swc_ecma_ast = {version = "0.58", path = "../swc_ecma_ast"}
swc_ecma_parser = {version = "0.78", path = "../swc_ecma_parser"}
swc_ecma_utils = {version = "0.52.3", path = "../swc_ecma_utils"}
swc_ecma_visit = {version = "0.44", path = "../swc_ecma_visit"}
swc_estree_ast = {version = "0.2.0", path = "../swc_estree_ast"}
swc_estree_ast = {version = "0.2.1", path = "../swc_estree_ast"}
swc_node_comments = {version = "0.1", path = "../swc_node_comments/"}

[dev-dependencies]
Expand Down
15 changes: 10 additions & 5 deletions crates/swc_estree_compat/scripts/test-acorn.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// File to parse input as an AST using acorn


const acorn = require("acorn");
const res = acorn.parse(process.argv[1], {
ecmaVersion: 2020,

const parserOptions = {
ranges: true,
allowHashBang: true
})
locations: true,
ecmaVersion: "latest",
// https://github.com/tc39/proposal-hashbang
allowHashBang: true,
};

const acorn = require("acorn");
const res = acorn.parse(process.argv[1], parserOptions)
console.log(JSON.stringify(res, void 0, 2));
3 changes: 0 additions & 3 deletions crates/swc_estree_compat/src/babelify/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ impl Babelify for Script {
/// Module nodes.
fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
let mut base = ctx.base(span);
if matches!(Flavor::current(), Flavor::Acorn) {
return base;
}

base.end = base.end.map(|num| num + 1);
base.loc = base.loc.map(|loc| Loc {
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_estree_compat/tests/flavor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ fn assert_flavor(flavor: Flavor, input: &Path, output_json_path: &Path) {
}
}

// We don't try to match fully.
"column" | "line" => match value {
Value::Number(..) => {
*value = Value::Null;
}
_ => {}
},

_ => {}
}
});
Expand Down
74 changes: 72 additions & 2 deletions crates/swc_estree_compat/tests/flavor/acorn/1/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"end": 28,
"id": {
"end": 9,
"loc": {
"end": {
"column": 9,
"line": 1
},
"start": {
"column": 6,
"line": 1
}
},
"name": "foo",
"range": [
6,
Expand All @@ -18,6 +28,16 @@
"arguments": [
{
"end": 27,
"loc": {
"end": {
"column": 27,
"line": 1
},
"start": {
"column": 20,
"line": 1
}
},
"range": [
20,
27
Expand All @@ -30,6 +50,16 @@
],
"callee": {
"end": 19,
"loc": {
"end": {
"column": 19,
"line": 1
},
"start": {
"column": 12,
"line": 1
}
},
"name": "require",
"range": [
12,
Expand All @@ -39,6 +69,16 @@
"type": "Identifier"
},
"end": 28,
"loc": {
"end": {
"column": 28,
"line": 1
},
"start": {
"column": 12,
"line": 1
}
},
"optional": false,
"range": [
12,
Expand All @@ -47,6 +87,16 @@
"start": 12,
"type": "CallExpression"
},
"loc": {
"end": {
"column": 28,
"line": 1
},
"start": {
"column": 6,
"line": 1
}
},
"range": [
6,
28
Expand All @@ -57,6 +107,16 @@
],
"end": 29,
"kind": "const",
"loc": {
"end": {
"column": 29,
"line": 1
},
"start": {
"column": 0,
"line": 1
}
},
"range": [
0,
29
Expand All @@ -65,10 +125,20 @@
"type": "VariableDeclaration"
}
],
"end": 29,
"end": 30,
"loc": {
"end": {
"column": 0,
"line": 2
},
"start": {
"column": 0,
"line": 1
}
},
"range": [
0,
29
30
],
"sourceType": "script",
"start": 0,
Expand Down
Loading

1 comment on commit 586ab0c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 586ab0c Previous: edc0cb0 Ratio
base_tr_fixer 27200 ns/iter (± 793) 25850 ns/iter (± 4511) 1.05
base_tr_resolver_and_hygiene 151237 ns/iter (± 29643) 153985 ns/iter (± 12156) 0.98
codegen_es2015 60889 ns/iter (± 584) 57006 ns/iter (± 4363) 1.07
codegen_es2016 61323 ns/iter (± 561) 56612 ns/iter (± 4488) 1.08
codegen_es2017 60542 ns/iter (± 333) 58697 ns/iter (± 4369) 1.03
codegen_es2018 61169 ns/iter (± 604) 56303 ns/iter (± 3986) 1.09
codegen_es2019 61584 ns/iter (± 471) 56767 ns/iter (± 4716) 1.08
codegen_es2020 60557 ns/iter (± 457) 56467 ns/iter (± 3809) 1.07
codegen_es3 60696 ns/iter (± 936) 56610 ns/iter (± 4732) 1.07
codegen_es5 60927 ns/iter (± 452) 56382 ns/iter (± 3100) 1.08
full_es2015 211134765 ns/iter (± 14972457) 199292291 ns/iter (± 6428713) 1.06
full_es2016 160863027 ns/iter (± 10291611) 160624985 ns/iter (± 5467169) 1.00
full_es2017 173417649 ns/iter (± 14682545) 165749277 ns/iter (± 8517200) 1.05
full_es2018 173187298 ns/iter (± 11268824) 164594761 ns/iter (± 8091966) 1.05
full_es2019 166082476 ns/iter (± 12971506) 161111114 ns/iter (± 8981444) 1.03
full_es2020 169978631 ns/iter (± 16509375) 161322989 ns/iter (± 8424552) 1.05
full_es3 237834207 ns/iter (± 22446593) 225995999 ns/iter (± 20519121) 1.05
full_es5 225556573 ns/iter (± 13916602) 210355023 ns/iter (± 13751540) 1.07
parser 770415 ns/iter (± 17462) 729157 ns/iter (± 56992) 1.06
ser_ast_node 176 ns/iter (± 6) 165 ns/iter (± 11) 1.07
ser_serde 192 ns/iter (± 6) 183 ns/iter (± 16) 1.05
emit_colors 20256765 ns/iter (± 26114007) 18883578 ns/iter (± 24327556) 1.07
emit_large 109916946 ns/iter (± 166806054) 103259281 ns/iter (± 159195501) 1.06
base_clone 2795592 ns/iter (± 124460) 2526799 ns/iter (± 107632) 1.11
fold_span 4629526 ns/iter (± 57964) 4385509 ns/iter (± 216405) 1.06
fold_span_panic 4897665 ns/iter (± 194829) 4667149 ns/iter (± 218875) 1.05
visit_mut_span 3408050 ns/iter (± 52010) 3099522 ns/iter (± 238653) 1.10
visit_mut_span_panic 3467414 ns/iter (± 95913) 3201029 ns/iter (± 160177) 1.08
ast_clone 19651 ns/iter (± 841) 18820 ns/iter (± 1699) 1.04
ast_clone_to_stable 59944 ns/iter (± 1585) 55719 ns/iter (± 4679) 1.08
ast_clone_to_stable_then_to_unstable 110464 ns/iter (± 1940) 101451 ns/iter (± 8661) 1.09
json_deserialize 2311893 ns/iter (± 18587) 2147332 ns/iter (± 112590) 1.08
json_serialize 100747 ns/iter (± 1923) 93097 ns/iter (± 6687) 1.08
boxing_boxed 178 ns/iter (± 2) 162 ns/iter (± 16) 1.10
boxing_boxed_clone 80 ns/iter (± 1) 74 ns/iter (± 6) 1.08
boxing_unboxed 159 ns/iter (± 190) 149 ns/iter (± 9) 1.07
boxing_unboxed_clone 75 ns/iter (± 0) 72 ns/iter (± 5) 1.04
time_10 417 ns/iter (± 3) 343 ns/iter (± 20) 1.22
time_15 786 ns/iter (± 7) 744 ns/iter (± 66) 1.06
time_20 1351 ns/iter (± 19) 1257 ns/iter (± 112) 1.07
time_40 4900 ns/iter (± 36) 4763 ns/iter (± 501) 1.03
time_5 121 ns/iter (± 1) 121 ns/iter (± 5) 1
time_60 10417 ns/iter (± 61) 10157 ns/iter (± 439) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.