Skip to content

Commit

Permalink
fix(swc): Fix target (#2226)
Browse files Browse the repository at this point in the history
swc:
 - Fix `target` option. (#2225)
  • Loading branch information
kdy1 authored Sep 10, 2021
1 parent 2c47778 commit 9ffe471
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 320 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc"
repository = "https://github.com/swc-project/swc.git"
version = "0.51.1"
version = "0.52.0"

[lib]
name = "swc"
Expand All @@ -29,6 +29,13 @@ name = "swc"
default = ["es3"]
# You can disable this feautre to reduce binary size.
es3 = []
# See https://github.com/swc-project/swc/issues/1108
#
# If this option is enabled, es2015 transforms are enabled if target is es2015.
# If this option is disabled, es2015 transforms are disabled if target is es2015.
#
# This feature will be removed on v2 of node bindings.
wrong-target = []

[dependencies]
ahash = "0.7.2"
Expand Down
2 changes: 1 addition & 1 deletion node/binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ napi-derive = {version = "1"}
path-clean = "0.1"
serde = {version = "1", features = ["derive"]}
serde_json = {version = "1", features = ["unbounded_depth"]}
swc = {path = "../../"}
swc = {path = "../../", features = ["wrong-target"]}
swc_atoms = {version = "0.2.4", path = "../../atoms"}
swc_bundler = {path = "../../bundler"}
swc_common = {path = "../../common", features = ["sourcemap"]}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/core",
"version": "1.2.85",
"version": "1.2.86",
"description": "Super-fast alternative for babel",
"homepage": "https://swc.rs",
"main": "./index.js",
Expand Down
39 changes: 31 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use swc_common::{
chain, comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark,
SourceMap,
};
use swc_ecma_ast::Module;
use swc_ecma_ast::{EsVersion, Module};
use swc_ecma_minifier::option::MinifyOptions;
use swc_ecma_parser::Syntax;
use swc_ecma_transforms::{
Expand Down Expand Up @@ -169,12 +169,27 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
} else {
Either::Right(chain!(
import_assertions(),
Optional::new(compat::es2021::es2021(), self.target < JscTarget::Es2021),
Optional::new(compat::es2020::es2020(), self.target < JscTarget::Es2020),
Optional::new(
compat::es2021::es2021(),
should_enable(self.target, JscTarget::Es2021)
),
Optional::new(
compat::es2020::es2020(),
should_enable(self.target, JscTarget::Es2020)
),
Optional::new(typescript::strip(), syntax.typescript()),
Optional::new(compat::es2018(), self.target <= JscTarget::Es2018),
Optional::new(compat::es2017(), self.target <= JscTarget::Es2017),
Optional::new(compat::es2016(), self.target <= JscTarget::Es2016),
Optional::new(
compat::es2018(),
should_enable(self.target, JscTarget::Es2018)
),
Optional::new(
compat::es2017(),
should_enable(self.target, JscTarget::Es2017)
),
Optional::new(
compat::es2016(),
should_enable(self.target, JscTarget::Es2016)
),
Optional::new(
compat::es2015(
self.global_mark,
Expand All @@ -189,11 +204,11 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
},
}
),
self.target <= JscTarget::Es2015
should_enable(self.target, JscTarget::Es2015)
),
Optional::new(
compat::es3(syntax.dynamic_import()),
cfg!(feature = "es3") && self.target <= JscTarget::Es3
cfg!(feature = "es3") && self.target == JscTarget::Es3
)
))
};
Expand Down Expand Up @@ -271,3 +286,11 @@ impl VisitMut for MinifierPass {
}
}
}

fn should_enable(target: EsVersion, feature: EsVersion) -> bool {
if cfg!(feature = "wrong-target") {
target <= feature
} else {
target < feature
}
}
39 changes: 4 additions & 35 deletions tests/fixture/issue-1235/case-1/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function() {
var self = this, args = arguments;
return new Promise(function(resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
class Service {
is(a) {
return _asyncToGenerator(function*() {
return a.toUpperCase() === a;
})();
async is(a) {
return a.toUpperCase() === a;
}
}
_asyncToGenerator(function*() {
yield new Service().is('ABC');
(async ()=>{
await new Service().is('ABC');
})();
5 changes: 2 additions & 3 deletions tests/fixture/issue-1271/output/input.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export var importSomething = function() {
return import('lodash');
};
export const importSomething = ()=>import('lodash')
;
11 changes: 8 additions & 3 deletions tests/fixture/issue-1314/case1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
var a = {
method: function() {
var string = "\n width: ".concat(Math.abs(this.currentX - this.startX - left), "px;\n height: ").concat(Math.abs(this.currentY - this.realtimeStartY - top), "px;\n top: ").concat(Math.min(this.currentY - top, this.realtimeStartY) + this.scrollTop, "px;\n left: ").concat(Math.min(this.currentX - left, this.startX) + this.scrollLeft, "px\n ");
const a = {
method () {
const string = `
width: ${Math.abs(this.currentX - this.startX - left)}px;
height: ${Math.abs(this.currentY - this.realtimeStartY - top)}px;
top: ${Math.min(this.currentY - top, this.realtimeStartY) + this.scrollTop}px;
left: ${Math.min(this.currentX - left, this.startX) + this.scrollLeft}px
`;
}
};
2 changes: 1 addition & 1 deletion tests/fixture/issue-1549/case3-es2015/output/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var a = "\r\n";
const a = `\r\n`;
37 changes: 5 additions & 32 deletions tests/fixture/issue-1681/es2015/output/input.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
function _taggedTemplateLiteral(strings, raw) {
if (!raw) {
raw = strings.slice(0);
}
return Object.freeze(Object.defineProperties(strings, {
raw: {
value: Object.freeze(raw)
}
}));
}
function _templateObject() {
var data = _taggedTemplateLiteral([
"a\nb\nc\n"
]);
_templateObject = function _templateObject() {
return data;
};
return data;
}
function _templateObject1() {
var data = _taggedTemplateLiteral([
"a\nb\nc\n"
], [
"a\nb\nc\nc\n"
]);
_templateObject1 = function _templateObject1() {
return data;
};
return data;
}
console.log(String.raw(_templateObject()));
console.log(String.raw(_templateObject1()));
console.log(String.raw`a
b
c
`);
console.log(String.raw`a\nb\nc\n`);
2 changes: 1 addition & 1 deletion tests/fixture/issue-1701/case1/output/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var f=function(){for(var _len=arguments.length,characters=new Array(_len),_key=0;_key<_len;_key++){characters[_key]=arguments[_key]}return characters.length};var g=function(str){return f.apply(void 0,str)};g("meow")
const f=(...characters)=>characters.length;const g=str=>f(...str);g("meow")
13 changes: 4 additions & 9 deletions tests/fixture/issue-1701/case2/output/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
var f = function() {
for(var _len = arguments.length, characters = new Array(_len), _key = 0; _key < _len; _key++){
characters[_key] = arguments[_key];
}
return characters.length;
};
var g = function(str) {
return f.apply(void 0, str);
};
const f = (...characters)=>characters.length
;
const g = (str)=>f(...str)
;
g("meow");
Loading

0 comments on commit 9ffe471

Please sign in to comment.