From ea0e55c5af5b1ad164b791a52074c26f6e30be29 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Sat, 7 Nov 2015 14:44:47 -0800 Subject: [PATCH 1/9] Add failing test --- .../es6.block-scoping/for-const-closure/actual.js | 6 ++++++ .../es6.block-scoping/for-const-closure/expected.js | 10 ++++++++++ 2 files changed, 16 insertions(+) create mode 100644 packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js create mode 100644 packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js diff --git a/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js b/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js new file mode 100644 index 0000000000..55dbb21d37 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js @@ -0,0 +1,6 @@ +for (let i = 0; i < 5; i++) { + const l = i; + setTimeout(function() { + console.log(l); + }, 1); +} diff --git a/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js b/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js new file mode 100644 index 0000000000..2b43aae3b3 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js @@ -0,0 +1,10 @@ +var _loop = function _loop(i) { + var l = i; + setTimeout(function () { + console.log(l); + }, 1); +}; + +for (var i = 0; i < 5; i++) { + _loop(i); +} From cd6a972342d9f8ed3240383f25e0e11a7716b0c5 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 14:28:22 -0800 Subject: [PATCH 2/9] move test files --- .../for-const-closure/actual.js | 0 .../for-const-closure/expected.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/{babel-core/test/fixtures/transformation/es6.block-scoping => babel-plugin-transform-es2015-block-scoping}/for-const-closure/actual.js (100%) rename packages/{babel-core/test/fixtures/transformation/es6.block-scoping => babel-plugin-transform-es2015-block-scoping}/for-const-closure/expected.js (100%) diff --git a/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/actual.js similarity index 100% rename from packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/actual.js rename to packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/actual.js diff --git a/packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/expected.js similarity index 100% rename from packages/babel-core/test/fixtures/transformation/es6.block-scoping/for-const-closure/expected.js rename to packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/expected.js From 4cca0abd2f1b4a549a6aba9f5b0aa06df4e97f84 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 14:30:21 -0800 Subject: [PATCH 3/9] move them to the correct place --- .../{ => test/fixtures/general}/for-const-closure/actual.js | 0 .../{ => test/fixtures/general}/for-const-closure/expected.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-plugin-transform-es2015-block-scoping/{ => test/fixtures/general}/for-const-closure/actual.js (100%) rename packages/babel-plugin-transform-es2015-block-scoping/{ => test/fixtures/general}/for-const-closure/expected.js (100%) diff --git a/packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/actual.js rename to packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/actual.js diff --git a/packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-block-scoping/for-const-closure/expected.js rename to packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js From aa8b96bf38894400ae1e26794efd5b0c14184bd5 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 15:55:33 -0800 Subject: [PATCH 4/9] Support consts in block-scoping transform --- .../src/index.js | 34 +++++++++---------- .../general/assignment-patterns/expected.js | 2 +- .../general/for-const-closure/expected.js | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index 4354217f98..3f676459f0 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -13,9 +13,10 @@ export default function () { visitor: { VariableDeclaration(path, file) { let { node, parent, scope } = path; - if (!isLet(node, parent, scope)) return; + if (!isBlockScoped(node)) return; + convertBlockScopedToVar(node, parent, scope); - if (isLetInitable(node) && node._tdzThis) { + if (node._tdzThis) { let nodes = [node]; for (let i = 0; i < node.declarations.length; i++) { @@ -44,7 +45,7 @@ export default function () { let { node, parent, scope } = path; let init = node.left || node.init; - if (isLet(init, node, scope)) { + if (isBlockScoped(init)) { t.ensureBlock(node); node.body._letDeclarators = [init]; } @@ -68,13 +69,16 @@ let buildRetCheck = template(` if (typeof RETURN === "object") return RETURN.v; `); -function isLet(node, parent, scope) { +function isBlockScoped(node) { if (!t.isVariableDeclaration(node)) return false; if (node._let) return true; - if (node.kind !== "let") return false; + if (node.kind !== "let" && node.kind !== "const") return false; + return true; +} +function convertBlockScopedToVar(node, parent, scope) { // https://github.com/babel/babel/issues/255 - if (isLetInitable(node, parent)) { + if (!t.isFor(parent)) { for (let i = 0; i < node.declarations.length; i++) { let declar = node.declarations[i]; declar.init = declar.init || scope.buildUndefinedNode(); @@ -83,15 +87,10 @@ function isLet(node, parent, scope) { node._let = true; node.kind = "var"; - return true; } -function isLetInitable(node, parent) { - return !t.isFor(parent) || !t.isFor(parent, { left: node }); -} - -function isVar(node, parent, scope) { - return t.isVariableDeclaration(node, { kind: "var" }) && !isLet(node, parent, scope); +function isVar(node, parent) { + return t.isVariableDeclaration(node, { kind: "var" }) && !isBlockScoped(node, parent); } function standardizeLets(declars) { @@ -170,7 +169,7 @@ let hoistVarDeclarationsVisitor = { let { node, parent, scope } = path; if (path.isForStatement()) { - if (isVar(node.init, node, scope)) { + if (isVar(node.init, node)) { let nodes = self.pushDeclar(node.init); if (nodes.length === 1) { node.init = nodes[0]; @@ -179,11 +178,11 @@ let hoistVarDeclarationsVisitor = { } } } else if (path.isFor()) { - if (isVar(node.left, node, scope)) { + if (isVar(node.left, node)) { self.pushDeclar(node.left); node.left = node.left.declarations[0].id; } - } else if (isVar(node, parent, scope)) { + } else if (isVar(node, parent)) { path.replaceWithMultiple(self.pushDeclar(node).map(expr => t.expressionStatement(expr))); } else if (path.isFunction()) { return path.skip(); @@ -510,7 +509,8 @@ class BlockScoping { if (block.body) { for (let i = 0; i < block.body.length; i++) { let declar = block.body[i]; - if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isLet(declar, block, this.scope)) { + if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) { + if (isBlockScoped(declar)) convertBlockScopedToVar(declar, block, this.scope); declarators = declarators.concat(declar.declarations || declar); } } diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/assignment-patterns/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/assignment-patterns/expected.js index b497a041e4..41c81d3bb4 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/assignment-patterns/expected.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/assignment-patterns/expected.js @@ -1,4 +1,4 @@ -const foo = "foo"; +var foo = "foo"; function foobar() { for (var item of [1, 2, 3]) { diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js index 2b43aae3b3..5b83611a8b 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/for-const-closure/expected.js @@ -1,4 +1,4 @@ -var _loop = function _loop(i) { +var _loop = function (i) { var l = i; setTimeout(function () { console.log(l); From ed4a5fb811648ac244d8ff15b601e8c8e6b4d86f Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 16:44:28 -0800 Subject: [PATCH 5/9] get rid of _let --- .../src/index.js | 16 ++++------------ .../rest-binding-deoptimisation/expected.js | 2 +- packages/babel-types/src/constants.js | 2 ++ packages/babel-types/src/validators.js | 5 +++-- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index 3f676459f0..a0bffa579a 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -71,7 +71,7 @@ let buildRetCheck = template(` function isBlockScoped(node) { if (!t.isVariableDeclaration(node)) return false; - if (node._let) return true; + if (node[t.BLOCK_SCOPED_SYMBOL]) return true; if (node.kind !== "let" && node.kind !== "const") return false; return true; } @@ -85,18 +85,13 @@ function convertBlockScopedToVar(node, parent, scope) { } } + node[t.BLOCK_SCOPED_SYMBOL] = true; node._let = true; node.kind = "var"; } -function isVar(node, parent) { - return t.isVariableDeclaration(node, { kind: "var" }) && !isBlockScoped(node, parent); -} - -function standardizeLets(declars) { - for (let declar of (declars: Array)) { - delete declar._let; - } +function isVar(node) { + return t.isVariableDeclaration(node, { kind: "var" }) && !isBlockScoped(node); } function replace(path, node, scope, remaps) { @@ -527,9 +522,6 @@ class BlockScoping { // no let references so we can just quit if (!this.hasLetReferences) return; - // set let references to plain let references - standardizeLets(declarators); - let state = { letReferences: this.letReferences, closurify: false, diff --git a/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-binding-deoptimisation/expected.js b/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-binding-deoptimisation/expected.js index 80224dbe80..af7c898bdc 100644 --- a/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-binding-deoptimisation/expected.js +++ b/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-binding-deoptimisation/expected.js @@ -1,4 +1,4 @@ -const deepAssign = function () { +var deepAssign = function () { for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } diff --git a/packages/babel-types/src/constants.js b/packages/babel-types/src/constants.js index 7cccc83192..8d764200c1 100644 --- a/packages/babel-types/src/constants.js +++ b/packages/babel-types/src/constants.js @@ -22,3 +22,5 @@ export const INHERIT_KEYS = { optional: ["typeAnnotation", "typeParameters", "returnType"], force: ["start", "loc", "end"] }; + +export const BLOCK_SCOPED_SYMBOL = Symbol("var used to be block scoped"); diff --git a/packages/babel-types/src/validators.js b/packages/babel-types/src/validators.js index 6460b9ae1f..019cec3f0b 100644 --- a/packages/babel-types/src/validators.js +++ b/packages/babel-types/src/validators.js @@ -3,6 +3,7 @@ import { getBindingIdentifiers } from "./retrievers"; import esutils from "esutils"; import * as t from "./index"; +import { BLOCK_SCOPED_SYMBOL } from "./constants"; /** * Check if the input `node` is a binding identifier. @@ -169,7 +170,7 @@ export function isValidIdentifier(name: string): boolean { */ export function isLet(node: Object): boolean { - return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let); + return t.isVariableDeclaration(node) && (node.kind !== "var" || node[BLOCK_SCOPED_SYMBOL]); } /** @@ -185,7 +186,7 @@ export function isBlockScoped(node: Object): boolean { */ export function isVar(node: Object): boolean { - return t.isVariableDeclaration(node, { kind: "var" }) && !node._let; + return t.isVariableDeclaration(node, { kind: "var" }) && !node[BLOCK_SCOPED_SYMBOL]; } /** From 8891ed38b8da4685acd271bcc8724a10b40f1453 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 17:19:30 -0800 Subject: [PATCH 6/9] cleanup --- .../src/index.js | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index a0bffa579a..759eb9c62f 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -42,14 +42,7 @@ export default function () { }, Loop(path, file) { - let { node, parent, scope } = path; - - let init = node.left || node.init; - if (isBlockScoped(init)) { - t.ensureBlock(node); - node.body._letDeclarators = [init]; - } - + let { parent, scope } = path; let blockScoping = new BlockScoping(path, path.get("body"), parent, scope, file); let replace = blockScoping.run(); if (replace) path.replaceWith(replace); @@ -161,7 +154,7 @@ let letReferenceFunctionVisitor = traverse.visitors.merge([{ let hoistVarDeclarationsVisitor = { enter(path, self) { - let { node, parent, scope } = path; + let { node, parent } = path; if (path.isForStatement()) { if (isVar(node.init, node)) { @@ -492,12 +485,14 @@ class BlockScoping { getLetReferences() { let block = this.block; - let declarators = block._letDeclarators || []; + let declarators = []; - // - for (let i = 0; i < declarators.length; i++) { - let declar = declarators[i]; - extend(this.outsideLetReferences, t.getBindingIdentifiers(declar)); + if (this.loop) { + let init = this.loop.left || this.loop.init; + if (isBlockScoped(init)) { + declarators.push(init); + extend(this.outsideLetReferences, t.getBindingIdentifiers(init)); + } } // From 0711fa67b434baa73984f19943cb15500641786e Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 17:34:46 -0800 Subject: [PATCH 7/9] ensure block --- .../babel-plugin-transform-es2015-block-scoping/src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index 759eb9c62f..f0347595d6 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -42,7 +42,8 @@ export default function () { }, Loop(path, file) { - let { parent, scope } = path; + let { node, parent, scope } = path; + t.ensureBlock(node); let blockScoping = new BlockScoping(path, path.get("body"), parent, scope, file); let replace = blockScoping.run(); if (replace) path.replaceWith(replace); @@ -79,7 +80,6 @@ function convertBlockScopedToVar(node, parent, scope) { } node[t.BLOCK_SCOPED_SYMBOL] = true; - node._let = true; node.kind = "var"; } From a17d62ca2b86010071ee20e768df7625370180ea Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 10 Nov 2015 17:45:29 -0800 Subject: [PATCH 8/9] Convert the constants transform plugin to a validation plugin A follow up from https://github.com/babel/babel/pull/2969 were we added support for consts in the block-scoping plugin. --- .../.npmignore | 0 .../README.md | 39 +++++++++++++++++++ .../package.json | 4 +- .../src/index.js | 16 ++++++++ .../fixtures/general/block-scoped/exec.js | 0 .../general/block-statement/actual.js | 0 .../general/block-statement/expected.js | 0 .../destructuring-assignment/actual.js | 0 .../destructuring-assignment/options.json | 0 .../fixtures/general/destructuring/actual.js | 0 .../general/destructuring/expected.js | 0 .../ignore-member-expressions/actual.js | 0 .../ignore-member-expressions/expected.js | 0 .../test/fixtures/general/loop/actual.js | 0 .../test/fixtures/general/loop/options.json | 0 .../fixtures/general/no-assignment/actual.js | 0 .../general/no-assignment/options.json | 0 .../fixtures/general/no-classes/actual.js | 0 .../fixtures/general/no-classes/options.json | 0 .../fixtures/general/no-declaration/actual.js | 0 .../general/no-declaration/options.json | 0 .../test/fixtures/general/no-for-in/actual.js | 0 .../fixtures/general/no-for-in/options.json | 0 .../fixtures/general/no-functions/actual.js | 0 .../general/no-functions/options.json | 0 .../test/fixtures/general/program/actual.js | 0 .../test/fixtures/general/program/expected.js | 0 .../general/update-expression/actual.js | 0 .../general/update-expression/expected.js | 0 .../general/update-expression/options.json | 0 .../test/fixtures/options.json | 3 ++ .../test/index.js | 0 .../test/fixtures/pass/options.json | 2 +- .../README.md | 39 ------------------- .../src/index.js | 38 ------------------ .../test/fixtures/options.json | 3 -- packages/babel-preset-es2015/index.js | 2 +- packages/babel-preset-es2015/package.json | 2 +- .../test/fixtures/traceur/options.json | 2 +- 39 files changed, 64 insertions(+), 86 deletions(-) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/.npmignore (100%) create mode 100644 packages/babel-plugin-check-es2015-constants/README.md rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/package.json (78%) create mode 100644 packages/babel-plugin-check-es2015-constants/src/index.js rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/block-scoped/exec.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/block-statement/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/block-statement/expected.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/destructuring-assignment/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/destructuring-assignment/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/destructuring/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/destructuring/expected.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/ignore-member-expressions/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/ignore-member-expressions/expected.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/loop/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/loop/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-assignment/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-assignment/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-classes/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-classes/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-declaration/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-declaration/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-for-in/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-for-in/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-functions/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/no-functions/options.json (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/program/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/program/expected.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/update-expression/actual.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/update-expression/expected.js (100%) rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/fixtures/general/update-expression/options.json (100%) create mode 100644 packages/babel-plugin-check-es2015-constants/test/fixtures/options.json rename packages/{babel-plugin-transform-es2015-constants => babel-plugin-check-es2015-constants}/test/index.js (100%) delete mode 100644 packages/babel-plugin-transform-es2015-constants/README.md delete mode 100644 packages/babel-plugin-transform-es2015-constants/src/index.js delete mode 100644 packages/babel-plugin-transform-es2015-constants/test/fixtures/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/.npmignore b/packages/babel-plugin-check-es2015-constants/.npmignore similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/.npmignore rename to packages/babel-plugin-check-es2015-constants/.npmignore diff --git a/packages/babel-plugin-check-es2015-constants/README.md b/packages/babel-plugin-check-es2015-constants/README.md new file mode 100644 index 0000000000..0bd2c3b4ab --- /dev/null +++ b/packages/babel-plugin-check-es2015-constants/README.md @@ -0,0 +1,39 @@ +# babel-plugin-check-es2015-constants + +Validate ES2015 constants + +## Installation + +```sh +$ npm install babel-plugin-check-es2015-constants +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["check-es2015-constants"] +} +``` + +### Via CLI + +```sh +$ babel --plugins check-es2015-constants script.js +``` + +### Via Node API + +```javascript +require("babel-core").transform("code", { + plugins: ["check-es2015-constants"] +}); +``` + +## Note + +This check will only validate consts. If you need it to compile down to `var` then you must also install and enable [`check-es2015-block-scoping`](../babel-plugin-check-es2015-block-scoping). diff --git a/packages/babel-plugin-transform-es2015-constants/package.json b/packages/babel-plugin-check-es2015-constants/package.json similarity index 78% rename from packages/babel-plugin-transform-es2015-constants/package.json rename to packages/babel-plugin-check-es2015-constants/package.json index 85fb9e267b..4942be62d0 100644 --- a/packages/babel-plugin-transform-es2015-constants/package.json +++ b/packages/babel-plugin-check-es2015-constants/package.json @@ -1,8 +1,8 @@ { - "name": "babel-plugin-transform-es2015-constants", + "name": "babel-plugin-check-es2015-constants", "version": "6.1.4", "description": "Compile ES2015 constants to ES5", - "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-constants", + "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-check-es2015-constants", "license": "MIT", "main": "lib/index.js", "keywords": [ diff --git a/packages/babel-plugin-check-es2015-constants/src/index.js b/packages/babel-plugin-check-es2015-constants/src/index.js new file mode 100644 index 0000000000..4269b0385c --- /dev/null +++ b/packages/babel-plugin-check-es2015-constants/src/index.js @@ -0,0 +1,16 @@ +export default function ({ messages }) { + return { + visitor: { + Scope({ scope }) { + for (let name in scope.bindings) { + let binding = scope.bindings[name]; + if (binding.kind !== "const" && binding.kind !== "module") continue; + + for (let violation of (binding.constantViolations: Array)) { + throw violation.buildCodeFrameError(messages.get("readOnly", name)); + } + } + }, + } + }; +} diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-scoped/exec.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-scoped/exec.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-scoped/exec.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-scoped/exec.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-statement/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-statement/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-statement/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-statement/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-statement/expected.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-statement/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/block-statement/expected.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/block-statement/expected.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring-assignment/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring-assignment/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring-assignment/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring-assignment/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring-assignment/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring-assignment/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring-assignment/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring-assignment/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring/expected.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/destructuring/expected.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/destructuring/expected.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/ignore-member-expressions/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/ignore-member-expressions/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/ignore-member-expressions/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/ignore-member-expressions/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/ignore-member-expressions/expected.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/ignore-member-expressions/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/ignore-member-expressions/expected.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/ignore-member-expressions/expected.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/loop/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/loop/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/loop/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/loop/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/loop/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/loop/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/loop/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/loop/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-assignment/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-assignment/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-assignment/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-assignment/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-assignment/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-assignment/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-assignment/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-assignment/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-classes/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-classes/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-classes/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-classes/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-classes/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-classes/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-classes/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-classes/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-declaration/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-declaration/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-declaration/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-declaration/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-declaration/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-declaration/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-declaration/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-declaration/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-for-in/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-for-in/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-for-in/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-for-in/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-for-in/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-for-in/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-for-in/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-for-in/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-functions/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-functions/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-functions/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-functions/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-functions/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-functions/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/no-functions/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/no-functions/options.json diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/program/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/program/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/program/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/program/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/program/expected.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/program/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/program/expected.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/program/expected.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/actual.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/actual.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/actual.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/expected.js b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/expected.js rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/expected.js diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/options.json similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/fixtures/general/update-expression/options.json rename to packages/babel-plugin-check-es2015-constants/test/fixtures/general/update-expression/options.json diff --git a/packages/babel-plugin-check-es2015-constants/test/fixtures/options.json b/packages/babel-plugin-check-es2015-constants/test/fixtures/options.json new file mode 100644 index 0000000000..c0e516303a --- /dev/null +++ b/packages/babel-plugin-check-es2015-constants/test/fixtures/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["check-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-destructuring"] +} diff --git a/packages/babel-plugin-transform-es2015-constants/test/index.js b/packages/babel-plugin-check-es2015-constants/test/index.js similarity index 100% rename from packages/babel-plugin-transform-es2015-constants/test/index.js rename to packages/babel-plugin-check-es2015-constants/test/index.js diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/pass/options.json b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/pass/options.json index 7c2bea4aad..a0ae43ffad 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/pass/options.json +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/pass/options.json @@ -1,3 +1,3 @@ { - "plugins": ["transform-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-parameters", "transform-es2015-destructuring", "transform-es2015-modules-commonjs"] + "plugins": ["check-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-parameters", "transform-es2015-destructuring", "transform-es2015-modules-commonjs"] } diff --git a/packages/babel-plugin-transform-es2015-constants/README.md b/packages/babel-plugin-transform-es2015-constants/README.md deleted file mode 100644 index 33c2610514..0000000000 --- a/packages/babel-plugin-transform-es2015-constants/README.md +++ /dev/null @@ -1,39 +0,0 @@ -# babel-plugin-transform-es2015-constants - -Compile ES2015 constants to ES5 - -## Installation - -```sh -$ npm install babel-plugin-transform-es2015-constants -``` - -## Usage - -### Via `.babelrc` (Recommended) - -**.babelrc** - -```json -{ - "plugins": ["transform-es2015-constants"] -} -``` - -### Via CLI - -```sh -$ babel --plugins transform-es2015-constants script.js -``` - -### Via Node API - -```javascript -require("babel-core").transform("code", { - plugins: ["transform-es2015-constants"] -}); -``` - -## Note - -This transform on its own will compile `const` to `let`. If you need it to compile down to `var` then you must also install and enable [`transform-es2015-block-scoping`](../babel-plugin-transform-es2015-block-scoping). diff --git a/packages/babel-plugin-transform-es2015-constants/src/index.js b/packages/babel-plugin-transform-es2015-constants/src/index.js deleted file mode 100644 index 7970a0ad89..0000000000 --- a/packages/babel-plugin-transform-es2015-constants/src/index.js +++ /dev/null @@ -1,38 +0,0 @@ -export default function ({ messages, types: t }) { - function check(node) { - if (t.isVariableDeclaration(node, { kind: "const" })) { - node.kind = "let"; - } - } - - return { - visitor: { - Scope({ scope }) { - for (let name in scope.bindings) { - let binding = scope.bindings[name]; - if (binding.kind !== "const" && binding.kind !== "module") continue; - - for (let violation of (binding.constantViolations: Array)) { - throw violation.buildCodeFrameError(messages.get("readOnly", name)); - } - } - }, - - VariableDeclaration({ node }) { - check(node); - }, - - ForXStatement({ node: { left } }) { - check(left); - }, - - ForStatement({ node: { init } }) { - check(init); - }, - - "BlockStatement|Program"({ node: { body } }) { - for (let node of body) check(node); - } - } - }; -} diff --git a/packages/babel-plugin-transform-es2015-constants/test/fixtures/options.json b/packages/babel-plugin-transform-es2015-constants/test/fixtures/options.json deleted file mode 100644 index b4063f1b64..0000000000 --- a/packages/babel-plugin-transform-es2015-constants/test/fixtures/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["transform-es2015-constants", "transform-es2015-block-scoping", "transform-es2015-destructuring"] -} diff --git a/packages/babel-preset-es2015/index.js b/packages/babel-preset-es2015/index.js index d30d1461ad..f8fdb617e4 100644 --- a/packages/babel-preset-es2015/index.js +++ b/packages/babel-preset-es2015/index.js @@ -12,7 +12,7 @@ module.exports = { require("babel-plugin-transform-es2015-for-of"), require("babel-plugin-transform-es2015-sticky-regex"), require("babel-plugin-transform-es2015-unicode-regex"), - require("babel-plugin-transform-es2015-constants"), + require("babel-plugin-check-es2015-constants"), require("babel-plugin-transform-es2015-spread"), require("babel-plugin-transform-es2015-parameters"), require("babel-plugin-transform-es2015-destructuring"), diff --git a/packages/babel-preset-es2015/package.json b/packages/babel-preset-es2015/package.json index d1aac6653f..b8e59870b7 100644 --- a/packages/babel-preset-es2015/package.json +++ b/packages/babel-preset-es2015/package.json @@ -20,7 +20,7 @@ "babel-plugin-transform-es2015-for-of": "^6.1.4", "babel-plugin-transform-es2015-sticky-regex": "^6.1.4", "babel-plugin-transform-es2015-unicode-regex": "^6.1.4", - "babel-plugin-transform-es2015-constants": "^6.1.4", + "babel-plugin-check-es2015-constants": "^6.1.4", "babel-plugin-transform-es2015-spread": "^6.1.4", "babel-plugin-transform-es2015-parameters": "^6.1.4", "babel-plugin-transform-es2015-destructuring": "^6.1.4", diff --git a/packages/babel-preset-es2015/test/fixtures/traceur/options.json b/packages/babel-preset-es2015/test/fixtures/traceur/options.json index 668b348e59..f549ded098 100644 --- a/packages/babel-preset-es2015/test/fixtures/traceur/options.json +++ b/packages/babel-preset-es2015/test/fixtures/traceur/options.json @@ -14,7 +14,7 @@ "transform-es2015-for-of", "transform-es2015-sticky-regex", "transform-es2015-unicode-regex", - "transform-es2015-constants", + "check-es2015-constants", "transform-es2015-spread", "transform-es2015-parameters", "transform-es2015-destructuring", From 6825846c8248df079ba532b9f0312ceb50c671d1 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Wed, 11 Nov 2015 11:06:47 -0800 Subject: [PATCH 9/9] Use Symbol.for --- packages/babel-types/src/constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-types/src/constants.js b/packages/babel-types/src/constants.js index 8d764200c1..275b6cfcf5 100644 --- a/packages/babel-types/src/constants.js +++ b/packages/babel-types/src/constants.js @@ -23,4 +23,4 @@ export const INHERIT_KEYS = { force: ["start", "loc", "end"] }; -export const BLOCK_SCOPED_SYMBOL = Symbol("var used to be block scoped"); +export const BLOCK_SCOPED_SYMBOL = Symbol.for("var used to be block scoped");