Don't allow instance properties transformation on namespace (#10372)

This commit is contained in:
Bin Xin 2019-09-05 23:32:57 +08:00 committed by Brian Ng
parent 6813ee02d7
commit 29734b924a
13 changed files with 62 additions and 2 deletions

View File

@ -113,7 +113,14 @@ export default declare((api, options, dirname) => {
); );
} }
function isNamespaced(path) {
const binding = path.scope.getBinding(path.node.name);
if (!binding) return false;
return binding.path.isImportNamespaceSpecifier();
}
function maybeNeedsPolyfill(path, methods, name) { function maybeNeedsPolyfill(path, methods, name) {
if (isNamespaced(path.get("object"))) return false;
if (!methods[name].types) return true; if (!methods[name].types) return true;
const typeAnnotation = path.get("object").getTypeAnnotation(); const typeAnnotation = path.get("object").getTypeAnnotation();

View File

@ -0,0 +1,2 @@
import * as bar from "bar";
bar.map();

View File

@ -0,0 +1,3 @@
{
"plugins": [["transform-runtime", { "corejs": 3 }]]
}

View File

@ -0,0 +1,2 @@
import * as bar from "bar";
bar.map();

View File

@ -15,6 +15,7 @@ import {
isPolyfillSource, isPolyfillSource,
getImportSource, getImportSource,
getRequireSource, getRequireSource,
isNamespaced,
} from "../../utils"; } from "../../utils";
import { logUsagePolyfills } from "../../debug"; import { logUsagePolyfills } from "../../debug";
@ -102,6 +103,9 @@ export default function(
const { node } = path; const { node } = path;
const { object, property } = node; const { object, property } = node;
// ignore namespace
if (isNamespaced(path.get("object"))) return;
let evaluatedPropType = object.name; let evaluatedPropType = object.name;
let propertyName = property.name; let propertyName = property.name;
let instanceType = ""; let instanceType = "";

View File

@ -21,6 +21,7 @@ import {
isPolyfillSource, isPolyfillSource,
getImportSource, getImportSource,
getRequireSource, getRequireSource,
isNamespaced,
} from "../../utils"; } from "../../utils";
import { logUsagePolyfills } from "../../debug"; import { logUsagePolyfills } from "../../debug";
@ -98,7 +99,7 @@ export default function(
} }
} }
} }
return { builtIn, instanceType }; return { builtIn, instanceType, isNamespaced: isNamespaced(path) };
} }
const addAndRemovePolyfillImports = { const addAndRemovePolyfillImports = {
@ -230,7 +231,8 @@ export default function(
}; };
this.addPropertyDependencies = function(source = {}, key) { this.addPropertyDependencies = function(source = {}, key) {
const { builtIn, instanceType } = source; const { builtIn, instanceType, isNamespaced } = source;
if (isNamespaced) return;
if (PossibleGlobalObjects.has(builtIn)) { if (PossibleGlobalObjects.has(builtIn)) {
this.addBuiltInDependencies(key); this.addBuiltInDependencies(key);
} else if (has(StaticProperties, builtIn)) { } else if (has(StaticProperties, builtIn)) {

View File

@ -162,3 +162,10 @@ export function getModulePath(mod: string): string {
export function createImport(path: NodePath, mod: string) { export function createImport(path: NodePath, mod: string) {
return addSideEffect(path, getModulePath(mod)); return addSideEffect(path, getModulePath(mod));
} }
export function isNamespaced(path: NodePath) {
if (!path.node) return false;
const binding = path.scope.getBinding(path.node.name);
if (!binding) return false;
return binding.path.isImportNamespaceSpecifier();
}

View File

@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;

View File

@ -0,0 +1,12 @@
{
"presets": [
[
"../../../../lib",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 2
}
]
]
}

View File

@ -0,0 +1,3 @@
import * as ns from "ns";
ns.map;

View File

@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;

View File

@ -0,0 +1,12 @@
{
"presets": [
[
"../../../../lib",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}

View File

@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;