remove es20xx prefixes from plugins and rename folders (#6575)
This commit is contained in:
3
packages/babel-plugin-transform-classes/.npmignore
Normal file
3
packages/babel-plugin-transform-classes/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
121
packages/babel-plugin-transform-classes/README.md
Normal file
121
packages/babel-plugin-transform-classes/README.md
Normal file
@@ -0,0 +1,121 @@
|
||||
# @babel/plugin-transform-classes
|
||||
|
||||
> Compile ES2015 classes to ES5
|
||||
|
||||
## Caveats
|
||||
|
||||
Built-in classes such as `Date`, `Array`, `DOM` etc cannot be properly subclassed
|
||||
due to limitations in ES5 (for the [classes](http://babeljs.io/docs/plugins/transform-classes) plugin).
|
||||
You can try to use [babel-plugin-transform-builtin-extend](https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend) based on `Object.setPrototypeOf` and `Reflect.construct`, but it also has some limitations.
|
||||
|
||||
## Examples
|
||||
|
||||
**In**
|
||||
|
||||
```javascript
|
||||
class Test {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
logger () {
|
||||
console.log("Hello", this.name);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Test = function () {
|
||||
function Test(name) {
|
||||
_classCallCheck(this, Test);
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
Test.prototype.logger = function logger() {
|
||||
console.log("Hello", this.name);
|
||||
};
|
||||
|
||||
return Test;
|
||||
}();
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-transform-classes
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```js
|
||||
// without options
|
||||
{
|
||||
"plugins": ["@babel/transform-classes"]
|
||||
}
|
||||
|
||||
// with options
|
||||
{
|
||||
"plugins": [
|
||||
["@babel/transform-classes", {
|
||||
"loose": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins @babel/transform-classes script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("@babel/core").transform("code", {
|
||||
plugins: ["@babel/transform-classes"]
|
||||
});
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
### `loose`
|
||||
|
||||
`boolean`, defaults to `false`.
|
||||
|
||||
#### Method enumerability
|
||||
|
||||
Please note that in loose mode class methods **are** enumerable. This is not in line
|
||||
with the spec and you may run into issues.
|
||||
|
||||
#### Method assignment
|
||||
|
||||
Under loose mode, methods are defined on the class prototype with simple assignments
|
||||
instead of being defined. This can result in the following not working:
|
||||
|
||||
```javascript
|
||||
class Foo {
|
||||
set bar() {
|
||||
throw new Error("foo!");
|
||||
}
|
||||
}
|
||||
|
||||
class Bar extends Foo {
|
||||
bar() {
|
||||
// will throw an error when this method is defined
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
When `Bar.prototype.foo` is defined it triggers the setter on `Foo`. This is a
|
||||
case that is very unlikely to appear in production code however it's something
|
||||
to keep in mind.
|
||||
27
packages/babel-plugin-transform-classes/package.json
Normal file
27
packages/babel-plugin-transform-classes/package.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "@babel/plugin-transform-classes",
|
||||
"version": "7.0.0-beta.3",
|
||||
"description": "Compile ES2015 classes to ES5",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-classes",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@babel/helper-annotate-as-pure": "7.0.0-beta.3",
|
||||
"@babel/helper-define-map": "7.0.0-beta.3",
|
||||
"@babel/helper-function-name": "7.0.0-beta.3",
|
||||
"@babel/helper-optimise-call-expression": "7.0.0-beta.3",
|
||||
"@babel/helper-replace-supers": "7.0.0-beta.3",
|
||||
"@babel/template": "7.0.0-beta.3",
|
||||
"@babel/traverse": "7.0.0-beta.3",
|
||||
"@babel/types": "7.0.0-beta.3"
|
||||
},
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.3"
|
||||
}
|
||||
}
|
||||
67
packages/babel-plugin-transform-classes/src/index.js
Normal file
67
packages/babel-plugin-transform-classes/src/index.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import LooseTransformer from "./loose";
|
||||
import VanillaTransformer from "./vanilla";
|
||||
import annotateAsPure from "@babel/helper-annotate-as-pure";
|
||||
import nameFunction from "@babel/helper-function-name";
|
||||
|
||||
export default function({ types: t }, options) {
|
||||
const { loose } = options;
|
||||
const Constructor = loose ? LooseTransformer : VanillaTransformer;
|
||||
|
||||
// todo: investigate traversal requeueing
|
||||
const VISITED = Symbol();
|
||||
|
||||
return {
|
||||
visitor: {
|
||||
ExportDefaultDeclaration(path) {
|
||||
if (!path.get("declaration").isClassDeclaration()) return;
|
||||
|
||||
const { node } = path;
|
||||
const ref =
|
||||
node.declaration.id || path.scope.generateUidIdentifier("class");
|
||||
node.declaration.id = ref;
|
||||
|
||||
// Split the class declaration and the export into two separate statements.
|
||||
path.replaceWith(node.declaration);
|
||||
path.insertAfter(
|
||||
t.exportNamedDeclaration(null, [
|
||||
t.exportSpecifier(ref, t.identifier("default")),
|
||||
]),
|
||||
);
|
||||
},
|
||||
|
||||
ClassDeclaration(path) {
|
||||
const { node } = path;
|
||||
|
||||
const ref = node.id || path.scope.generateUidIdentifier("class");
|
||||
|
||||
path.replaceWith(
|
||||
t.variableDeclaration("let", [
|
||||
t.variableDeclarator(ref, t.toExpression(node)),
|
||||
]),
|
||||
);
|
||||
},
|
||||
|
||||
ClassExpression(path, state) {
|
||||
const { node } = path;
|
||||
if (node[VISITED]) return;
|
||||
|
||||
const inferred = nameFunction(path);
|
||||
if (inferred && inferred !== node) {
|
||||
path.replaceWith(inferred);
|
||||
return;
|
||||
}
|
||||
|
||||
node[VISITED] = true;
|
||||
|
||||
path.replaceWith(new Constructor(path, state.file).run());
|
||||
|
||||
if (path.isCallExpression()) {
|
||||
annotateAsPure(path);
|
||||
if (path.get("callee").isArrowFunctionExpression()) {
|
||||
path.get("callee").arrowFunctionToExpression();
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { Scope } from "@babel/traverse";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
export default function(
|
||||
decorators: Array<Object>,
|
||||
scope: Scope,
|
||||
): Array<Object> {
|
||||
for (const decorator of decorators) {
|
||||
const expression = decorator.expression;
|
||||
if (!t.isMemberExpression(expression)) continue;
|
||||
|
||||
const temp = scope.maybeGenerateMemoised(expression.object);
|
||||
let ref;
|
||||
|
||||
const nodes = [];
|
||||
|
||||
if (temp) {
|
||||
ref = temp;
|
||||
nodes.push(t.assignmentExpression("=", temp, expression.object));
|
||||
} else {
|
||||
ref = expression.object;
|
||||
}
|
||||
|
||||
nodes.push(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.memberExpression(ref, expression.property, expression.computed),
|
||||
t.identifier("bind"),
|
||||
),
|
||||
[ref],
|
||||
),
|
||||
);
|
||||
|
||||
if (nodes.length === 1) {
|
||||
decorator.expression = nodes[0];
|
||||
} else {
|
||||
decorator.expression = t.sequenceExpression(nodes);
|
||||
}
|
||||
}
|
||||
|
||||
return decorators;
|
||||
}
|
||||
67
packages/babel-plugin-transform-classes/src/loose.js
Normal file
67
packages/babel-plugin-transform-classes/src/loose.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import nameFunction from "@babel/helper-function-name";
|
||||
import VanillaTransformer from "./vanilla";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
export default class LooseClassTransformer extends VanillaTransformer {
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this._protoAlias = null;
|
||||
this.isLoose = true;
|
||||
}
|
||||
|
||||
_insertProtoAliasOnce() {
|
||||
if (!this._protoAlias) {
|
||||
this._protoAlias = this.scope.generateUidIdentifier("proto");
|
||||
const classProto = t.memberExpression(
|
||||
this.classRef,
|
||||
t.identifier("prototype"),
|
||||
);
|
||||
const protoDeclaration = t.variableDeclaration("var", [
|
||||
t.variableDeclarator(this._protoAlias, classProto),
|
||||
]);
|
||||
|
||||
this.body.push(protoDeclaration);
|
||||
}
|
||||
}
|
||||
|
||||
_processMethod(node, scope) {
|
||||
if (!node.decorators) {
|
||||
// use assignments instead of define properties for loose classes
|
||||
|
||||
let classRef = this.classRef;
|
||||
if (!node.static) {
|
||||
this._insertProtoAliasOnce();
|
||||
classRef = this._protoAlias;
|
||||
}
|
||||
const methodName = t.memberExpression(
|
||||
classRef,
|
||||
node.key,
|
||||
node.computed || t.isLiteral(node.key),
|
||||
);
|
||||
|
||||
let func = t.functionExpression(
|
||||
null,
|
||||
node.params,
|
||||
node.body,
|
||||
node.generator,
|
||||
node.async,
|
||||
);
|
||||
func.returnType = node.returnType;
|
||||
const key = t.toComputedKey(node, node.key);
|
||||
if (t.isStringLiteral(key)) {
|
||||
func = nameFunction({
|
||||
node: func,
|
||||
id: key,
|
||||
scope,
|
||||
});
|
||||
}
|
||||
|
||||
const expr = t.expressionStatement(
|
||||
t.assignmentExpression("=", methodName, func),
|
||||
);
|
||||
t.inheritsComments(expr, node);
|
||||
this.body.push(expr);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
608
packages/babel-plugin-transform-classes/src/vanilla.js
Normal file
608
packages/babel-plugin-transform-classes/src/vanilla.js
Normal file
@@ -0,0 +1,608 @@
|
||||
import type { NodePath } from "@babel/traverse";
|
||||
import { visitors } from "@babel/traverse";
|
||||
import ReplaceSupers from "@babel/helper-replace-supers";
|
||||
import optimiseCall from "@babel/helper-optimise-call-expression";
|
||||
import * as defineMap from "@babel/helper-define-map";
|
||||
import template from "@babel/template";
|
||||
import * as t from "@babel/types";
|
||||
|
||||
const noMethodVisitor = {
|
||||
"FunctionExpression|FunctionDeclaration"(path) {
|
||||
path.skip();
|
||||
},
|
||||
|
||||
Method(path) {
|
||||
path.skip();
|
||||
},
|
||||
};
|
||||
|
||||
const verifyConstructorVisitor = visitors.merge([
|
||||
noMethodVisitor,
|
||||
{
|
||||
MemberExpression: {
|
||||
exit(path) {
|
||||
const objectPath = path.get("object");
|
||||
if (this.isDerived && !this.hasBareSuper && objectPath.isSuper()) {
|
||||
const hasArrowFunctionParent = path.findParent(p =>
|
||||
p.isArrowFunctionExpression(),
|
||||
);
|
||||
if (!hasArrowFunctionParent) {
|
||||
throw objectPath.buildCodeFrameError(
|
||||
"'super.*' is not allowed before super()",
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
CallExpression: {
|
||||
exit(path) {
|
||||
if (path.get("callee").isSuper()) {
|
||||
this.hasBareSuper = true;
|
||||
|
||||
if (!this.isDerived) {
|
||||
throw path.buildCodeFrameError(
|
||||
"super() is only allowed in a derived constructor",
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
ThisExpression(path) {
|
||||
if (this.isDerived && !this.hasBareSuper) {
|
||||
const fn = path.find(p => p.isFunction());
|
||||
|
||||
if (!fn || !fn.isArrowFunctionExpression()) {
|
||||
throw path.buildCodeFrameError(
|
||||
"'this' is not allowed before super()",
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
const findThisesVisitor = visitors.merge([
|
||||
noMethodVisitor,
|
||||
{
|
||||
ThisExpression(path) {
|
||||
this.superThises.push(path);
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
export default class ClassTransformer {
|
||||
constructor(path: NodePath, file) {
|
||||
this.parent = path.parent;
|
||||
this.scope = path.scope;
|
||||
this.node = path.node;
|
||||
this.path = path;
|
||||
this.file = file;
|
||||
|
||||
this.clearDescriptors();
|
||||
|
||||
this.instancePropBody = [];
|
||||
this.instancePropRefs = {};
|
||||
this.staticPropBody = [];
|
||||
this.body = [];
|
||||
|
||||
this.bareSupers = [];
|
||||
|
||||
this.pushedConstructor = false;
|
||||
this.pushedInherits = false;
|
||||
this.isLoose = false;
|
||||
|
||||
this.superThises = [];
|
||||
|
||||
// class id
|
||||
this.classId = this.node.id;
|
||||
|
||||
// this is the name of the binding that will **always** reference the class we've constructed
|
||||
this.classRef = this.node.id
|
||||
? t.identifier(this.node.id.name)
|
||||
: this.scope.generateUidIdentifier("class");
|
||||
|
||||
this.superName = this.node.superClass || t.identifier("Function");
|
||||
this.isDerived = !!this.node.superClass;
|
||||
}
|
||||
|
||||
run() {
|
||||
let superName = this.superName;
|
||||
const file = this.file;
|
||||
let body = this.body;
|
||||
|
||||
//
|
||||
|
||||
const constructorBody = (this.constructorBody = t.blockStatement([]));
|
||||
this.constructor = this.buildConstructor();
|
||||
|
||||
//
|
||||
|
||||
const closureParams = [];
|
||||
const closureArgs = [];
|
||||
|
||||
//
|
||||
if (this.isDerived) {
|
||||
closureArgs.push(superName);
|
||||
|
||||
superName = this.scope.generateUidIdentifierBasedOnNode(superName);
|
||||
closureParams.push(superName);
|
||||
|
||||
this.superName = superName;
|
||||
}
|
||||
|
||||
//
|
||||
this.buildBody();
|
||||
|
||||
// make sure this class isn't directly called (with A() instead new A())
|
||||
if (!this.isLoose) {
|
||||
constructorBody.body.unshift(
|
||||
t.expressionStatement(
|
||||
t.callExpression(file.addHelper("classCallCheck"), [
|
||||
t.thisExpression(),
|
||||
this.classRef,
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
body = body.concat(this.staticPropBody.map(fn => fn(this.classRef)));
|
||||
|
||||
if (this.classId) {
|
||||
// named class with only a constructor
|
||||
if (body.length === 1) return t.toExpression(body[0]);
|
||||
}
|
||||
|
||||
//
|
||||
body.push(t.returnStatement(this.classRef));
|
||||
|
||||
const container = t.arrowFunctionExpression(
|
||||
closureParams,
|
||||
t.blockStatement(body),
|
||||
);
|
||||
return t.callExpression(container, closureArgs);
|
||||
}
|
||||
|
||||
buildConstructor() {
|
||||
const func = t.functionDeclaration(this.classRef, [], this.constructorBody);
|
||||
t.inherits(func, this.node);
|
||||
return func;
|
||||
}
|
||||
|
||||
pushToMap(node, enumerable, kind = "value", scope?) {
|
||||
let mutatorMap;
|
||||
if (node.static) {
|
||||
this.hasStaticDescriptors = true;
|
||||
mutatorMap = this.staticMutatorMap;
|
||||
} else {
|
||||
this.hasInstanceDescriptors = true;
|
||||
mutatorMap = this.instanceMutatorMap;
|
||||
}
|
||||
|
||||
const map = defineMap.push(mutatorMap, node, kind, this.file, scope);
|
||||
|
||||
if (enumerable) {
|
||||
map.enumerable = t.booleanLiteral(true);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Please add a description.]
|
||||
* https://www.youtube.com/watch?v=fWNaR-rxAic
|
||||
*/
|
||||
|
||||
constructorMeMaybe() {
|
||||
let hasConstructor = false;
|
||||
const paths = this.path.get("body.body");
|
||||
for (const path of (paths: Array)) {
|
||||
hasConstructor = path.equals("kind", "constructor");
|
||||
if (hasConstructor) break;
|
||||
}
|
||||
if (hasConstructor) return;
|
||||
|
||||
let params, body;
|
||||
|
||||
if (this.isDerived) {
|
||||
const constructor = template.expression.ast`
|
||||
(function () {
|
||||
super(...arguments);
|
||||
})
|
||||
`;
|
||||
params = constructor.params;
|
||||
body = constructor.body;
|
||||
} else {
|
||||
params = [];
|
||||
body = t.blockStatement([]);
|
||||
}
|
||||
|
||||
this.path
|
||||
.get("body")
|
||||
.unshiftContainer(
|
||||
"body",
|
||||
t.classMethod("constructor", t.identifier("constructor"), params, body),
|
||||
);
|
||||
}
|
||||
|
||||
buildBody() {
|
||||
this.constructorMeMaybe();
|
||||
this.pushBody();
|
||||
this.verifyConstructor();
|
||||
|
||||
if (this.userConstructor) {
|
||||
const constructorBody = this.constructorBody;
|
||||
constructorBody.body = constructorBody.body.concat(
|
||||
this.userConstructor.body.body,
|
||||
);
|
||||
t.inherits(this.constructor, this.userConstructor);
|
||||
t.inherits(constructorBody, this.userConstructor.body);
|
||||
}
|
||||
|
||||
this.pushDescriptors();
|
||||
}
|
||||
|
||||
pushBody() {
|
||||
const classBodyPaths: Array<Object> = this.path.get("body.body");
|
||||
|
||||
for (const path of classBodyPaths) {
|
||||
const node = path.node;
|
||||
|
||||
if (path.isClassProperty()) {
|
||||
throw path.buildCodeFrameError("Missing class properties transform.");
|
||||
}
|
||||
|
||||
if (node.decorators) {
|
||||
throw path.buildCodeFrameError(
|
||||
"Method has decorators, put the decorator plugin before the classes one.",
|
||||
);
|
||||
}
|
||||
|
||||
if (t.isClassMethod(node)) {
|
||||
const isConstructor = node.kind === "constructor";
|
||||
|
||||
if (isConstructor) {
|
||||
path.traverse(verifyConstructorVisitor, this);
|
||||
}
|
||||
|
||||
const replaceSupers = new ReplaceSupers(
|
||||
{
|
||||
forceSuperMemoisation: isConstructor,
|
||||
methodPath: path,
|
||||
methodNode: node,
|
||||
objectRef: this.classRef,
|
||||
superRef: this.superName,
|
||||
isStatic: node.static,
|
||||
isLoose: this.isLoose,
|
||||
scope: this.scope,
|
||||
file: this.file,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
replaceSupers.replace();
|
||||
|
||||
if (isConstructor) {
|
||||
this.pushConstructor(replaceSupers, node, path);
|
||||
} else {
|
||||
this.pushMethod(node, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearDescriptors() {
|
||||
this.hasInstanceDescriptors = false;
|
||||
this.hasStaticDescriptors = false;
|
||||
|
||||
this.instanceMutatorMap = {};
|
||||
this.staticMutatorMap = {};
|
||||
}
|
||||
|
||||
pushDescriptors() {
|
||||
this.pushInherits();
|
||||
|
||||
const body = this.body;
|
||||
|
||||
let instanceProps;
|
||||
let staticProps;
|
||||
|
||||
if (this.hasInstanceDescriptors) {
|
||||
instanceProps = defineMap.toClassObject(this.instanceMutatorMap);
|
||||
}
|
||||
|
||||
if (this.hasStaticDescriptors) {
|
||||
staticProps = defineMap.toClassObject(this.staticMutatorMap);
|
||||
}
|
||||
|
||||
if (instanceProps || staticProps) {
|
||||
if (instanceProps) {
|
||||
instanceProps = defineMap.toComputedObjectFromClass(instanceProps);
|
||||
}
|
||||
if (staticProps) {
|
||||
staticProps = defineMap.toComputedObjectFromClass(staticProps);
|
||||
}
|
||||
|
||||
const nullNode = t.nullLiteral();
|
||||
|
||||
let args = [
|
||||
this.classRef, // Constructor
|
||||
nullNode, // instanceDescriptors
|
||||
nullNode, // staticDescriptors
|
||||
nullNode, // instanceInitializers
|
||||
nullNode, // staticInitializers
|
||||
];
|
||||
|
||||
if (instanceProps) args[1] = instanceProps;
|
||||
if (staticProps) args[2] = staticProps;
|
||||
|
||||
if (this.instanceInitializersId) {
|
||||
args[3] = this.instanceInitializersId;
|
||||
body.unshift(this.buildObjectAssignment(this.instanceInitializersId));
|
||||
}
|
||||
|
||||
if (this.staticInitializersId) {
|
||||
args[4] = this.staticInitializersId;
|
||||
body.unshift(this.buildObjectAssignment(this.staticInitializersId));
|
||||
}
|
||||
|
||||
let lastNonNullIndex = 0;
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] !== nullNode) lastNonNullIndex = i;
|
||||
}
|
||||
args = args.slice(0, lastNonNullIndex + 1);
|
||||
|
||||
body.push(
|
||||
t.expressionStatement(
|
||||
t.callExpression(this.file.addHelper("createClass"), args),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
this.clearDescriptors();
|
||||
}
|
||||
|
||||
buildObjectAssignment(id) {
|
||||
return t.variableDeclaration("var", [
|
||||
t.variableDeclarator(id, t.objectExpression([])),
|
||||
]);
|
||||
}
|
||||
|
||||
wrapSuperCall(bareSuper, superRef, thisRef, body) {
|
||||
let bareSuperNode = bareSuper.node;
|
||||
|
||||
if (this.isLoose) {
|
||||
bareSuperNode.arguments.unshift(t.thisExpression());
|
||||
if (
|
||||
bareSuperNode.arguments.length === 2 &&
|
||||
t.isSpreadElement(bareSuperNode.arguments[1]) &&
|
||||
t.isIdentifier(bareSuperNode.arguments[1].argument, {
|
||||
name: "arguments",
|
||||
})
|
||||
) {
|
||||
// special case single arguments spread
|
||||
bareSuperNode.arguments[1] = bareSuperNode.arguments[1].argument;
|
||||
bareSuperNode.callee = t.memberExpression(
|
||||
superRef,
|
||||
t.identifier("apply"),
|
||||
);
|
||||
} else {
|
||||
bareSuperNode.callee = t.memberExpression(
|
||||
superRef,
|
||||
t.identifier("call"),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
bareSuperNode = optimiseCall(
|
||||
t.logicalExpression(
|
||||
"||",
|
||||
t.memberExpression(this.classRef, t.identifier("__proto__")),
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier("Object"),
|
||||
t.identifier("getPrototypeOf"),
|
||||
),
|
||||
[this.classRef],
|
||||
),
|
||||
),
|
||||
t.thisExpression(),
|
||||
bareSuperNode.arguments,
|
||||
);
|
||||
}
|
||||
|
||||
let call;
|
||||
|
||||
if (this.isLoose) {
|
||||
call = t.logicalExpression("||", bareSuperNode, t.thisExpression());
|
||||
} else {
|
||||
call = t.callExpression(
|
||||
this.file.addHelper("possibleConstructorReturn"),
|
||||
[t.thisExpression(), bareSuperNode],
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
bareSuper.parentPath.isExpressionStatement() &&
|
||||
bareSuper.parentPath.container === body.node.body &&
|
||||
body.node.body.length - 1 === bareSuper.parentPath.key
|
||||
) {
|
||||
// this super call is the last statement in the body so we can just straight up
|
||||
// turn it into a return
|
||||
|
||||
if (this.superThises.length) {
|
||||
call = t.assignmentExpression("=", thisRef(), call);
|
||||
}
|
||||
|
||||
bareSuper.parentPath.replaceWith(t.returnStatement(call));
|
||||
} else {
|
||||
bareSuper.replaceWith(t.assignmentExpression("=", thisRef(), call));
|
||||
}
|
||||
}
|
||||
|
||||
verifyConstructor() {
|
||||
if (!this.isDerived) return;
|
||||
|
||||
const path = this.userConstructorPath;
|
||||
const body = path.get("body");
|
||||
|
||||
if (!this.hasBareSuper && !this.superReturns.length) {
|
||||
throw path.buildCodeFrameError("missing super() call in constructor");
|
||||
}
|
||||
|
||||
path.traverse(findThisesVisitor, this);
|
||||
|
||||
let guaranteedSuperBeforeFinish = !!this.bareSupers.length;
|
||||
|
||||
const superRef = this.superName || t.identifier("Function");
|
||||
let thisRef = function() {
|
||||
const ref = path.scope.generateDeclaredUidIdentifier("this");
|
||||
thisRef = () => ref;
|
||||
return ref;
|
||||
};
|
||||
|
||||
for (const bareSuper of this.bareSupers) {
|
||||
this.wrapSuperCall(bareSuper, superRef, thisRef, body);
|
||||
|
||||
if (guaranteedSuperBeforeFinish) {
|
||||
bareSuper.find(function(parentPath) {
|
||||
// hit top so short circuit
|
||||
if (parentPath === path) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (parentPath.isLoop() || parentPath.isConditional()) {
|
||||
guaranteedSuperBeforeFinish = false;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const thisPath of this.superThises) {
|
||||
thisPath.replaceWith(thisRef());
|
||||
}
|
||||
|
||||
let wrapReturn;
|
||||
|
||||
if (this.isLoose) {
|
||||
wrapReturn = returnArg => {
|
||||
return returnArg
|
||||
? t.logicalExpression("||", returnArg, thisRef())
|
||||
: thisRef();
|
||||
};
|
||||
} else {
|
||||
wrapReturn = returnArg =>
|
||||
t.callExpression(
|
||||
this.file.addHelper("possibleConstructorReturn"),
|
||||
[thisRef()].concat(returnArg || []),
|
||||
);
|
||||
}
|
||||
|
||||
// if we have a return as the last node in the body then we've already caught that
|
||||
// return
|
||||
const bodyPaths = body.get("body");
|
||||
if (bodyPaths.length && !bodyPaths.pop().isReturnStatement()) {
|
||||
body.pushContainer(
|
||||
"body",
|
||||
t.returnStatement(
|
||||
guaranteedSuperBeforeFinish ? thisRef() : wrapReturn(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
for (const returnPath of this.superReturns) {
|
||||
returnPath
|
||||
.get("argument")
|
||||
.replaceWith(wrapReturn(returnPath.node.argument));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Push a method to its respective mutatorMap.
|
||||
*/
|
||||
|
||||
pushMethod(node: { type: "ClassMethod" }, path?: NodePath) {
|
||||
const scope = path ? path.scope : this.scope;
|
||||
|
||||
if (node.kind === "method") {
|
||||
if (this._processMethod(node, scope)) return;
|
||||
}
|
||||
|
||||
this.pushToMap(node, false, null, scope);
|
||||
}
|
||||
|
||||
_processMethod() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the constructor body of our class.
|
||||
*/
|
||||
|
||||
pushConstructor(
|
||||
replaceSupers,
|
||||
method: { type: "ClassMethod" },
|
||||
path: NodePath,
|
||||
) {
|
||||
this.bareSupers = replaceSupers.bareSupers;
|
||||
this.superReturns = replaceSupers.returns;
|
||||
|
||||
// https://github.com/babel/babel/issues/1077
|
||||
if (path.scope.hasOwnBinding(this.classRef.name)) {
|
||||
path.scope.rename(this.classRef.name);
|
||||
}
|
||||
|
||||
const construct = this.constructor;
|
||||
|
||||
this.userConstructorPath = path;
|
||||
this.userConstructor = method;
|
||||
this.hasConstructor = true;
|
||||
|
||||
t.inheritsComments(construct, method);
|
||||
|
||||
construct.params = method.params;
|
||||
|
||||
t.inherits(construct.body, method.body);
|
||||
construct.body.directives = method.body.directives;
|
||||
|
||||
// push constructor to body
|
||||
this._pushConstructor();
|
||||
}
|
||||
|
||||
_pushConstructor() {
|
||||
if (this.pushedConstructor) return;
|
||||
this.pushedConstructor = true;
|
||||
|
||||
// we haven't pushed any descriptors yet
|
||||
if (this.hasInstanceDescriptors || this.hasStaticDescriptors) {
|
||||
this.pushDescriptors();
|
||||
}
|
||||
|
||||
this.body.push(this.constructor);
|
||||
|
||||
this.pushInherits();
|
||||
}
|
||||
|
||||
/**
|
||||
* Push inherits helper to body.
|
||||
*/
|
||||
|
||||
pushInherits() {
|
||||
if (!this.isDerived || this.pushedInherits) return;
|
||||
|
||||
// Unshift to ensure that the constructor inheritance is set up before
|
||||
// any properties can be assigned to the prototype.
|
||||
this.pushedInherits = true;
|
||||
this.body.unshift(
|
||||
t.expressionStatement(
|
||||
t.callExpression(
|
||||
this.isLoose
|
||||
? this.file.addHelper("inheritsLoose")
|
||||
: this.file.addHelper("inherits"),
|
||||
[this.classRef, this.superName],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
9
packages/babel-plugin-transform-classes/test/fixtures/exec/declaration-binding.js
vendored
Normal file
9
packages/babel-plugin-transform-classes/test/fixtures/exec/declaration-binding.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
bar() {
|
||||
return Foo;
|
||||
}
|
||||
}
|
||||
|
||||
var Bar = Foo;
|
||||
Foo = 5;
|
||||
assert.equal((new Bar).bar(), Bar);
|
||||
9
packages/babel-plugin-transform-classes/test/fixtures/exec/expression-binding.js
vendored
Normal file
9
packages/babel-plugin-transform-classes/test/fixtures/exec/expression-binding.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
var Foo = class Foo {
|
||||
bar() {
|
||||
return Foo;
|
||||
}
|
||||
}
|
||||
|
||||
var Bar = Foo;
|
||||
Foo = 5;
|
||||
assert.equal((new Bar).bar(), Bar);
|
||||
3
packages/babel-plugin-transform-classes/test/fixtures/exec/options.json
vendored
Normal file
3
packages/babel-plugin-transform-classes/test/fixtures/exec/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-classes", "transform-block-scoping", "transform-typeof-symbol"]
|
||||
}
|
||||
11
packages/babel-plugin-transform-classes/test/fixtures/exec/retain-no-call-on-reassign.js
vendored
Normal file
11
packages/babel-plugin-transform-classes/test/fixtures/exec/retain-no-call-on-reassign.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class Foo {
|
||||
bar() {
|
||||
return Foo;
|
||||
}
|
||||
}
|
||||
|
||||
var Bar = Foo;
|
||||
Foo = 5;
|
||||
assert.throws(function () {
|
||||
Bar.call(6);
|
||||
}, /Cannot call a class as a function/);
|
||||
9
packages/babel-plugin-transform-classes/test/fixtures/exec/return-symbol.js
vendored
Normal file
9
packages/babel-plugin-transform-classes/test/fixtures/exec/return-symbol.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class Foo {
|
||||
constructor() {
|
||||
return Symbol();
|
||||
}
|
||||
}
|
||||
|
||||
const f = new Foo;
|
||||
assert.ok(f instanceof Foo);
|
||||
assert.ok(typeof f === "object");
|
||||
10
packages/babel-plugin-transform-classes/test/fixtures/exec/shadow-container.js
vendored
Normal file
10
packages/babel-plugin-transform-classes/test/fixtures/exec/shadow-container.js
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
function build(val) {
|
||||
return class {
|
||||
[this.key]() {
|
||||
return val;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var Class = build.call({ key: "foo" }, "bar");
|
||||
assert.equal(new Class().foo(), "bar");
|
||||
21
packages/babel-plugin-transform-classes/test/fixtures/exec/super-change-proto.js
vendored
Normal file
21
packages/babel-plugin-transform-classes/test/fixtures/exec/super-change-proto.js
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
var log = '';
|
||||
|
||||
class Base {
|
||||
p() { log += '[Base]'; }
|
||||
}
|
||||
|
||||
class OtherBase {
|
||||
p() { log += '[OtherBase]'; }
|
||||
}
|
||||
|
||||
class Derived extends Base {
|
||||
p() {
|
||||
log += '[Derived]';
|
||||
super.p();
|
||||
Derived.prototype.__proto__ = OtherBase.prototype;
|
||||
super.p();
|
||||
}
|
||||
}
|
||||
|
||||
new Derived().p();
|
||||
assert.equal(log, '[Derived][Base][OtherBase]');
|
||||
14
packages/babel-plugin-transform-classes/test/fixtures/exec/super-return.js
vendored
Normal file
14
packages/babel-plugin-transform-classes/test/fixtures/exec/super-return.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
class Foo {
|
||||
constructor() {
|
||||
return { i: 1 };
|
||||
}
|
||||
}
|
||||
|
||||
class Bar extends Foo {
|
||||
constructor() {
|
||||
assert.equal(super().i, 1);
|
||||
assert.equal(this.i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
new Bar;
|
||||
1
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/class/actual.js
vendored
Normal file
1
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/class/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
class A {}
|
||||
1
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/class/expected.js
vendored
Normal file
1
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/class/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
let A = function A() {};
|
||||
7
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/options.json
vendored
Normal file
7
packages/babel-plugin-transform-classes/test/fixtures/loose-classCallCheck/options.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
["transform-classes", {
|
||||
"loose": true
|
||||
}]
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class A {
|
||||
constructor() {
|
||||
console.log('a');
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
b() {
|
||||
console.log('b');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
let A = function A() {
|
||||
console.log('a');
|
||||
};
|
||||
|
||||
let B =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function B() {}
|
||||
|
||||
var _proto = B.prototype;
|
||||
|
||||
_proto.b = function b() {
|
||||
console.log('b');
|
||||
};
|
||||
|
||||
return B;
|
||||
}();
|
||||
@@ -0,0 +1,8 @@
|
||||
class B {}
|
||||
|
||||
class A extends B {
|
||||
constructor(track) {
|
||||
if (track !== undefined) super(track);
|
||||
else super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
||||
|
||||
let B = function B() {};
|
||||
|
||||
let A =
|
||||
/*#__PURE__*/
|
||||
function (_B) {
|
||||
_inheritsLoose(A, _B);
|
||||
|
||||
function A(track) {
|
||||
var _this;
|
||||
|
||||
if (track !== undefined) _this = _B.call(this, track) || this;else _this = _B.call(this) || this;
|
||||
return _this;
|
||||
}
|
||||
|
||||
return A;
|
||||
}(B);
|
||||
13
packages/babel-plugin-transform-classes/test/fixtures/loose/accessing-super-class/actual.js
vendored
Normal file
13
packages/babel-plugin-transform-classes/test/fixtures/loose/accessing-super-class/actual.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
woops.super.test();
|
||||
super();
|
||||
super.test();
|
||||
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
|
||||
super.test(...arguments);
|
||||
super.test("test", ...arguments);
|
||||
}
|
||||
}
|
||||
27
packages/babel-plugin-transform-classes/test/fixtures/loose/accessing-super-class/expected.js
vendored
Normal file
27
packages/babel-plugin-transform-classes/test/fixtures/loose/accessing-super-class/expected.js
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inheritsLoose(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _Foo$prototype$test, _Foo$prototype$test2;
|
||||
|
||||
var _this;
|
||||
|
||||
woops.super.test();
|
||||
_this = _Foo.call(this) || this;
|
||||
|
||||
_Foo.prototype.test.call(_this);
|
||||
|
||||
_this = _Foo.apply(this, arguments) || this;
|
||||
_this = _Foo.call.apply(_Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))) || this;
|
||||
|
||||
(_Foo$prototype$test = _Foo.prototype.test).call.apply(_Foo$prototype$test, [_this].concat(Array.prototype.slice.call(arguments)));
|
||||
|
||||
(_Foo$prototype$test2 = _Foo.prototype.test).call.apply(_Foo$prototype$test2, [_this, "test"].concat(Array.prototype.slice.call(arguments)));
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
super.test;
|
||||
super.test.whatever;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inheritsLoose(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _this;
|
||||
|
||||
_this = _Foo.call(this) || this;
|
||||
_Foo.prototype.test;
|
||||
_Foo.prototype.test.whatever;
|
||||
return _this;
|
||||
}
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
11
packages/babel-plugin-transform-classes/test/fixtures/loose/calling-super-properties/actual.js
vendored
Normal file
11
packages/babel-plugin-transform-classes/test/fixtures/loose/calling-super-properties/actual.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
super.test.whatever();
|
||||
super.test();
|
||||
}
|
||||
|
||||
static test() {
|
||||
return super.wow();
|
||||
}
|
||||
}
|
||||
23
packages/babel-plugin-transform-classes/test/fixtures/loose/calling-super-properties/expected.js
vendored
Normal file
23
packages/babel-plugin-transform-classes/test/fixtures/loose/calling-super-properties/expected.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inheritsLoose(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _this;
|
||||
|
||||
_this = _Foo.call(this) || this;
|
||||
|
||||
_Foo.prototype.test.whatever();
|
||||
|
||||
_Foo.prototype.test.call(_this);
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
Test.test = function test() {
|
||||
return _Foo.wow.call(this);
|
||||
};
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
13
packages/babel-plugin-transform-classes/test/fixtures/loose/constructor-order/actual.js
vendored
Normal file
13
packages/babel-plugin-transform-classes/test/fixtures/loose/constructor-order/actual.js
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
class x {
|
||||
f() {
|
||||
1
|
||||
2
|
||||
3
|
||||
}
|
||||
|
||||
constructor() {
|
||||
4
|
||||
5
|
||||
6
|
||||
}
|
||||
}
|
||||
19
packages/babel-plugin-transform-classes/test/fixtures/loose/constructor-order/expected.js
vendored
Normal file
19
packages/babel-plugin-transform-classes/test/fixtures/loose/constructor-order/expected.js
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
var x =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
var _proto = x.prototype;
|
||||
|
||||
_proto.f = function f() {
|
||||
1;
|
||||
2;
|
||||
3;
|
||||
};
|
||||
|
||||
function x() {
|
||||
4;
|
||||
5;
|
||||
6;
|
||||
}
|
||||
|
||||
return x;
|
||||
}();
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo extends Bar {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"throws": "missing super() call in constructor",
|
||||
"plugins": ["transform-classes"]
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class Child extends Base {
|
||||
constructor(){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
var Child =
|
||||
/*#__PURE__*/
|
||||
function (_Base) {
|
||||
babelHelpers.inheritsLoose(Child, _Base);
|
||||
|
||||
function Child() {
|
||||
var _this;
|
||||
|
||||
return false || _this;
|
||||
}
|
||||
|
||||
return Child;
|
||||
}(Base);
|
||||
@@ -0,0 +1,5 @@
|
||||
class Child extends Base {
|
||||
constructor(){
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
var Child =
|
||||
/*#__PURE__*/
|
||||
function (_Base) {
|
||||
babelHelpers.inheritsLoose(Child, _Base);
|
||||
|
||||
function Child() {
|
||||
var _this;
|
||||
|
||||
return {} || _this;
|
||||
}
|
||||
|
||||
return Child;
|
||||
}(Base);
|
||||
5
packages/babel-plugin-transform-classes/test/fixtures/loose/literal-key/actual.js
vendored
Normal file
5
packages/babel-plugin-transform-classes/test/fixtures/loose/literal-key/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
class Foo {
|
||||
"bar"() {
|
||||
|
||||
}
|
||||
}
|
||||
11
packages/babel-plugin-transform-classes/test/fixtures/loose/literal-key/expected.js
vendored
Normal file
11
packages/babel-plugin-transform-classes/test/fixtures/loose/literal-key/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Foo() {}
|
||||
|
||||
var _proto = Foo.prototype;
|
||||
|
||||
_proto["bar"] = function bar() {};
|
||||
|
||||
return Foo;
|
||||
}();
|
||||
@@ -0,0 +1,6 @@
|
||||
// @flow
|
||||
class C {
|
||||
m(x: number): string {
|
||||
return 'a';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// @flow
|
||||
var C =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function C() {}
|
||||
|
||||
var _proto = C.prototype;
|
||||
|
||||
_proto.m = function m(x: number): string {
|
||||
return 'a';
|
||||
};
|
||||
|
||||
return C;
|
||||
}();
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "transform-function-name", ["transform-classes", { "loose": true }], "transform-spread", "transform-block-scoping", "syntax-flow"]
|
||||
}
|
||||
5
packages/babel-plugin-transform-classes/test/fixtures/loose/method-reuse-prototype/actual.js
vendored
Normal file
5
packages/babel-plugin-transform-classes/test/fixtures/loose/method-reuse-prototype/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
a() {}
|
||||
static b() {}
|
||||
c() {}
|
||||
}
|
||||
15
packages/babel-plugin-transform-classes/test/fixtures/loose/method-reuse-prototype/expected.js
vendored
Normal file
15
packages/babel-plugin-transform-classes/test/fixtures/loose/method-reuse-prototype/expected.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Test() {}
|
||||
|
||||
var _proto = Test.prototype;
|
||||
|
||||
_proto.a = function a() {};
|
||||
|
||||
Test.b = function b() {};
|
||||
|
||||
_proto.c = function c() {};
|
||||
|
||||
return Test;
|
||||
}();
|
||||
9
packages/babel-plugin-transform-classes/test/fixtures/loose/options.json
vendored
Normal file
9
packages/babel-plugin-transform-classes/test/fixtures/loose/options.json
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugins": [
|
||||
"external-helpers",
|
||||
"transform-function-name",
|
||||
["transform-classes", { "loose": true }],
|
||||
["transform-spread", { "loose": true }],
|
||||
"transform-block-scoping"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
"use strict";
|
||||
class Foo {
|
||||
constructor() {
|
||||
return { x: 1 };
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(new Foo().x, 1);
|
||||
|
||||
class Bar extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(new Bar().x, 1);
|
||||
|
||||
class Bar2 extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
assert.equal(this.x, 1);
|
||||
return { x: 2 };
|
||||
}
|
||||
}
|
||||
|
||||
assert.equal(new Bar2().x, 2);
|
||||
|
||||
|
||||
let singleton;
|
||||
class Sub extends Foo {
|
||||
constructor() {
|
||||
if (singleton) {
|
||||
return singleton;
|
||||
}
|
||||
singleton = super();
|
||||
}
|
||||
}
|
||||
|
||||
let instance = new Sub;
|
||||
assert.equal(instance, singleton);
|
||||
|
||||
instance = new Sub;
|
||||
assert.equal(instance, singleton);
|
||||
@@ -0,0 +1,7 @@
|
||||
class BaseController extends Chaplin.Controller {
|
||||
|
||||
}
|
||||
|
||||
class BaseController2 extends Chaplin.Controller.Another {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
var BaseController =
|
||||
/*#__PURE__*/
|
||||
function (_Chaplin$Controller) {
|
||||
babelHelpers.inheritsLoose(BaseController, _Chaplin$Controller);
|
||||
|
||||
function BaseController() {
|
||||
return _Chaplin$Controller.apply(this, arguments) || this;
|
||||
}
|
||||
|
||||
return BaseController;
|
||||
}(Chaplin.Controller);
|
||||
|
||||
var BaseController2 =
|
||||
/*#__PURE__*/
|
||||
function (_Chaplin$Controller$A) {
|
||||
babelHelpers.inheritsLoose(BaseController2, _Chaplin$Controller$A);
|
||||
|
||||
function BaseController2() {
|
||||
return _Chaplin$Controller$A.apply(this, arguments) || this;
|
||||
}
|
||||
|
||||
return BaseController2;
|
||||
}(Chaplin.Controller.Another);
|
||||
1
packages/babel-plugin-transform-classes/test/fixtures/loose/super-class/actual.js
vendored
Normal file
1
packages/babel-plugin-transform-classes/test/fixtures/loose/super-class/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
class Test extends Foo { }
|
||||
11
packages/babel-plugin-transform-classes/test/fixtures/loose/super-class/expected.js
vendored
Normal file
11
packages/babel-plugin-transform-classes/test/fixtures/loose/super-class/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inheritsLoose(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
return _Foo.apply(this, arguments) || this;
|
||||
}
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
@@ -0,0 +1,5 @@
|
||||
class Test {
|
||||
constructor() {
|
||||
super.hasOwnProperty("test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
var Test = function Test() {
|
||||
Function.prototype.hasOwnProperty.call(this, "test");
|
||||
};
|
||||
23
packages/babel-plugin-transform-classes/test/fixtures/regression/2663/actual.js
vendored
Normal file
23
packages/babel-plugin-transform-classes/test/fixtures/regression/2663/actual.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
import net from 'net';
|
||||
import { EventEmitter } from 'events';
|
||||
import BinarySerializer from './helpers/binary-serializer';
|
||||
// import ...
|
||||
|
||||
export default class Connection extends EventEmitter {
|
||||
constructor(endpoint, joinKey, joinData, roomId) {
|
||||
super();
|
||||
|
||||
this.isConnected = false;
|
||||
this.roomId = roomId;
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
send(message) {
|
||||
this.sock.write(BinarySerializer.serializeMessage(message));
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.sock.close();
|
||||
}
|
||||
}
|
||||
45
packages/babel-plugin-transform-classes/test/fixtures/regression/2663/expected.js
vendored
Normal file
45
packages/babel-plugin-transform-classes/test/fixtures/regression/2663/expected.js
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _net = babelHelpers.interopRequireDefault(require("net"));
|
||||
|
||||
var _events = require("events");
|
||||
|
||||
var _binarySerializer = babelHelpers.interopRequireDefault(require("./helpers/binary-serializer"));
|
||||
|
||||
// import ...
|
||||
var Connection =
|
||||
/*#__PURE__*/
|
||||
function (_EventEmitter) {
|
||||
babelHelpers.inherits(Connection, _EventEmitter);
|
||||
|
||||
function Connection(endpoint, joinKey, joinData, roomId) {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Connection);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Connection.__proto__ || Object.getPrototypeOf(Connection)).call(this));
|
||||
_this.isConnected = false;
|
||||
_this.roomId = roomId; // ...
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Connection, [{
|
||||
key: "send",
|
||||
value: function send(message) {
|
||||
this.sock.write(_binarySerializer.default.serializeMessage(message));
|
||||
}
|
||||
}, {
|
||||
key: "disconnect",
|
||||
value: function disconnect() {
|
||||
this.sock.close();
|
||||
}
|
||||
}]);
|
||||
return Connection;
|
||||
}(_events.EventEmitter);
|
||||
|
||||
exports.default = Connection;
|
||||
8
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/actual.js
vendored
Normal file
8
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import BaseFoo from './BaseFoo';
|
||||
|
||||
export default class SubFoo extends BaseFoo {
|
||||
static talk() {
|
||||
super.talk();
|
||||
console.log('SubFoo.talk');
|
||||
}
|
||||
}
|
||||
30
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/expected.js
vendored
Normal file
30
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/expected.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _BaseFoo2 = babelHelpers.interopRequireDefault(require("./BaseFoo"));
|
||||
|
||||
var SubFoo =
|
||||
/*#__PURE__*/
|
||||
function (_BaseFoo) {
|
||||
babelHelpers.inherits(SubFoo, _BaseFoo);
|
||||
|
||||
function SubFoo() {
|
||||
babelHelpers.classCallCheck(this, SubFoo);
|
||||
return babelHelpers.possibleConstructorReturn(this, (SubFoo.__proto__ || Object.getPrototypeOf(SubFoo)).apply(this, arguments));
|
||||
}
|
||||
|
||||
babelHelpers.createClass(SubFoo, null, [{
|
||||
key: "talk",
|
||||
value: function talk() {
|
||||
babelHelpers.get(SubFoo.__proto__ || Object.getPrototypeOf(SubFoo), "talk", this).call(this);
|
||||
console.log('SubFoo.talk');
|
||||
}
|
||||
}]);
|
||||
return SubFoo;
|
||||
}(_BaseFoo2.default);
|
||||
|
||||
exports.default = SubFoo;
|
||||
4
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/options.json
vendored
Normal file
4
packages/babel-plugin-transform-classes/test/fixtures/regression/2694/options.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["external-helpers"],
|
||||
"presets": ["es2015"]
|
||||
}
|
||||
15
packages/babel-plugin-transform-classes/test/fixtures/regression/2775/actual.js
vendored
Normal file
15
packages/babel-plugin-transform-classes/test/fixtures/regression/2775/actual.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
import React, {Component} from 'react';
|
||||
|
||||
export default class RandomComponent extends Component {
|
||||
constructor(){
|
||||
super();
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className='sui-RandomComponent'>
|
||||
<h2>Hi there!</h2>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
31
packages/babel-plugin-transform-classes/test/fixtures/regression/2775/expected.js
vendored
Normal file
31
packages/babel-plugin-transform-classes/test/fixtures/regression/2775/expected.js
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _react = babelHelpers.interopRequireWildcard(require("react"));
|
||||
|
||||
var RandomComponent =
|
||||
/*#__PURE__*/
|
||||
function (_Component) {
|
||||
babelHelpers.inherits(RandomComponent, _Component);
|
||||
|
||||
function RandomComponent() {
|
||||
babelHelpers.classCallCheck(this, RandomComponent);
|
||||
return babelHelpers.possibleConstructorReturn(this, (RandomComponent.__proto__ || Object.getPrototypeOf(RandomComponent)).call(this));
|
||||
}
|
||||
|
||||
babelHelpers.createClass(RandomComponent, [{
|
||||
key: "render",
|
||||
value: function render() {
|
||||
return _react.default.createElement("div", {
|
||||
className: "sui-RandomComponent"
|
||||
}, _react.default.createElement("h2", null, "Hi there!"));
|
||||
}
|
||||
}]);
|
||||
return RandomComponent;
|
||||
}(_react.Component);
|
||||
|
||||
exports.default = RandomComponent;
|
||||
1
packages/babel-plugin-transform-classes/test/fixtures/regression/2941/actual.js
vendored
Normal file
1
packages/babel-plugin-transform-classes/test/fixtures/regression/2941/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
export default class {}
|
||||
12
packages/babel-plugin-transform-classes/test/fixtures/regression/2941/expected.js
vendored
Normal file
12
packages/babel-plugin-transform-classes/test/fixtures/regression/2941/expected.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _class = function _class() {
|
||||
babelHelpers.classCallCheck(this, _class);
|
||||
};
|
||||
|
||||
exports.default = _class;
|
||||
16
packages/babel-plugin-transform-classes/test/fixtures/regression/3028/actual.js
vendored
Normal file
16
packages/babel-plugin-transform-classes/test/fixtures/regression/3028/actual.js
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
class b {
|
||||
}
|
||||
|
||||
class a1 extends b {
|
||||
constructor() {
|
||||
super();
|
||||
this.x = () => this;
|
||||
}
|
||||
}
|
||||
|
||||
export default class a2 extends b {
|
||||
constructor() {
|
||||
super();
|
||||
this.x = () => this;
|
||||
}
|
||||
}
|
||||
54
packages/babel-plugin-transform-classes/test/fixtures/regression/3028/expected.js
vendored
Normal file
54
packages/babel-plugin-transform-classes/test/fixtures/regression/3028/expected.js
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var b = function b() {
|
||||
babelHelpers.classCallCheck(this, b);
|
||||
};
|
||||
|
||||
var a1 =
|
||||
/*#__PURE__*/
|
||||
function (_b) {
|
||||
babelHelpers.inherits(a1, _b);
|
||||
|
||||
function a1() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, a1);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (a1.__proto__ || Object.getPrototypeOf(a1)).call(this));
|
||||
|
||||
_this.x = function () {
|
||||
return _this;
|
||||
};
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
return a1;
|
||||
}(b);
|
||||
|
||||
var a2 =
|
||||
/*#__PURE__*/
|
||||
function (_b2) {
|
||||
babelHelpers.inherits(a2, _b2);
|
||||
|
||||
function a2() {
|
||||
var _this2;
|
||||
|
||||
babelHelpers.classCallCheck(this, a2);
|
||||
_this2 = babelHelpers.possibleConstructorReturn(this, (a2.__proto__ || Object.getPrototypeOf(a2)).call(this));
|
||||
|
||||
_this2.x = function () {
|
||||
return _this2;
|
||||
};
|
||||
|
||||
return _this2;
|
||||
}
|
||||
|
||||
return a2;
|
||||
}(b);
|
||||
|
||||
exports.default = a2;
|
||||
8
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/actual.js
vendored
Normal file
8
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
class A extends B {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.arrow1 = (x) => { return x; };
|
||||
this.arrow2 = (x) => x;
|
||||
}
|
||||
}
|
||||
16
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/exec.js
vendored
Normal file
16
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/exec.js
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
// https://github.com/babel/babel/issues/5817
|
||||
|
||||
class Parent {}
|
||||
|
||||
class Table extends Parent {
|
||||
constructor() {
|
||||
super();
|
||||
this.returnParam = (param) => {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const table = new Table();
|
||||
|
||||
assert(table.returnParam(false) === false);
|
||||
24
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/expected.js
vendored
Normal file
24
packages/babel-plugin-transform-classes/test/fixtures/regression/5817/expected.js
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
var A =
|
||||
/*#__PURE__*/
|
||||
function (_B) {
|
||||
babelHelpers.inherits(A, _B);
|
||||
|
||||
function A() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, A);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this));
|
||||
|
||||
_this.arrow1 = function (x) {
|
||||
return x;
|
||||
};
|
||||
|
||||
_this.arrow2 = function (x) {
|
||||
return x;
|
||||
};
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
return A;
|
||||
}(B);
|
||||
3
packages/babel-plugin-transform-classes/test/fixtures/regression/T2494/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-classes/test/fixtures/regression/T2494/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var x = {
|
||||
Foo: class extends Foo {}
|
||||
};
|
||||
14
packages/babel-plugin-transform-classes/test/fixtures/regression/T2494/expected.js
vendored
Normal file
14
packages/babel-plugin-transform-classes/test/fixtures/regression/T2494/expected.js
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
var x = {
|
||||
Foo:
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inherits(_class, _Foo);
|
||||
|
||||
function _class() {
|
||||
babelHelpers.classCallCheck(this, _class);
|
||||
return babelHelpers.possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments));
|
||||
}
|
||||
|
||||
return _class;
|
||||
}(Foo)
|
||||
};
|
||||
7
packages/babel-plugin-transform-classes/test/fixtures/regression/T2997/actual.js
vendored
Normal file
7
packages/babel-plugin-transform-classes/test/fixtures/regression/T2997/actual.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
class A {}
|
||||
|
||||
class B extends A {
|
||||
constructor() {
|
||||
return super();
|
||||
}
|
||||
}
|
||||
18
packages/babel-plugin-transform-classes/test/fixtures/regression/T2997/expected.js
vendored
Normal file
18
packages/babel-plugin-transform-classes/test/fixtures/regression/T2997/expected.js
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
var A = function A() {
|
||||
babelHelpers.classCallCheck(this, A);
|
||||
};
|
||||
|
||||
var B =
|
||||
/*#__PURE__*/
|
||||
function (_A) {
|
||||
babelHelpers.inherits(B, _A);
|
||||
|
||||
function B() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, B);
|
||||
return babelHelpers.possibleConstructorReturn(_this, _this = babelHelpers.possibleConstructorReturn(this, (B.__proto__ || Object.getPrototypeOf(B)).call(this)));
|
||||
}
|
||||
|
||||
return B;
|
||||
}(A);
|
||||
5
packages/babel-plugin-transform-classes/test/fixtures/regression/T6712/actual.js
vendored
Normal file
5
packages/babel-plugin-transform-classes/test/fixtures/regression/T6712/actual.js
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
class A {
|
||||
foo() {
|
||||
const foo = 2;
|
||||
}
|
||||
}
|
||||
15
packages/babel-plugin-transform-classes/test/fixtures/regression/T6712/expected.js
vendored
Normal file
15
packages/babel-plugin-transform-classes/test/fixtures/regression/T6712/expected.js
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
var A =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function A() {
|
||||
babelHelpers.classCallCheck(this, A);
|
||||
}
|
||||
|
||||
babelHelpers.createClass(A, [{
|
||||
key: "foo",
|
||||
value: function foo() {
|
||||
var foo = 2;
|
||||
}
|
||||
}]);
|
||||
return A;
|
||||
}();
|
||||
6
packages/babel-plugin-transform-classes/test/fixtures/regression/T6750/actual.js
vendored
Normal file
6
packages/babel-plugin-transform-classes/test/fixtures/regression/T6750/actual.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export default function() {
|
||||
return class Select {
|
||||
query(query) {
|
||||
}
|
||||
}
|
||||
}
|
||||
23
packages/babel-plugin-transform-classes/test/fixtures/regression/T6750/expected.js
vendored
Normal file
23
packages/babel-plugin-transform-classes/test/fixtures/regression/T6750/expected.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = _default;
|
||||
|
||||
function _default() {
|
||||
return (
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Select() {
|
||||
babelHelpers.classCallCheck(this, Select);
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Select, [{
|
||||
key: "query",
|
||||
value: function query(_query) {}
|
||||
}]);
|
||||
return Select;
|
||||
}()
|
||||
);
|
||||
}
|
||||
9
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/actual.js
vendored
Normal file
9
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/actual.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
class Example {
|
||||
async test1(){
|
||||
await Promise.resolve(2);
|
||||
}
|
||||
|
||||
*test2(){
|
||||
yield 3;
|
||||
}
|
||||
}
|
||||
30
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/expected.js
vendored
Normal file
30
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/expected.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
var Example =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Example() {}
|
||||
|
||||
var _proto = Example.prototype;
|
||||
|
||||
_proto.test1 = async function test1() {
|
||||
await Promise.resolve(2);
|
||||
};
|
||||
|
||||
_proto.test2 =
|
||||
/*#__PURE__*/
|
||||
regeneratorRuntime.mark(function test2() {
|
||||
return regeneratorRuntime.wrap(function test2$(_context) {
|
||||
while (1) {
|
||||
switch (_context.prev = _context.next) {
|
||||
case 0:
|
||||
_context.next = 2;
|
||||
return 3;
|
||||
|
||||
case 2:
|
||||
case "end":
|
||||
return _context.stop();
|
||||
}
|
||||
}
|
||||
}, test2, this);
|
||||
});
|
||||
return Example;
|
||||
}();
|
||||
5
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/options.json
vendored
Normal file
5
packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/options.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"plugins": [
|
||||
["transform-classes", { "loose": true }]
|
||||
]
|
||||
}
|
||||
8
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/actual.js
vendored
Normal file
8
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/actual.js
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
constructor(val){
|
||||
this._val = val;
|
||||
}
|
||||
foo2(){
|
||||
return foo2(this._val);
|
||||
}
|
||||
}
|
||||
26
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/expected.js
vendored
Normal file
26
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/expected.js
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Foo(val) {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
this._val = val;
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Foo, [{
|
||||
key: "foo2",
|
||||
value: function (_foo) {
|
||||
function foo2() {
|
||||
return _foo.apply(this, arguments);
|
||||
}
|
||||
|
||||
foo2.toString = function () {
|
||||
return _foo.toString();
|
||||
};
|
||||
|
||||
return foo2;
|
||||
}(function () {
|
||||
return foo2(this._val);
|
||||
})
|
||||
}]);
|
||||
return Foo;
|
||||
}();
|
||||
6
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/options.json
vendored
Normal file
6
packages/babel-plugin-transform-classes/test/fixtures/regression/T7010/options.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"transform-classes",
|
||||
"external-helpers"
|
||||
]
|
||||
}
|
||||
7
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/actual.js
vendored
Normal file
7
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/actual.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
class B{}
|
||||
class A extends B{
|
||||
constructor(track){
|
||||
if (track !== undefined) super(track);
|
||||
else super();
|
||||
}
|
||||
}
|
||||
30
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/expected.js
vendored
Normal file
30
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/expected.js
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
||||
|
||||
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var B = function B() {
|
||||
_classCallCheck(this, B);
|
||||
};
|
||||
|
||||
var A =
|
||||
/*#__PURE__*/
|
||||
function (_B) {
|
||||
_inherits(A, _B);
|
||||
|
||||
function A(track) {
|
||||
var _this;
|
||||
|
||||
_classCallCheck(this, A);
|
||||
|
||||
if (track !== undefined) _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this, track));else _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this));
|
||||
return _possibleConstructorReturn(_this);
|
||||
}
|
||||
|
||||
return A;
|
||||
}(B);
|
||||
4
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/options.json
vendored
Normal file
4
packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/options.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["transform-exponentiation-operator"],
|
||||
"presets": ["es2015"]
|
||||
}
|
||||
4
packages/babel-plugin-transform-classes/test/fixtures/regression/options.json
vendored
Normal file
4
packages/babel-plugin-transform-classes/test/fixtures/regression/options.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["external-helpers", "proposal-class-properties"],
|
||||
"presets": ["es2015", "react"]
|
||||
}
|
||||
25
packages/babel-plugin-transform-classes/test/fixtures/spec/accessing-super-class/actual.js
vendored
Normal file
25
packages/babel-plugin-transform-classes/test/fixtures/spec/accessing-super-class/actual.js
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
woops.super.test();
|
||||
super();
|
||||
super.test();
|
||||
|
||||
super(...arguments);
|
||||
super("test", ...arguments);
|
||||
|
||||
super.test(...arguments);
|
||||
super.test("test", ...arguments);
|
||||
}
|
||||
|
||||
test() {
|
||||
super.test();
|
||||
super.test(...arguments);
|
||||
super.test("test", ...arguments);
|
||||
}
|
||||
|
||||
static foo() {
|
||||
super.foo();
|
||||
super.foo(...arguments);
|
||||
super.foo("test", ...arguments);
|
||||
}
|
||||
}
|
||||
46
packages/babel-plugin-transform-classes/test/fixtures/spec/accessing-super-class/expected.js
vendored
Normal file
46
packages/babel-plugin-transform-classes/test/fixtures/spec/accessing-super-class/expected.js
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inherits(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _ref, _babelHelpers$get;
|
||||
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Test);
|
||||
woops.super.test();
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).apply(this, arguments));
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (_ref = Test.__proto__ || Object.getPrototypeOf(Test)).call.apply(_ref, [this, "test"].concat(Array.prototype.slice.call(arguments))));
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).apply(_this, arguments);
|
||||
|
||||
(_babelHelpers$get = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this)).call.apply(_babelHelpers$get, [_this, "test"].concat(Array.prototype.slice.call(arguments)));
|
||||
|
||||
return _this;
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Test, [{
|
||||
key: "test",
|
||||
value: function test() {
|
||||
var _babelHelpers$get2;
|
||||
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this).call(this);
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this).apply(this, arguments);
|
||||
|
||||
(_babelHelpers$get2 = babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_babelHelpers$get2, [this, "test"].concat(Array.prototype.slice.call(arguments)));
|
||||
}
|
||||
}], [{
|
||||
key: "foo",
|
||||
value: function foo() {
|
||||
var _babelHelpers$get3;
|
||||
|
||||
babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this).call(this);
|
||||
babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this).apply(this, arguments);
|
||||
|
||||
(_babelHelpers$get3 = babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "foo", this)).call.apply(_babelHelpers$get3, [this, "test"].concat(Array.prototype.slice.call(arguments)));
|
||||
}
|
||||
}]);
|
||||
return Test;
|
||||
}(Foo);
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
super.test;
|
||||
super.test.whatever;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inherits(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Test);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this);
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever;
|
||||
return _this;
|
||||
}
|
||||
|
||||
return Test;
|
||||
}(Foo);
|
||||
11
packages/babel-plugin-transform-classes/test/fixtures/spec/calling-super-properties/actual.js
vendored
Normal file
11
packages/babel-plugin-transform-classes/test/fixtures/spec/calling-super-properties/actual.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
class Test extends Foo {
|
||||
constructor() {
|
||||
super();
|
||||
super.test.whatever();
|
||||
super.test();
|
||||
}
|
||||
|
||||
static test() {
|
||||
return super.wow();
|
||||
}
|
||||
}
|
||||
23
packages/babel-plugin-transform-classes/test/fixtures/spec/calling-super-properties/expected.js
vendored
Normal file
23
packages/babel-plugin-transform-classes/test/fixtures/spec/calling-super-properties/expected.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
var Test =
|
||||
/*#__PURE__*/
|
||||
function (_Foo) {
|
||||
babelHelpers.inherits(Test, _Foo);
|
||||
|
||||
function Test() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Test);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Test.__proto__ || Object.getPrototypeOf(Test)).call(this));
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).whatever();
|
||||
babelHelpers.get(Test.prototype.__proto__ || Object.getPrototypeOf(Test.prototype), "test", _this).call(_this);
|
||||
return _this;
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Test, null, [{
|
||||
key: "test",
|
||||
value: function test() {
|
||||
return babelHelpers.get(Test.__proto__ || Object.getPrototypeOf(Test), "wow", this).call(this);
|
||||
}
|
||||
}]);
|
||||
return Test;
|
||||
}(Foo);
|
||||
6
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/actual.js
vendored
Normal file
6
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/actual.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
foo() {}
|
||||
"foo"() {}
|
||||
[bar]() {}
|
||||
[bar + "foo"]() {}
|
||||
}
|
||||
17
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/exec.js
vendored
Normal file
17
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/exec.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
const sym = Symbol();
|
||||
|
||||
class Foo {
|
||||
[sym] () {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Bar extends Foo {
|
||||
[sym] () {
|
||||
return super[sym]() + 2;
|
||||
}
|
||||
}
|
||||
|
||||
let i = new Bar();
|
||||
|
||||
assert.equal(i[sym](), 3);
|
||||
22
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/expected.js
vendored
Normal file
22
packages/babel-plugin-transform-classes/test/fixtures/spec/computed-methods/expected.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function Foo() {
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
}
|
||||
|
||||
babelHelpers.createClass(Foo, [{
|
||||
key: "foo",
|
||||
value: function foo() {}
|
||||
}, {
|
||||
key: "foo",
|
||||
value: function foo() {}
|
||||
}, {
|
||||
key: bar,
|
||||
value: function value() {}
|
||||
}, {
|
||||
key: bar + "foo",
|
||||
value: function value() {}
|
||||
}]);
|
||||
return Foo;
|
||||
}();
|
||||
@@ -0,0 +1,7 @@
|
||||
class Example {
|
||||
constructor() {
|
||||
var Example;
|
||||
}
|
||||
}
|
||||
|
||||
var t = new Example();
|
||||
@@ -0,0 +1,7 @@
|
||||
var Example = function Example() {
|
||||
babelHelpers.classCallCheck(this, Example);
|
||||
|
||||
var _Example;
|
||||
};
|
||||
|
||||
var t = new Example();
|
||||
21
packages/babel-plugin-transform-classes/test/fixtures/spec/constructor/actual.js
vendored
Normal file
21
packages/babel-plugin-transform-classes/test/fixtures/spec/constructor/actual.js
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
class Test {
|
||||
constructor() {
|
||||
this.state = "test";
|
||||
}
|
||||
}
|
||||
|
||||
class Foo extends Bar {
|
||||
constructor() {
|
||||
super();
|
||||
this.state = "test";
|
||||
}
|
||||
}
|
||||
|
||||
class ConstructorScoping {
|
||||
constructor(){
|
||||
let bar;
|
||||
{
|
||||
let bar;
|
||||
}
|
||||
}
|
||||
}
|
||||
29
packages/babel-plugin-transform-classes/test/fixtures/spec/constructor/expected.js
vendored
Normal file
29
packages/babel-plugin-transform-classes/test/fixtures/spec/constructor/expected.js
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
var Test = function Test() {
|
||||
babelHelpers.classCallCheck(this, Test);
|
||||
this.state = "test";
|
||||
};
|
||||
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
babelHelpers.inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
_this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this));
|
||||
_this.state = "test";
|
||||
return _this;
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
|
||||
var ConstructorScoping = function ConstructorScoping() {
|
||||
babelHelpers.classCallCheck(this, ConstructorScoping);
|
||||
var bar;
|
||||
{
|
||||
var _bar;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo extends Bar {
|
||||
constructor() {
|
||||
super(() => {
|
||||
this.test;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
var Foo =
|
||||
/*#__PURE__*/
|
||||
function (_Bar) {
|
||||
babelHelpers.inherits(Foo, _Bar);
|
||||
|
||||
function Foo() {
|
||||
var _this;
|
||||
|
||||
babelHelpers.classCallCheck(this, Foo);
|
||||
return _this = babelHelpers.possibleConstructorReturn(this, (Foo.__proto__ || Object.getPrototypeOf(Foo)).call(this, () => {
|
||||
_this.test;
|
||||
}));
|
||||
}
|
||||
|
||||
return Foo;
|
||||
}(Bar);
|
||||
@@ -0,0 +1,5 @@
|
||||
class Foo extends Bar {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"throws": "missing super() call in constructor",
|
||||
"plugins": ["transform-classes"]
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user