diff --git a/packages/babel-plugin-transform-class-properties/src/index.js b/packages/babel-plugin-transform-class-properties/src/index.js index ad13630cb6..083b1f9dde 100644 --- a/packages/babel-plugin-transform-class-properties/src/index.js +++ b/packages/babel-plugin-transform-class-properties/src/index.js @@ -41,9 +41,9 @@ export default function ({ types: t }) { let nodes = []; let ref; - if (path.isClassExpression()) { - ref = path.scope.generateUidIdentifier(); - } else { // path.isClassDeclaration() + if (path.isClassExpression() || !path.node.id) { + ref = path.scope.generateUidIdentifier("class"); + } else { // path.isClassDeclaration() && path.node.id ref = path.node.id; } @@ -131,11 +131,16 @@ export default function ({ types: t }) { if (!nodes.length) return; if (path.isClassExpression()) { - nodes.push(t.expressionStatement(ref)); - } + path.scope.push({ id: ref }); + path.replaceWith(t.assignmentExpression("=", ref, path.node)); + } else { // path.isClassDeclaration() + if (!path.node.id) { + path.node.id = ref; + } - if (path.isClassDeclaration() && path.parentPath.isExportDeclaration()) { - path = path.parentPath; + if (path.parentPath.isExportDeclaration()) { + path = path.parentPath; + } } path.insertAfter(nodes); diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/actual.js b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/actual.js new file mode 100644 index 0000000000..2961c9a403 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/actual.js @@ -0,0 +1,7 @@ +call(class { + static test = true +}); + +export default class { + static test = true +}; diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/expected.js b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/expected.js new file mode 100644 index 0000000000..2b7ee5536f --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/T2983/expected.js @@ -0,0 +1,18 @@ +var _class, _temp; + +call((_temp = _class = (function () { + function _class2() { + babelHelpers.classCallCheck(this, _class2); + } + + return _class2; +})(), _class.test = true, _temp)); + +var _class3 = function _class3() { + babelHelpers.classCallCheck(this, _class3); +}; + +_class3.test = true; +export default _class3; +; + diff --git a/packages/babel-plugin-transform-class-properties/test/fixtures/regression/options.json b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/options.json new file mode 100644 index 0000000000..68b5f03d73 --- /dev/null +++ b/packages/babel-plugin-transform-class-properties/test/fixtures/regression/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers-2", "transform-class-properties", "transform-es2015-classes", "transform-es2015-block-scoping", "syntax-class-properties"] +} diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index 2f6da75192..e7702be6d8 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -224,7 +224,7 @@ export default class Scope { * Generate a unique identifier. */ - generateUidIdentifier(name: string) { + generateUidIdentifier(name: string = "temp") { return t.identifier(this.generateUid(name)); } @@ -232,7 +232,7 @@ export default class Scope { * Generate a unique `_id1` binding. */ - generateUid(name: string) { + generateUid(name: string = "temp") { name = t.toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, ""); let uid;