Add plugins name (#8769)

* Add plugins name

* Add missing names found by the plugin

* Add eslint plugin
This commit is contained in:
Nicolò Ribaudo 2018-11-18 23:02:58 +01:00 committed by GitHub
parent 88696601e1
commit 8c7d4b55c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
94 changed files with 431 additions and 163 deletions

View File

@ -26,6 +26,13 @@
"env": { "env": {
"jest": true "jest": true
} }
},
{
"files": ["packages/babel-plugin-*/src/index.js"],
"excludedFiles": ["packages/babel-plugin-transform-regenerator/**/*.js"],
"rules": {
"local-rules/plugin-name": "error"
}
} }
] ]
} }

View File

@ -2,8 +2,10 @@
const noDeprecatedClone = require("./scripts/eslint_rules/no-deprecated-clone"); const noDeprecatedClone = require("./scripts/eslint_rules/no-deprecated-clone");
const noUndefinedIdentifier = require("./scripts/eslint_rules/no-undefined-identifier"); const noUndefinedIdentifier = require("./scripts/eslint_rules/no-undefined-identifier");
const pluginName = require("./scripts/eslint_rules/plugin-name");
module.exports = { module.exports = {
"no-deprecated-clone": noDeprecatedClone, "no-deprecated-clone": noDeprecatedClone,
"no-undefined-identifier": noUndefinedIdentifier, "no-undefined-identifier": noUndefinedIdentifier,
"plugin-name": pluginName,
}; };

View File

@ -18,6 +18,7 @@ export default declare((api, options) => {
const helperWhitelist = whitelist ? new Set(whitelist) : null; const helperWhitelist = whitelist ? new Set(whitelist) : null;
return { return {
name: "external-helpers",
pre(file) { pre(file) {
file.set("helperGenerator", name => { file.set("helperGenerator", name => {
// If the helper didn't exist yet at the version given, we bail // If the helper didn't exist yet at the version given, we bail

View File

@ -78,7 +78,9 @@ export default declare(api => {
}; };
return { return {
name: "proposal-async-generator-functions",
inherits: syntaxAsyncGenerators, inherits: syntaxAsyncGenerators,
visitor: { visitor: {
Program(path, state) { Program(path, state) {
// We need to traverse the ast here (instead of just vising Function // We need to traverse the ast here (instead of just vising Function

View File

@ -336,6 +336,7 @@ export default declare((api, options) => {
: buildClassStaticPrivatePropertySpec; : buildClassStaticPrivatePropertySpec;
return { return {
name: "proposal-class-properties",
inherits: syntaxClassProperties, inherits: syntaxClassProperties,
visitor: { visitor: {

View File

@ -32,6 +32,7 @@ export default declare((api, options) => {
} }
return { return {
name: "proposal-decorators",
inherits: syntaxDecorators, inherits: syntaxDecorators,
manipulateOptions({ generatorOpts }) { manipulateOptions({ generatorOpts }) {

View File

@ -5,6 +5,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-do-expressions",
inherits: syntaxDoExpressions, inherits: syntaxDoExpressions,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-export-default-from",
inherits: syntaxExportDefaultFrom, inherits: syntaxExportDefaultFrom,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-export-namespace-from",
inherits: syntaxExportNamespaceFrom, inherits: syntaxExportNamespaceFrom,
visitor: { visitor: {

View File

@ -39,6 +39,7 @@ export default declare(api => {
} }
return { return {
name: "proposal-function-bind",
inherits: syntaxFunctionBind, inherits: syntaxFunctionBind,
visitor: { visitor: {

View File

@ -35,6 +35,7 @@ export default declare(api => {
}; };
return { return {
name: "proposal-function-sent",
inherits: syntaxFunctionSent, inherits: syntaxFunctionSent,
visitor: { visitor: {

View File

@ -15,6 +15,7 @@ export default declare(api => {
} }
return { return {
name: "proposal-json-strings",
inherits: syntaxJsonStrings, inherits: syntaxJsonStrings,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-logical-assignment-operators",
inherits: syntaxLogicalAssignmentOperators, inherits: syntaxLogicalAssignmentOperators,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare((api, { loose = false }) => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-nullish-coalescing-operator",
inherits: syntaxNullishCoalescingOperator, inherits: syntaxNullishCoalescingOperator,
visitor: { visitor: {

View File

@ -19,6 +19,7 @@ export default declare(api => {
} }
return { return {
name: "proposal-numeric-separator",
inherits: syntaxNumericSeparator, inherits: syntaxNumericSeparator,
visitor: { visitor: {

View File

@ -178,6 +178,7 @@ export default declare((api, opts) => {
} }
return { return {
name: "proposal-object-rest-spread",
inherits: syntaxObjectRestSpread, inherits: syntaxObjectRestSpread,
visitor: { visitor: {

View File

@ -5,6 +5,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-optional-catch-binding",
inherits: syntaxOptionalCatchBinding, inherits: syntaxOptionalCatchBinding,
visitor: { visitor: {

View File

@ -127,6 +127,7 @@ export default declare((api, options) => {
} }
return { return {
name: "proposal-optional-chaining",
inherits: syntaxOptionalChaining, inherits: syntaxOptionalChaining,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-pipeline-operator",
inherits: syntaxPipelineOperator, inherits: syntaxPipelineOperator,
visitor: { visitor: {

View File

@ -6,6 +6,7 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "proposal-throw-expressions",
inherits: syntaxThrowExpressions, inherits: syntaxThrowExpressions,
visitor: { visitor: {

View File

@ -11,6 +11,8 @@ export default declare((api, options) => {
} }
return { return {
name: "proposal-unicode-property-regex",
visitor: { visitor: {
RegExpLiteral(path) { RegExpLiteral(path) {
const node = path.node; const node = path.node;

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-async-generators",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("asyncGenerators"); parserOpts.plugins.push("asyncGenerators");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-bigint",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("bigInt"); parserOpts.plugins.push("bigInt");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-class-properties",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("classProperties", "classPrivateProperties"); parserOpts.plugins.push("classProperties", "classPrivateProperties");
}, },

View File

@ -30,6 +30,8 @@ export default declare((api, options) => {
} }
return { return {
name: "syntax-decorators",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push( parserOpts.plugins.push(
legacy legacy

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-do-expressions",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("doExpressions"); parserOpts.plugins.push("doExpressions");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-dynamic-import",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("dynamicImport"); parserOpts.plugins.push("dynamicImport");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-export-default-from",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("exportDefaultFrom"); parserOpts.plugins.push("exportDefaultFrom");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-export-namespace-from",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("exportNamespaceFrom"); parserOpts.plugins.push("exportNamespaceFrom");
}, },

View File

@ -12,6 +12,8 @@ export default declare((api, options) => {
} }
return { return {
name: "syntax-flow",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
// If the file has already enabled TS, assume that this is not a // If the file has already enabled TS, assume that this is not a
// valid Flowtype file. // valid Flowtype file.

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-function-bind",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("functionBind"); parserOpts.plugins.push("functionBind");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-function-sent",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("functionSent"); parserOpts.plugins.push("functionSent");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-import-meta",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("importMeta"); parserOpts.plugins.push("importMeta");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-json-strings",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("jsonStrings"); parserOpts.plugins.push("jsonStrings");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-jsx",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
// If the Typescript plugin already ran, it will have decided whether // If the Typescript plugin already ran, it will have decided whether
// or not this is a TSX file. // or not this is a TSX file.

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-logical-assignment-operators",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("logicalAssignment"); parserOpts.plugins.push("logicalAssignment");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-nullish-coalescing-operator",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("nullishCoalescingOperator"); parserOpts.plugins.push("nullishCoalescingOperator");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-numeric-separator",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("numericSeparator"); parserOpts.plugins.push("numericSeparator");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-object-rest-spread",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("objectRestSpread"); parserOpts.plugins.push("objectRestSpread");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-optional-catch-binding",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("optionalCatchBinding"); parserOpts.plugins.push("optionalCatchBinding");
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-optional-chaining",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("optionalChaining"); parserOpts.plugins.push("optionalChaining");
}, },

View File

@ -15,6 +15,8 @@ export default declare((api, { proposal }) => {
} }
return { return {
name: "syntax-pipeline-operator",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push(["pipelineOperator", { proposal }]); parserOpts.plugins.push(["pipelineOperator", { proposal }]);
}, },

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-throw-expressions",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("throwExpressions"); parserOpts.plugins.push("throwExpressions");
}, },

View File

@ -19,6 +19,8 @@ export default declare((api, { isTSX }) => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "syntax-typescript",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
const { plugins } = parserOpts; const { plugins } = parserOpts;
// If the Flow syntax plugin already ran, remove it since Typescript // If the Flow syntax plugin already ran, remove it since Typescript

View File

@ -6,6 +6,8 @@ export default declare((api, options) => {
const { spec } = options; const { spec } = options;
return { return {
name: "transform-arrow-functions",
visitor: { visitor: {
ArrowFunctionExpression( ArrowFunctionExpression(
path: NodePath<BabelNodeArrowFunctionExpression>, path: NodePath<BabelNodeArrowFunctionExpression>,

View File

@ -10,6 +10,8 @@ export default declare((api, options) => {
if (method && module) { if (method && module) {
return { return {
name: "transform-async-to-generator",
visitor: { visitor: {
Function(path, state) { Function(path, state) {
if (!path.node.async || path.node.generator) return; if (!path.node.async || path.node.generator) return;
@ -28,6 +30,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-async-to-generator",
visitor: { visitor: {
Function(path, state) { Function(path, state) {
if (!path.node.async || path.node.generator) return; if (!path.node.async || path.node.generator) return;

View File

@ -26,6 +26,8 @@ export default declare(api => {
} }
return { return {
name: "transform-block-scoped-functions",
visitor: { visitor: {
BlockStatement(path) { BlockStatement(path) {
const { node, parent } = path; const { node, parent } = path;

View File

@ -20,6 +20,8 @@ export default declare((api, opts) => {
} }
return { return {
name: "transform-block-scoping",
visitor: { visitor: {
VariableDeclaration(path) { VariableDeclaration(path) {
const { node, parent, scope } = path; const { node, parent, scope } = path;

View File

@ -25,6 +25,8 @@ export default declare((api, options) => {
const VISITED = Symbol(); const VISITED = Symbol();
return { return {
name: "transform-classes",
visitor: { visitor: {
ExportDefaultDeclaration(path: NodePath) { ExportDefaultDeclaration(path: NodePath) {
if (!path.get("declaration").isClassDeclaration()) return; if (!path.get("declaration").isClassDeclaration()) return;

View File

@ -115,6 +115,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-computed-properties",
visitor: { visitor: {
ObjectExpression: { ObjectExpression: {
exit(path, state) { exit(path, state) {

View File

@ -411,6 +411,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-destructuring",
visitor: { visitor: {
ExportNamedDeclaration(path) { ExportNamedDeclaration(path) {
const declaration = path.get("declaration"); const declaration = path.get("declaration");

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-dotall-regex",
visitor: { visitor: {
RegExpLiteral(path) { RegExpLiteral(path) {
const node = path.node; const node = path.node;

View File

@ -12,6 +12,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-duplicate-keys",
visitor: { visitor: {
ObjectExpression(path) { ObjectExpression(path) {
const { node } = path; const { node } = path;

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-exponentiation-operator",
visitor: build({ visitor: build({
operator: "**", operator: "**",

View File

@ -27,6 +27,7 @@ export default declare(api => {
} }
return { return {
name: "transform-flow-comments",
inherits: syntaxFlow, inherits: syntaxFlow,
visitor: { visitor: {

View File

@ -10,6 +10,7 @@ export default declare(api => {
let skipStrip = false; let skipStrip = false;
return { return {
name: "transform-flow-strip-types",
inherits: syntaxFlow, inherits: syntaxFlow,
visitor: { visitor: {

View File

@ -14,6 +14,8 @@ export default declare((api, options) => {
if (assumeArray) { if (assumeArray) {
return { return {
name: "transform-for-of",
visitor: { visitor: {
ForOfStatement(path) { ForOfStatement(path) {
const { scope } = path; const { scope } = path;
@ -176,6 +178,7 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-for-of",
visitor: { visitor: {
ForOfStatement(path, state) { ForOfStatement(path, state) {
const right = path.get("right"); const right = path.get("right");

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-function-name",
visitor: { visitor: {
FunctionExpression: { FunctionExpression: {
exit(path) { exit(path) {

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-instanceof",
visitor: { visitor: {
BinaryExpression(path) { BinaryExpression(path) {
const { node } = path; const { node } = path;

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-jscript",
visitor: { visitor: {
FunctionExpression: { FunctionExpression: {
exit(path) { exit(path) {

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-literals",
visitor: { visitor: {
NumericLiteral({ node }) { NumericLiteral({ node }) {
// number octal like 0b10 or 0o70 // number octal like 0b10 or 0o70

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-member-expression-literals",
visitor: { visitor: {
MemberExpression: { MemberExpression: {
exit({ node }) { exit({ node }) {

View File

@ -20,6 +20,8 @@ export default declare((api, options) => {
const { loose, allowTopLevelThis, strict, strictMode, noInterop } = options; const { loose, allowTopLevelThis, strict, strictMode, noInterop } = options;
return { return {
name: "transform-modules-amd",
visitor: { visitor: {
Program: { Program: {
exit(path) { exit(path) {

View File

@ -116,6 +116,8 @@ export default declare((api, options) => {
}; };
return { return {
name: "transform-modules-commonjs",
visitor: { visitor: {
Program: { Program: {
exit(path, state) { exit(path, state) {

View File

@ -166,6 +166,8 @@ export default declare((api, options) => {
}; };
return { return {
name: "transform-modules-systemjs",
visitor: { visitor: {
CallExpression(path, state) { CallExpression(path, state) {
if (path.node.callee.type === TYPE_IMPORT) { if (path.node.callee.type === TYPE_IMPORT) {

View File

@ -125,6 +125,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-modules-umd",
visitor: { visitor: {
Program: { Program: {
exit(path) { exit(path) {

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-object-assign",
visitor: { visitor: {
CallExpression: function(path, file) { CallExpression: function(path, file) {
if (path.get("callee").matchesPattern("Object.assign")) { if (path.get("callee").matchesPattern("Object.assign")) {

View File

@ -4,6 +4,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-object-set-prototype-of-to-assign",
visitor: { visitor: {
CallExpression(path, file) { CallExpression(path, file) {
if (path.get("callee").matchesPattern("Object.setPrototypeOf")) { if (path.get("callee").matchesPattern("Object.setPrototypeOf")) {

View File

@ -16,6 +16,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-object-super",
visitor: { visitor: {
ObjectExpression(path, state) { ObjectExpression(path, state) {
let objectRef; let objectRef;

View File

@ -7,6 +7,8 @@ export default declare((api, options) => {
const { loose } = options; const { loose } = options;
return { return {
name: "transform-parameters",
visitor: { visitor: {
Function(path) { Function(path) {
if ( if (

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-property-literals",
visitor: { visitor: {
ObjectProperty: { ObjectProperty: {
exit({ node }) { exit({ node }) {

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-property-mutators",
visitor: { visitor: {
ObjectExpression(path, file) { ObjectExpression(path, file) {
const { node } = path; const { node } = path;

View File

@ -24,6 +24,8 @@ export default declare(api => {
} }
return { return {
name: "transform-proto-to-assign",
visitor: { visitor: {
AssignmentExpression(path, file) { AssignmentExpression(path, file) {
if (!isProtoAssignmentExpression(path.node)) return; if (!isProtoAssignmentExpression(path.node)) return;

View File

@ -78,6 +78,8 @@ export default declare((api, options) => {
}; };
return { return {
name: "transform-react-constant-elements",
visitor: { visitor: {
JSXElement(path) { JSXElement(path) {
if (HOISTED.has(path.node)) return; if (HOISTED.has(path.node)) return;

View File

@ -53,6 +53,8 @@ export default declare(api => {
} }
return { return {
name: "transform-react-display-name",
visitor: { visitor: {
ExportDefaultDeclaration({ node }, state) { ExportDefaultDeclaration({ node }, state) {
if (isCreateClass(node.declaration)) { if (isCreateClass(node.declaration)) {

View File

@ -64,5 +64,8 @@ export default declare(api => {
} }
}, },
}); });
return { visitor }; return {
name: "transform-react-inline-elements",
visitor,
};
}); });

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-react-jsx-compat",
manipulateOptions(opts, parserOpts) { manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("jsx"); parserOpts.plugins.push("jsx");
}, },

View File

@ -29,6 +29,7 @@ export default declare(api => {
}; };
return { return {
name: "transform-react-jsx-self",
visitor, visitor,
}; };
}); });

View File

@ -75,6 +75,7 @@ export default declare(api => {
}; };
return { return {
name: "transform-react-jsx-source",
visitor, visitor,
}; };
}); });

View File

@ -93,6 +93,7 @@ export default declare((api, options) => {
}; };
return { return {
name: "transform-react-jsx",
inherits: jsx, inherits: jsx,
visitor, visitor,
}; };

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-reserved-words",
visitor: { visitor: {
"BindingIdentifier|ReferencedIdentifier"(path) { "BindingIdentifier|ReferencedIdentifier"(path) {
if (!t.isValidES3Identifier(path.node.name)) { if (!t.isValidES3Identifier(path.node.name)) {

View File

@ -135,6 +135,8 @@ export default declare((api, options, dirname) => {
} }
return { return {
name: "transform-runtime",
pre(file) { pre(file) {
if (useRuntimeHelpers) { if (useRuntimeHelpers) {
file.set("helperGenerator", name => { file.set("helperGenerator", name => {

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-shorthand-properties",
visitor: { visitor: {
ObjectMethod(path) { ObjectMethod(path) {
const { node } = path; const { node } = path;

View File

@ -48,6 +48,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-spread",
visitor: { visitor: {
ArrayExpression(path) { ArrayExpression(path) {
const { node, scope } = path; const { node, scope } = path;

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-sticky-regex",
visitor: { visitor: {
RegExpLiteral(path) { RegExpLiteral(path) {
const { node } = path; const { node } = path;

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-strict-mode",
visitor: { visitor: {
Program(path) { Program(path) {
const { node } = path; const { node } = path;

View File

@ -44,6 +44,8 @@ export default declare((api, options) => {
} }
return { return {
name: "transform-template-literals",
visitor: { visitor: {
TaggedTemplateExpression(path) { TaggedTemplateExpression(path) {
const { node } = path; const { node } = path;

View File

@ -5,6 +5,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-typeof-symbol",
visitor: { visitor: {
Scope({ scope }) { Scope({ scope }) {
if (!scope.getBinding("Symbol")) { if (!scope.getBinding("Symbol")) {

View File

@ -26,7 +26,9 @@ export default declare((api, { jsxPragma = "React" }) => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-typescript",
inherits: syntaxTypeScript, inherits: syntaxTypeScript,
visitor: { visitor: {
//"Pattern" alias doesn't include Identifier or RestElement. //"Pattern" alias doesn't include Identifier or RestElement.
Pattern: visitPattern, Pattern: visitPattern,

View File

@ -6,6 +6,8 @@ export default declare(api => {
api.assertVersion(7); api.assertVersion(7);
return { return {
name: "transform-unicode-regex",
visitor: { visitor: {
RegExpLiteral({ node }) { RegExpLiteral({ node }) {
if (!regex.is(node, "u")) return; if (!regex.is(node, "u")) return;

View File

@ -2,168 +2,7 @@
"use strict"; "use strict";
function getVariableDefinition(name /*: string */, scope /*: Scope */) { const getReferenceOrigin = require("./utils/get-reference-origin");
let currentScope = scope;
do {
const variable = currentScope.set.get(name);
if (variable && variable.defs[0]) {
return { scope: currentScope, definition: variable.defs[0] };
}
} while ((currentScope = currentScope.upper));
}
/*::
type ReferenceOriginImport = { kind: "import", source: string, name: string };
type ReferenceOriginParam = {
kind: "export param",
exportName: string,
index: number,
};
type ReferenceOrigin =
| ReferenceOriginImport
| ReferenceOriginParam
| { kind: "import *", source: string }
| {
kind: "property",
base: ReferenceOriginImport | ReferenceOriginParam,
path: string,
};
*/
// Given a node and a context, returns a description of where its value comes
// from.
// It resolves imports, parameters of exported functions and property accesses.
// See the ReferenceOrigin type for more informations.
function getReferenceOrigin(
node /*: Node */,
scope /*: Scope */
) /*: ?ReferenceOrigin */ {
if (node.type === "Identifier") {
const variable = getVariableDefinition(node.name, scope);
if (!variable) return null;
const definition = variable.definition;
const defNode = definition.node;
if (definition.type === "ImportBinding") {
if (defNode.type === "ImportSpecifier") {
return {
kind: "import",
source: definition.parent.source.value,
name: defNode.imported.name,
};
}
if (defNode.type === "ImportNamespaceSpecifier") {
return {
kind: "import *",
source: definition.parent.source.value,
};
}
}
if (definition.type === "Variable" && defNode.init) {
const origin = getReferenceOrigin(defNode.init, variable.scope);
return origin && patternToProperty(definition.name, origin);
}
if (definition.type === "Parameter") {
const parent = defNode.parent;
let exportName /*: string */;
if (parent.type === "ExportDefaultDeclaration") {
exportName = "default";
} else if (parent.type === "ExportNamedDeclaration") {
exportName = defNode.id.name;
} else if (
parent.type === "AssignmentExpression" &&
parent.left.type === "MemberExpression" &&
parent.left.object.type === "Identifier" &&
parent.left.object.name === "module" &&
parent.left.property.type === "Identifier" &&
parent.left.property.name === "exports"
) {
exportName = "module.exports";
} else {
return null;
}
return patternToProperty(definition.name, {
kind: "export param",
exportName,
index: definition.index,
});
}
}
if (node.type === "MemberExpression" && !node.computed) {
const origin = getReferenceOrigin(node.object, scope);
return origin && addProperty(origin, node.property.name);
}
return null;
}
function patternToProperty(
id /*: Node */,
base /*: ReferenceOrigin */
) /*: ?ReferenceOrigin */ {
const path = getPatternPath(id);
return path && path.reduce(addProperty, base);
}
// Adds a property to a given origin. If it was a namespace import it becomes
// a named import, so that `import * as x from "foo"; x.bar` and
// `import { bar } from "foo"` have the same origin.
function addProperty(
origin /*: ReferenceOrigin */,
name /*: string */
) /* ReferenceOrigin */ {
if (origin.kind === "import *") {
return {
kind: "import",
source: origin.source,
name,
};
}
if (origin.kind === "property") {
return {
kind: "property",
base: origin.base,
path: origin.path + "." + name,
};
}
return {
kind: "property",
base: origin,
path: name,
};
}
// if "node" is c of { a: { b: c } }, the result is ["a","b"]
function getPatternPath(node /*: Node */) /*: ?string[] */ {
let current = node;
const path = [];
// Unshift keys to path while going up
do {
const property = current.parent;
if (
property.type === "ArrayPattern" ||
property.type === "AssignmentPattern" ||
property.computed
) {
// These nodes are not supported.
return null;
}
if (property.type === "Property") {
path.unshift(property.key.name);
} else {
// The destructuring pattern is finished
break;
}
} while ((current = current.parent.parent));
return path;
}
function reportError(context /*: Context */, node /*: Node */) { function reportError(context /*: Context */, node /*: Node */) {
const isMemberExpression = node.type === "MemberExpression"; const isMemberExpression = node.type === "MemberExpression";

View File

@ -0,0 +1,62 @@
"use strict";
const getReferenceOrigin = require("./utils/get-reference-origin");
function reportNoPlugin(context /*: Context */, node /*: Node */) {
context.report({
node,
message: "This file does not export a Babel plugin",
});
}
function is(type /*: string */) /*: (node: Node) => boolean */ {
return node => node.type === type;
}
module.exports = {
meta: {
schema: [],
},
create(context /*: Context */) {
return {
Program(program /*: Node */) {
if (!program.body.some(is("ExportDefaultDeclaration"))) {
return reportNoPlugin(context, program);
}
},
ExportDefaultDeclaration(exportDefaultDecl) {
let plugin = exportDefaultDecl.declaration;
if (plugin.type === "CallExpression") {
// export default declare(api => { ... });
const origin = getReferenceOrigin(plugin.callee, context.getScope());
if (
origin &&
origin.kind === "import" &&
origin.name === "declare" &&
origin.source === "@babel/helper-plugin-utils"
) {
plugin = plugin.arguments[0];
}
}
if (!plugin.type.includes("Function")) {
return reportNoPlugin(context, exportDefaultDecl.parent);
}
const returnNode = plugin.body.body.find(is("ReturnStatement"));
if (!returnNode || returnNode.argument.type !== "ObjectExpression") {
return reportNoPlugin(context, exportDefaultDecl.parent);
}
if (!returnNode.argument.properties.some(p => p.key.name === "name")) {
context.report(
returnNode,
"This Babel plugin doesn't have a 'name' property."
);
}
},
};
},
};

View File

@ -0,0 +1,34 @@
/*:: // ESLint types
type Node = { type: string, [string]: any };
type Definition = {
type: "ImportedBinding",
name: Node,
node: Node,
parent: Node,
};
type Variable = {
defs: Definition[],
};
type Scope = {
set: Map<string, Variable>,
upper: ?Scope,
};
type Context = {
report(options: {
node: Node,
message: string,
fix?: (fixer: Fixer) => ?Fixer,
}): void,
getScope(): Scope,
};
type Fixer = {
replaceText(node: Node, replacement: string): Fixer,
};
*/

View File

@ -0,0 +1,166 @@
"use strict";
module.exports = getReferenceOrigin;
/*::
type ReferenceOriginImport = { kind: "import", source: string, name: string };
type ReferenceOriginParam = {
kind: "export param",
exportName: string,
index: number,
};
type ReferenceOrigin =
| ReferenceOriginImport
| ReferenceOriginParam
| { kind: "import *", source: string }
| {
kind: "property",
base: ReferenceOriginImport | ReferenceOriginParam,
path: string,
};
*/
// Given a node and a context, returns a description of where its value comes
// from.
// It resolves imports, parameters of exported functions and property accesses.
// See the ReferenceOrigin type for more informations.
function getReferenceOrigin(
node /*: Node */,
scope /*: Scope */
) /*: ?ReferenceOrigin */ {
if (node.type === "Identifier") {
const variable = getVariableDefinition(node.name, scope);
if (!variable) return null;
const definition = variable.definition;
const defNode = definition.node;
if (definition.type === "ImportBinding") {
if (defNode.type === "ImportSpecifier") {
return {
kind: "import",
source: definition.parent.source.value,
name: defNode.imported.name,
};
}
if (defNode.type === "ImportNamespaceSpecifier") {
return {
kind: "import *",
source: definition.parent.source.value,
};
}
}
if (definition.type === "Variable" && defNode.init) {
const origin = getReferenceOrigin(defNode.init, variable.scope);
return origin && patternToProperty(definition.name, origin);
}
if (definition.type === "Parameter") {
const parent = defNode.parent;
let exportName /*: string */;
if (parent.type === "ExportDefaultDeclaration") {
exportName = "default";
} else if (parent.type === "ExportNamedDeclaration") {
exportName = defNode.id.name;
} else if (
parent.type === "AssignmentExpression" &&
parent.left.type === "MemberExpression" &&
parent.left.object.type === "Identifier" &&
parent.left.object.name === "module" &&
parent.left.property.type === "Identifier" &&
parent.left.property.name === "exports"
) {
exportName = "module.exports";
} else {
return null;
}
return patternToProperty(definition.name, {
kind: "export param",
exportName,
index: definition.index,
});
}
}
if (node.type === "MemberExpression" && !node.computed) {
const origin = getReferenceOrigin(node.object, scope);
return origin && addProperty(origin, node.property.name);
}
return null;
}
function getVariableDefinition(name /*: string */, scope /*: Scope */) {
let currentScope = scope;
do {
const variable = currentScope.set.get(name);
if (variable && variable.defs[0]) {
return { scope: currentScope, definition: variable.defs[0] };
}
} while ((currentScope = currentScope.upper));
}
function patternToProperty(
id /*: Node */,
base /*: ReferenceOrigin */
) /*: ?ReferenceOrigin */ {
const path = getPatternPath(id);
return path && path.reduce(addProperty, base);
}
// Adds a property to a given origin. If it was a namespace import it becomes
// a named import, so that `import * as x from "foo"; x.bar` and
// `import { bar } from "foo"` have the same origin.
function addProperty(
origin /*: ReferenceOrigin */,
name /*: string */
) /* ReferenceOrigin */ {
if (origin.kind === "import *") {
return {
kind: "import",
source: origin.source,
name,
};
}
if (origin.kind === "property") {
return {
kind: "property",
base: origin.base,
path: origin.path + "." + name,
};
}
return {
kind: "property",
base: origin,
path: name,
};
}
// if "node" is c of { a: { b: c } }, the result is ["a","b"]
function getPatternPath(node /*: Node */) /*: ?string[] */ {
let current = node;
const path = [];
// Unshift keys to path while going up
do {
const property = current.parent;
if (
property.type === "ArrayPattern" ||
property.type === "AssignmentPattern" ||
property.computed
) {
// These nodes are not supported.
return null;
}
if (property.type === "Property") {
path.unshift(property.key.name);
} else {
// The destructuring pattern is finished
break;
}
} while ((current = current.parent.parent));
return path;
}