Remove check-constants plugin (#6987)
* Rebased onto new version * Moved constants check into a separate method
This commit is contained in:
parent
fa5eb4f605
commit
92fc26d399
@ -1,3 +0,0 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
@ -1,58 +0,0 @@
|
||||
# @babel/plugin-check-constants
|
||||
|
||||
> Validate ES2015 constants (prevents reassignment of const variables).
|
||||
|
||||
## Example
|
||||
|
||||
**In**
|
||||
|
||||
```js
|
||||
const a = 1;
|
||||
a = 2;
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```bash
|
||||
repl: "a" is read-only
|
||||
1 | const a = 1;
|
||||
> 2 | a = 2;
|
||||
| ^
|
||||
```
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-check-constants
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["@babel/check-constants"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins @babel/check-constants script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("@babel/core").transform("code", {
|
||||
plugins: ["@babel/check-constants"]
|
||||
});
|
||||
```
|
||||
|
||||
## Note
|
||||
|
||||
This check will only validate consts. If you need it to compile down to `var` then you must also install and enable [`@babel/plugin-transform-block-scoping`](http://babeljs.io/docs/plugins/transform-block-scoping/).
|
||||
@ -1,18 +0,0 @@
|
||||
{
|
||||
"name": "@babel/plugin-check-constants",
|
||||
"version": "7.0.0-beta.38",
|
||||
"description": "Compile ES2015 constants to ES5",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-check-constants",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.38"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.38",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.38"
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
import { types as t } from "@babel/core";
|
||||
|
||||
export default function() {
|
||||
return {
|
||||
visitor: {
|
||||
Scope({ scope }, state) {
|
||||
for (const name in scope.bindings) {
|
||||
const binding = scope.bindings[name];
|
||||
if (binding.kind !== "const") continue;
|
||||
|
||||
for (const violation of (binding.constantViolations: Array)) {
|
||||
const readOnlyError = state.addHelper("readOnlyError");
|
||||
const throwNode = t.callExpression(readOnlyError, [
|
||||
t.stringLiteral(name),
|
||||
]);
|
||||
|
||||
if (violation.isAssignmentExpression()) {
|
||||
violation
|
||||
.get("right")
|
||||
.replaceWith(
|
||||
t.sequenceExpression([
|
||||
throwNode,
|
||||
violation.get("right").node,
|
||||
]),
|
||||
);
|
||||
} else if (violation.isUpdateExpression()) {
|
||||
violation.replaceWith(
|
||||
t.sequenceExpression([throwNode, violation.node]),
|
||||
);
|
||||
} else if (violation.isForXStatement()) {
|
||||
violation.ensureBlock();
|
||||
violation.node.body.body.unshift(
|
||||
t.expressionStatement(throwNode),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["check-constants", "transform-block-scoping", "transform-flow-strip-types"]
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
{
|
||||
"plugins": ["check-constants", "transform-block-scoping", "transform-destructuring"]
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
@ -48,7 +48,7 @@ export default function(api, opts) {
|
||||
}
|
||||
},
|
||||
|
||||
Loop(path) {
|
||||
Loop(path, state) {
|
||||
const { parent, scope } = path;
|
||||
path.ensureBlock();
|
||||
const blockScoping = new BlockScoping(
|
||||
@ -58,12 +58,13 @@ export default function(api, opts) {
|
||||
scope,
|
||||
throwIfClosureRequired,
|
||||
tdzEnabled,
|
||||
state,
|
||||
);
|
||||
const replace = blockScoping.run();
|
||||
if (replace) path.replaceWith(replace);
|
||||
},
|
||||
|
||||
CatchClause(path) {
|
||||
CatchClause(path, state) {
|
||||
const { parent, scope } = path;
|
||||
const blockScoping = new BlockScoping(
|
||||
null,
|
||||
@ -72,11 +73,12 @@ export default function(api, opts) {
|
||||
scope,
|
||||
throwIfClosureRequired,
|
||||
tdzEnabled,
|
||||
state,
|
||||
);
|
||||
blockScoping.run();
|
||||
},
|
||||
|
||||
"BlockStatement|SwitchStatement|Program"(path) {
|
||||
"BlockStatement|SwitchStatement|Program"(path, state) {
|
||||
if (!ignoreBlock(path)) {
|
||||
const blockScoping = new BlockScoping(
|
||||
null,
|
||||
@ -85,6 +87,7 @@ export default function(api, opts) {
|
||||
path.scope,
|
||||
throwIfClosureRequired,
|
||||
tdzEnabled,
|
||||
state,
|
||||
);
|
||||
blockScoping.run();
|
||||
}
|
||||
@ -338,9 +341,11 @@ class BlockScoping {
|
||||
scope: Scope,
|
||||
throwIfClosureRequired: boolean,
|
||||
tdzEnabled: boolean,
|
||||
state: Object,
|
||||
) {
|
||||
this.parent = parent;
|
||||
this.scope = scope;
|
||||
this.state = state;
|
||||
this.throwIfClosureRequired = throwIfClosureRequired;
|
||||
this.tdzEnabled = tdzEnabled;
|
||||
|
||||
@ -372,6 +377,8 @@ class BlockScoping {
|
||||
|
||||
const needsClosure = this.getLetReferences();
|
||||
|
||||
this.checkConstants();
|
||||
|
||||
// this is a block within a `Function/Program` so we can safely leave it be
|
||||
if (t.isFunction(this.parent) || t.isProgram(this.block)) {
|
||||
this.updateScopeInfo();
|
||||
@ -394,8 +401,41 @@ class BlockScoping {
|
||||
}
|
||||
}
|
||||
|
||||
checkConstants() {
|
||||
const scope = this.scope;
|
||||
const state = this.state;
|
||||
|
||||
for (const name in scope.bindings) {
|
||||
const binding = scope.bindings[name];
|
||||
if (binding.kind !== "const") continue;
|
||||
|
||||
for (const violation of (binding.constantViolations: Array)) {
|
||||
const readOnlyError = state.addHelper("readOnlyError");
|
||||
const throwNode = t.callExpression(readOnlyError, [
|
||||
t.stringLiteral(name),
|
||||
]);
|
||||
|
||||
if (violation.isAssignmentExpression()) {
|
||||
violation
|
||||
.get("right")
|
||||
.replaceWith(
|
||||
t.sequenceExpression([throwNode, violation.get("right").node]),
|
||||
);
|
||||
} else if (violation.isUpdateExpression()) {
|
||||
violation.replaceWith(
|
||||
t.sequenceExpression([throwNode, violation.node]),
|
||||
);
|
||||
} else if (violation.isForXStatement()) {
|
||||
violation.ensureBlock();
|
||||
violation.node.body.body.unshift(t.expressionStatement(throwNode));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateScopeInfo(wrappedInClosure) {
|
||||
const scope = this.scope;
|
||||
|
||||
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
||||
const letRefs = this.letReferences;
|
||||
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-block-scoping", "transform-flow-strip-types"]
|
||||
}
|
||||
3
packages/babel-plugin-transform-block-scoping/test/fixtures/const-violations/options.json
vendored
Normal file
3
packages/babel-plugin-transform-block-scoping/test/fixtures/const-violations/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-block-scoping", "transform-destructuring"]
|
||||
}
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"plugins": ["check-constants", "transform-block-scoping", "transform-parameters", "transform-destructuring", "transform-modules-commonjs"]
|
||||
"plugins": ["transform-block-scoping", "transform-parameters", "transform-destructuring", "transform-modules-commonjs"]
|
||||
}
|
||||
|
||||
@ -1,9 +1,4 @@
|
||||
const es2015 = {
|
||||
"check-constants": {
|
||||
features: [
|
||||
"const",
|
||||
],
|
||||
},
|
||||
"transform-arrow-functions": {
|
||||
features: [
|
||||
"arrow functions",
|
||||
|
||||
@ -1,14 +1,4 @@
|
||||
{
|
||||
"check-constants": {
|
||||
"chrome": "49",
|
||||
"edge": "14",
|
||||
"firefox": "51",
|
||||
"safari": "10",
|
||||
"node": "6",
|
||||
"ios": "10",
|
||||
"opera": "36",
|
||||
"electron": "1"
|
||||
},
|
||||
"transform-arrow-functions": {
|
||||
"chrome": "47",
|
||||
"edge": "13",
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
"build-data": "node ./scripts/build-data.js; node ./scripts/build-modules-support.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/plugin-check-constants": "7.0.0-beta.38",
|
||||
"@babel/plugin-proposal-async-generator-functions": "7.0.0-beta.38",
|
||||
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.38",
|
||||
"@babel/plugin-proposal-optional-catch-binding": "7.0.0-beta.38",
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
export default {
|
||||
"check-constants": require("@babel/plugin-check-constants"),
|
||||
"syntax-async-generators": require("@babel/plugin-syntax-async-generators"),
|
||||
"syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"),
|
||||
"syntax-optional-catch-binding": require("@babel/plugin-syntax-optional-catch-binding"),
|
||||
|
||||
@ -8,7 +8,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "android":"4" }
|
||||
transform-arrow-functions { "android":"4" }
|
||||
transform-block-scoped-functions { "android":"4" }
|
||||
transform-block-scoping { "android":"4" }
|
||||
|
||||
@ -11,7 +11,6 @@ Using targets:
|
||||
Using modules transform: false
|
||||
|
||||
Using plugins:
|
||||
check-constants {}
|
||||
transform-arrow-functions {}
|
||||
transform-block-scoped-functions {}
|
||||
transform-block-scoping {}
|
||||
|
||||
@ -10,7 +10,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "ie":"10" }
|
||||
transform-arrow-functions { "ie":"10" }
|
||||
transform-block-scoped-functions { "ie":"10" }
|
||||
transform-block-scoping { "ie":"10" }
|
||||
|
||||
@ -15,7 +15,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "electron":"0.36" }
|
||||
transform-block-scoping { "electron":"0.36" }
|
||||
transform-destructuring { "electron":"0.36" }
|
||||
transform-for-of { "electron":"0.36" }
|
||||
|
||||
@ -8,7 +8,6 @@ Using targets:
|
||||
Using modules transform: false
|
||||
|
||||
Using plugins:
|
||||
check-constants {}
|
||||
transform-arrow-functions {}
|
||||
transform-block-scoped-functions {}
|
||||
transform-block-scoping {}
|
||||
|
||||
@ -6,7 +6,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants {}
|
||||
transform-arrow-functions {}
|
||||
transform-block-scoped-functions {}
|
||||
transform-block-scoping {}
|
||||
|
||||
@ -13,7 +13,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
|
||||
transform-arrow-functions { "ie":"10", "ios":"9", "safari":"7" }
|
||||
transform-block-scoped-functions { "ie":"10", "ios":"9", "safari":"7" }
|
||||
transform-block-scoping { "edge":"13", "firefox":"49", "ie":"10", "ios":"9", "safari":"7" }
|
||||
|
||||
@ -10,7 +10,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "firefox":"50", "ie":"11" }
|
||||
transform-arrow-functions { "ie":"11" }
|
||||
transform-block-scoping { "firefox":"50", "ie":"11" }
|
||||
transform-classes { "ie":"11" }
|
||||
|
||||
@ -10,7 +10,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "firefox":"50", "ie":"11" }
|
||||
transform-arrow-functions { "ie":"11" }
|
||||
transform-block-scoping { "firefox":"50", "ie":"11" }
|
||||
transform-classes { "ie":"11" }
|
||||
|
||||
@ -19,7 +19,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "electron":"0.36", "ie":"10" }
|
||||
transform-arrow-functions { "ie":"10" }
|
||||
transform-block-scoped-functions { "ie":"10" }
|
||||
transform-block-scoping { "electron":"0.36", "ie":"10" }
|
||||
|
||||
@ -10,7 +10,6 @@ Using targets:
|
||||
Using modules transform: commonjs
|
||||
|
||||
Using plugins:
|
||||
check-constants { "ie":"10" }
|
||||
transform-arrow-functions { "ie":"10" }
|
||||
transform-block-scoped-functions { "ie":"10" }
|
||||
transform-block-scoping { "ie":"10" }
|
||||
|
||||
@ -8,7 +8,6 @@
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-preset-es2015",
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@babel/plugin-check-constants": "7.0.0-beta.38",
|
||||
"@babel/plugin-transform-arrow-functions": "7.0.0-beta.38",
|
||||
"@babel/plugin-transform-block-scoped-functions": "7.0.0-beta.38",
|
||||
"@babel/plugin-transform-block-scoping": "7.0.0-beta.38",
|
||||
|
||||
@ -11,7 +11,6 @@ import transformES2015ComputedProperties from "@babel/plugin-transform-computed-
|
||||
import transformES2015ForOf from "@babel/plugin-transform-for-of";
|
||||
import transformES2015StickyRegex from "@babel/plugin-transform-sticky-regex";
|
||||
import transformES2015UnicodeRegex from "@babel/plugin-transform-unicode-regex";
|
||||
import checkES2015Constants from "@babel/plugin-check-constants";
|
||||
import transformES2015Spread from "@babel/plugin-transform-spread";
|
||||
import transformES2015Parameters from "@babel/plugin-transform-parameters";
|
||||
import transformES2015Destructuring from "@babel/plugin-transform-destructuring";
|
||||
@ -67,7 +66,6 @@ export default function(api, opts = {}) {
|
||||
[transformES2015ForOf, optsLoose],
|
||||
transformES2015StickyRegex,
|
||||
transformES2015UnicodeRegex,
|
||||
checkES2015Constants,
|
||||
[transformES2015Spread, optsLoose],
|
||||
[transformES2015Parameters, optsLoose],
|
||||
[transformES2015Destructuring, optsLoose],
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
{
|
||||
"plugins": [
|
||||
"check-constants",
|
||||
"transform-arrow-functions",
|
||||
"transform-block-scoped-functions",
|
||||
"transform-block-scoping",
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
"transform-for-of",
|
||||
"transform-sticky-regex",
|
||||
"transform-unicode-regex",
|
||||
"check-constants",
|
||||
"transform-spread",
|
||||
"transform-parameters",
|
||||
"transform-destructuring",
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
],
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.38",
|
||||
"@babel/plugin-check-constants": "7.0.0-beta.38",
|
||||
"@babel/plugin-external-helpers": "7.0.0-beta.38",
|
||||
"@babel/plugin-proposal-async-generator-functions": "7.0.0-beta.38",
|
||||
"@babel/plugin-proposal-class-properties": "7.0.0-beta.38",
|
||||
|
||||
@ -140,7 +140,6 @@ export function registerPresets(newPresets) {
|
||||
// Want to get rid of this long whitelist of plugins?
|
||||
// Wait! Please read https://github.com/babel/babel/pull/6177 first.
|
||||
registerPlugins({
|
||||
"check-constants": require("@babel/plugin-check-constants"),
|
||||
"external-helpers": require("@babel/plugin-external-helpers"),
|
||||
"syntax-async-generators": require("@babel/plugin-syntax-async-generators"),
|
||||
"syntax-class-properties": require("@babel/plugin-syntax-class-properties"),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user