mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 02:06:08 +03:00
fix(es/transforms/module): Allow namespace import with default import (#1940)
swc_ecma_transforms_module: - Allow using a namespace import specifier with a default import specifier. (#1938)
This commit is contained in:
parent
d6454add72
commit
de24ff275d
@ -6,7 +6,7 @@ edition = "2018"
|
|||||||
license = "Apache-2.0/MIT"
|
license = "Apache-2.0/MIT"
|
||||||
name = "swc_ecma_transforms_module"
|
name = "swc_ecma_transforms_module"
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
version = "0.26.1"
|
version = "0.26.2"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -101,9 +101,11 @@ impl Visit for ImportAnalyzer {
|
|||||||
let mut has_non_default = false;
|
let mut has_non_default = false;
|
||||||
for s in &import.specifiers {
|
for s in &import.specifiers {
|
||||||
match *s {
|
match *s {
|
||||||
ImportSpecifier::Namespace(..) => unreachable!(
|
ImportSpecifier::Namespace(ref _ns) => {
|
||||||
"import * as foo cannot be used with other type of import specifiers"
|
if &*import.src.value != "@swc/helpers" {
|
||||||
),
|
scope.import_types.insert(import.src.value.clone(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
ImportSpecifier::Default(_) => {
|
ImportSpecifier::Default(_) => {
|
||||||
scope
|
scope
|
||||||
.import_types
|
.import_types
|
||||||
|
@ -364,9 +364,25 @@ impl Scope {
|
|||||||
let mut has_non_default = false;
|
let mut has_non_default = false;
|
||||||
for s in import.specifiers {
|
for s in import.specifiers {
|
||||||
match s {
|
match s {
|
||||||
ImportSpecifier::Namespace(..) => unreachable!(
|
ImportSpecifier::Namespace(ref ns) => {
|
||||||
"import * as foo cannot be used with other type of import specifiers"
|
self.idents.insert(
|
||||||
),
|
(ns.local.sym.clone(), ns.local.span.ctxt()),
|
||||||
|
(import.src.value.clone(), "".into()),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Override symbol if one exists
|
||||||
|
self.imports
|
||||||
|
.entry(import.src.value.clone())
|
||||||
|
.and_modify(|v| match *v {
|
||||||
|
Some(ref mut v) => v.0 = ns.local.sym.clone(),
|
||||||
|
None => *v = Some((ns.local.sym.clone(), ns.local.span)),
|
||||||
|
})
|
||||||
|
.or_insert_with(|| Some((ns.local.sym.clone(), ns.local.span)));
|
||||||
|
|
||||||
|
if &*import.src.value != "@swc/helpers" {
|
||||||
|
self.import_types.insert(import.src.value.clone(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
ImportSpecifier::Default(i) => {
|
ImportSpecifier::Default(i) => {
|
||||||
self.idents.insert(
|
self.idents.insert(
|
||||||
i.local.to_id(),
|
i.local.to_id(),
|
||||||
|
13
tests/fixture/issue-1938/cjs/input/.swcrc
Normal file
13
tests/fixture/issue-1938/cjs/input/.swcrc
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"jsc": {
|
||||||
|
"parser": {
|
||||||
|
"syntax": "typescript",
|
||||||
|
"tsx": true,
|
||||||
|
"dynamicImport": true,
|
||||||
|
"decorators": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"module": {
|
||||||
|
"type": "commonjs"
|
||||||
|
}
|
||||||
|
}
|
4
tests/fixture/issue-1938/cjs/input/index.js
Normal file
4
tests/fixture/issue-1938/cjs/input/index.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import foo, * as action from './actions';
|
||||||
|
|
||||||
|
console.log(action);
|
||||||
|
console.log(foo);
|
27
tests/fixture/issue-1938/cjs/output/index.js
Normal file
27
tests/fixture/issue-1938/cjs/output/index.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
"use strict";
|
||||||
|
var action = _interopRequireWildcard(require("./actions"));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log(action);
|
||||||
|
console.log(action.default);
|
10
tests/fixture/issue-1938/esm/input/.swcrc
Normal file
10
tests/fixture/issue-1938/esm/input/.swcrc
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"jsc": {
|
||||||
|
"parser": {
|
||||||
|
"syntax": "typescript",
|
||||||
|
"tsx": true,
|
||||||
|
"dynamicImport": true,
|
||||||
|
"decorators": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
tests/fixture/issue-1938/esm/input/index.js
Normal file
4
tests/fixture/issue-1938/esm/input/index.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import foo, * as action from './actions';
|
||||||
|
|
||||||
|
console.log(action);
|
||||||
|
console.log(foo);
|
3
tests/fixture/issue-1938/esm/output/index.js
Normal file
3
tests/fixture/issue-1938/esm/output/index.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import foo, * as action from './actions';
|
||||||
|
console.log(action);
|
||||||
|
console.log(foo);
|
Loading…
Reference in New Issue
Block a user