Skip to content

Commit

Permalink
fix(swc): Fix various bugs (swc-project#1588)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_module:
 - Change the order of functions exported as default. (swc-project#1568)
 - Handle mixed imports correctly. (swc-project#1525)

swc:
 - Ensure that swc-project#1581 is fixed. (swc-project#1581)
  • Loading branch information
kdy1 committed Apr 18, 2021
1 parent d7ea5ae commit 4db24fb
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 19 deletions.
10 changes: 8 additions & 2 deletions ecmascript/transforms/module/src/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Fold for CommonJs {
}
DefaultDecl::Fn(FnExpr { ident, function }) => {
// init_export!("default");

let will_inject_var = ident.is_none();
let ident = ident.unwrap_or_else(|| private_ident!("_default"));

extra_stmts.push(ModuleItem::Stmt(Stmt::Decl(Decl::Fn(
Expand All @@ -256,7 +256,13 @@ impl Fold for CommonJs {
.fold_with(self),
))));

extra_stmts.push(
let to = if will_inject_var {
&mut extra_stmts
} else {
&mut stmts
};

to.push(
AssignExpr {
span: DUMMY_SP,
left: PatOrExpr::Expr(member_expr!(
Expand Down
4 changes: 3 additions & 1 deletion ecmascript/transforms/module/src/import_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Visit for ImportAnalyzer {
.insert(import.src.value.clone(), true);
}
} else {
let mut has_non_default = false;
for s in &import.specifiers {
match *s {
ImportSpecifier::Namespace(..) => unreachable!(
Expand Down Expand Up @@ -89,8 +90,9 @@ impl Visit for ImportAnalyzer {
self.scope
.import_types
.entry(import.src.value.clone())
.or_insert(false);
.or_insert(has_non_default);
} else {
has_non_default = true;
self.scope
.import_types
.entry(import.src.value.clone())
Expand Down
18 changes: 8 additions & 10 deletions ecmascript/transforms/module/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use swc_atoms::{js_word, JsWord};
use swc_common::{Mark, Span, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::MapWithMut;
use swc_ecma_utils::ident::IdentLike;
use swc_ecma_utils::member_expr;
use swc_ecma_utils::private_ident;
use swc_ecma_utils::quote_ident;
Expand Down Expand Up @@ -324,14 +325,15 @@ impl Scope {
))
});

let mut has_non_default = false;
for s in import.specifiers {
match s {
ImportSpecifier::Namespace(..) => unreachable!(
"import * as foo cannot be used with other type of import specifiers"
),
ImportSpecifier::Default(i) => {
self.idents.insert(
(i.local.sym.clone(), i.local.span.ctxt()),
i.local.to_id(),
(import.src.value.clone(), js_word!("default")),
);
self.import_types
Expand All @@ -345,16 +347,15 @@ impl Scope {
let name = imported.map(|i| i.sym).unwrap_or_else(|| local.sym.clone());
let is_default = name == js_word!("default");

self.idents.insert(
(local.sym.clone(), local.span.ctxt()),
(import.src.value.clone(), name),
);
self.idents
.insert(local.to_id(), (import.src.value.clone(), name));

if is_default {
self.import_types
.entry(import.src.value.clone())
.or_insert(false);
.or_insert(has_non_default);
} else {
has_non_default = true;
self.import_types
.entry(import.src.value.clone())
.and_modify(|v| *v = true);
Expand Down Expand Up @@ -382,10 +383,7 @@ impl Scope {
}

fn fold_ident(folder: &mut impl ModulePass, top_level: bool, i: Ident) -> Result<Expr, Ident> {
let v = {
let v = folder.scope().idents.get(&(i.sym.clone(), i.span.ctxt()));
v.cloned()
};
let v = folder.scope().idents.get(&i.to_id()).cloned();
match v {
None => Err(i),
Some((src, prop)) => {
Expand Down
31 changes: 26 additions & 5 deletions ecmascript/transforms/module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2100,10 +2100,10 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
function foo() {}
exports.default = foo;
function foo() {}
"#
);

Expand Down Expand Up @@ -4634,12 +4634,12 @@ test!(
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = get;
function get(key) {
console.log(key);
}
exports.default = get;
"
);

Expand Down Expand Up @@ -4670,3 +4670,24 @@ test!(
exports.default = _default;
"
);

test!(
syntax(),
|_| tr(Default::default()),
issue_1588_1,
"
import { Component, default as React } from 'react';
class X extends Component {
}
React.render();
",
"
'use strict';
var _react = _interopRequireWildcard(require('react'));
class X extends _react.Component {
}
_react.default.render();
"
);
2 changes: 1 addition & 1 deletion ecmascript/transforms/react/src/refresh/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ test!(
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = App;
var _hooks = require("./hooks");
var _foo = _interopRequireDefault(require("./foo"));
Expand All @@ -1126,7 +1127,6 @@ test!(
const foo = (0, _foo).default();
return <h1>{bar}</h1>;
}
exports.default = App;
_s(App, "useFancyState{bar}\nuseFoo{foo}", false, function () {
return [_hooks.useFancyState, _foo.default];
Expand Down
12 changes: 12 additions & 0 deletions node-swc/__tests__/transform/issue_1581_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path');
const swc = require('../../..');

it('should emit source maps as inline if requested', () => {
const filename = path.resolve(
__dirname + "/../../tests/issue-1581/foo.ts"
);
// We only check if it's not empty sourcemap.
expect(swc.transformFileSync(filename).code.trim()).not.toContain(
`eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdGRvdXQifQ==`
);
});
3 changes: 3 additions & 0 deletions node-swc/tests/issue-1581/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceMaps": "inline"
}
3 changes: 3 additions & 0 deletions node-swc/tests/issue-1581/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = () => {
return 2;
};
17 changes: 17 additions & 0 deletions tests/fixture/issue-1525/case1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"targets": {
"node": "15.10"
}
},
"jsc": {
"target": "es2019",
"parser": {
"syntax": "typescript",
"tsx": true
}
},
"module": {
"type": "commonjs"
}
}
5 changes: 5 additions & 0 deletions tests/fixture/issue-1525/case1/input/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Component, default as React } from "react";

class X extends Component {}

React.render(document.getElementById("#root"), <X></X>);
28 changes: 28 additions & 0 deletions tests/fixture/issue-1525/case1/output/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";
var _react = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {
};
if (obj != null) {
for(var key in obj){
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}
newObj.default = obj;
return newObj;
}
}
class X extends _react.Component {
}
_react.default.render(document.getElementById("#root"), /*#__PURE__*/ _react.default.createElement(X, null));
21 changes: 21 additions & 0 deletions tests/fixture/issue-1568/case1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": false,
"dynamicImport": true
},
"target": "es5",
"loose": true,
"externalHelpers": false
},
"minify": false,
"env": {
"mode": "entry",
"coreJs": 3
},
"module": {
"type": "commonjs"
}
}
3 changes: 3 additions & 0 deletions tests/fixture/issue-1568/case1/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function get(key: string) {
console.log(key);
}
8 changes: 8 additions & 0 deletions tests/fixture/issue-1568/case1/output/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = get;
function get(key) {
console.log(key);
}

0 comments on commit 4db24fb

Please sign in to comment.