Skip to content

Commit

Permalink
Mark wrapNativeSuper and wrapRegExp as pure
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 12, 2020
1 parent 3c6a8ae commit 2e14276
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 17 deletions.
Expand Up @@ -18,6 +18,7 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-annotate-as-pure": "^7.8.3",
"@babel/helper-regex": "^7.8.3",
"regexpu-core": "^4.6.0"
},
Expand Down
14 changes: 8 additions & 6 deletions packages/babel-helper-create-regexp-features-plugin/src/index.js
Expand Up @@ -11,6 +11,7 @@ import { generateRegexpuOptions } from "./util";
import pkg from "../package.json";
import { types as t } from "@babel/core";
import { pullFlag } from "@babel/helper-regex";
import annotateAsPure from "@babel/helper-annotate-as-pure";

// Note: Versions are represented as an integer. e.g. 7.1.5 is represented
// as 70000100005. This method is easier than using a semver-parsing
Expand Down Expand Up @@ -68,12 +69,13 @@ export function createRegExpFeaturePlugin({ name, feature, options = {} }) {
runtime &&
!isRegExpTest(path)
) {
path.replaceWith(
t.callExpression(this.addHelper("wrapRegExp"), [
node,
t.valueToNode(namedCaptureGroups),
]),
);
const call = t.callExpression(this.addHelper("wrapRegExp"), [
node,
t.valueToNode(namedCaptureGroups),
]);
annotateAsPure(call);

path.replaceWith(call);
}
if (hasFeature(features, FEATURES.unicodeFlag)) {
pullFlag(node, "u");
Expand Down
14 changes: 9 additions & 5 deletions packages/babel-plugin-transform-classes/src/transformClass.js
Expand Up @@ -6,6 +6,7 @@ import ReplaceSupers, {
import optimiseCall from "@babel/helper-optimise-call-expression";
import * as defineMap from "@babel/helper-define-map";
import { traverse, template, types as t } from "@babel/core";
import annotateAsPure from "@babel/helper-annotate-as-pure";

type ReadonlySet<T> = Set<T> | { has(val: T): boolean };

Expand Down Expand Up @@ -565,11 +566,14 @@ export default function transformClass(
const closureArgs = [];

if (classState.isDerived) {
const arg = classState.extendsNative
? t.callExpression(classState.file.addHelper("wrapNativeSuper"), [
t.cloneNode(superName),
])
: t.cloneNode(superName);
let arg = t.cloneNode(superName);
if (classState.extendsNative) {
arg = t.callExpression(classState.file.addHelper("wrapNativeSuper"), [
arg,
]);
annotateAsPure(arg);
}

const param = classState.scope.generateUidIdentifierBasedOnNode(
superName,
);
Expand Down
Expand Up @@ -24,4 +24,6 @@ function (_Array) {
}

return List;
}(_wrapNativeSuper(Array));
}(
/*#__PURE__*/
_wrapNativeSuper(Array));
Expand Up @@ -32,4 +32,6 @@ function (_Array) {
}

return List;
}(_wrapNativeSuper(Array));
}(
/*#__PURE__*/
_wrapNativeSuper(Array));
Expand Up @@ -22,7 +22,9 @@ function (_HTMLElement) {
}

return CustomElement;
}(babelHelpers.wrapNativeSuper(HTMLElement));
}(
/*#__PURE__*/
babelHelpers.wrapNativeSuper(HTMLElement));

;
new CustomElement();
Expand Down
@@ -1,3 +1,5 @@
"foo".match(babelHelpers.wrapRegExp(/(.)\1/, {
"foo".match(
/*#__PURE__*/
babelHelpers.wrapRegExp(/(.)\1/, {
double: 1
}));
@@ -1,3 +1,5 @@
"abba".match(babelHelpers.wrapRegExp(/(.)(.)\2\1/, {
"abba".match(
/*#__PURE__*/
babelHelpers.wrapRegExp(/(.)(.)\2\1/, {
n: 2
}));
Expand Up @@ -34,6 +34,8 @@ function (_Date) {
}

return MyDate;
}(_wrapNativeSuper(Date));
}(
/*#__PURE__*/
_wrapNativeSuper(Date));

var myDate = new MyDate();

0 comments on commit 2e14276

Please sign in to comment.