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:
OJ Kwon 2021-07-20 02:00:27 -07:00 committed by GitHub
parent d6454add72
commit de24ff275d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 86 additions and 7 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_module"
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
[dependencies]

View File

@ -101,9 +101,11 @@ impl Visit for ImportAnalyzer {
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::Namespace(ref _ns) => {
if &*import.src.value != "@swc/helpers" {
scope.import_types.insert(import.src.value.clone(), true);
}
}
ImportSpecifier::Default(_) => {
scope
.import_types

View File

@ -364,9 +364,25 @@ 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::Namespace(ref ns) => {
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) => {
self.idents.insert(
i.local.to_id(),

View File

@ -0,0 +1,13 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"dynamicImport": true,
"decorators": false
}
},
"module": {
"type": "commonjs"
}
}

View File

@ -0,0 +1,4 @@
import foo, * as action from './actions';
console.log(action);
console.log(foo);

View 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);

View File

@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true,
"dynamicImport": true,
"decorators": false
}
}
}

View File

@ -0,0 +1,4 @@
import foo, * as action from './actions';
console.log(action);
console.log(foo);

View File

@ -0,0 +1,3 @@
import foo, * as action from './actions';
console.log(action);
console.log(foo);