JeromeFitz ae168edcfa Remove babel-messages (#6347), continuation of #6352
package.json "babel-messages" removed:
- babel-core
- babel-helper-replace-supers
- babel-plugin-transform-es2015-classes
- babel-traverse

"messages" remove from:
- babel-plugin-check-es2015-constants/src/index.js
- babel-plugin-transform-es2015-for-of/src/index.js

export "babel-messages" removed from:
- babel-core/src/index.js

import "babel-messages" removed from:
- babel-generator/src/index.js
- babel-helper-replace-supers/src/index.js
- babel-traverse/src/index.js
- babel-traverse/src/scope/index.js
- babel-traverse/src/visitors.js

package "babel-messages" removed completely.

💯️ All tests pass.
2017-09-30 09:31:53 -04:00

53 lines
1.6 KiB
JavaScript

export default function({ types: t }) {
/**
* Helper function to run a statement before an expression by replacing it with a comma expression
* and wrapping the statement in an IIFE as the first operand.
*/
function statementBeforeExpression(statement, expression) {
return t.sequenceExpression([
t.callExpression(
t.functionExpression(null, [], t.blockStatement([statement])),
[],
),
expression,
]);
}
return {
visitor: {
Scope({ scope }) {
for (const name in scope.bindings) {
const binding = scope.bindings[name];
if (binding.kind !== "const") continue;
for (const violation of (binding.constantViolations: Array)) {
const throwNode = t.throwStatement(
t.newExpression(t.identifier("Error"), [
t.stringLiteral(`"${name}" is read-only`),
]),
);
if (violation.isAssignmentExpression()) {
violation
.get("right")
.replaceWith(
statementBeforeExpression(
throwNode,
violation.get("right").node,
),
);
} else if (violation.isUpdateExpression()) {
violation.replaceWith(
statementBeforeExpression(throwNode, violation.node),
);
} else if (violation.isForXStatement()) {
violation.ensureBlock();
violation.node.body.body.unshift(throwNode);
}
}
}
},
},
};
}