Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow instance properties transformation on namespace #10372

Merged
merged 2 commits into from Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/babel-plugin-transform-runtime/src/index.js
Expand Up @@ -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) {
if (isNamespaced(path.get("object"))) return false;
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
if (!methods[name].types) return true;

const typeAnnotation = path.get("object").getTypeAnnotation();
Expand Down
@@ -0,0 +1,2 @@
import * as bar from "bar";
bar.map();
@@ -0,0 +1,3 @@
{
"plugins": [["transform-runtime", { "corejs": 3 }]]
}
@@ -0,0 +1,2 @@
import * as bar from "bar";
bar.map();
Expand Up @@ -15,6 +15,7 @@ import {
isPolyfillSource,
getImportSource,
getRequireSource,
isNamespaced,
} from "../../utils";
import { logUsagePolyfills } from "../../debug";

Expand Down Expand Up @@ -102,6 +103,9 @@ export default function(
const { node } = path;
const { object, property } = node;

// ignore namespace
if (isNamespaced(path.get("object"))) return;

let evaluatedPropType = object.name;
let propertyName = property.name;
let instanceType = "";
Expand Down
Expand Up @@ -21,6 +21,7 @@ import {
isPolyfillSource,
getImportSource,
getRequireSource,
isNamespaced,
} from "../../utils";
import { logUsagePolyfills } from "../../debug";

Expand Down Expand Up @@ -98,7 +99,7 @@ export default function(
}
}
}
return { builtIn, instanceType };
return { builtIn, instanceType, isNamespaced: isNamespaced(path) };
}

const addAndRemovePolyfillImports = {
Expand Down Expand Up @@ -230,7 +231,8 @@ export default function(
};

this.addPropertyDependencies = function(source = {}, key) {
const { builtIn, instanceType } = source;
const { builtIn, instanceType, isNamespaced } = source;
if (isNamespaced) return;
if (PossibleGlobalObjects.has(builtIn)) {
this.addBuiltInDependencies(key);
} else if (has(StaticProperties, builtIn)) {
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-preset-env/src/utils.js
Expand Up @@ -162,3 +162,10 @@ export function getModulePath(mod: string): string {
export function createImport(path: NodePath, mod: string) {
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();
}
@@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;
@@ -0,0 +1,12 @@
{
"presets": [
[
"../../../../lib",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 2
}
]
]
}
@@ -0,0 +1,3 @@
import * as ns from "ns";
ns.map;

@@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;
@@ -0,0 +1,12 @@
{
"presets": [
[
"../../../../lib",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
@@ -0,0 +1,2 @@
import * as ns from "ns";
ns.map;