Extract isFromBabelTypes helper
This commit is contained in:
parent
6285cb9b64
commit
1f7197ec16
@ -3,11 +3,30 @@
|
||||
"use strict";
|
||||
|
||||
const getReferenceOrigin = require("../utils/get-reference-origin");
|
||||
const getExportName = require("../utils/get-export-name");
|
||||
const isFromBabelTypes = require("../utils/is-from-babel-types");
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
schema: [],
|
||||
fixable: "code",
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
const { callee } = node;
|
||||
const scope = context.getScope();
|
||||
|
||||
const origin = getReferenceOrigin(callee, scope);
|
||||
if (!origin) return;
|
||||
|
||||
const { name } = origin;
|
||||
if (
|
||||
(name === "clone" || name === "cloneDeep") &&
|
||||
isFromBabelTypes(origin, scope)
|
||||
) {
|
||||
const isMemberExpression = callee.type === "MemberExpression";
|
||||
const id = isMemberExpression ? callee.property : callee;
|
||||
|
||||
function reportError(context, node, name) {
|
||||
const isMemberExpression = node.type === "MemberExpression";
|
||||
const id = isMemberExpression ? node.property : node;
|
||||
context.report({
|
||||
node: id,
|
||||
message: `t.${name}() is deprecated. Use t.cloneNode() instead.`,
|
||||
@ -18,73 +37,6 @@ function reportError(context, node, name) {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
schema: [],
|
||||
fixable: "code",
|
||||
},
|
||||
create(context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
const scope = context.getScope();
|
||||
const origin = getReferenceOrigin(node.callee, scope);
|
||||
|
||||
const report = () => reportError(context, node.callee, origin.name);
|
||||
|
||||
if (!origin) return;
|
||||
|
||||
if (
|
||||
origin.kind === "import" &&
|
||||
(origin.name === "clone" || origin.name === "cloneDeep") &&
|
||||
origin.source === "@babel/types"
|
||||
) {
|
||||
// imported from @babel/types
|
||||
return report();
|
||||
}
|
||||
|
||||
if (
|
||||
origin.kind === "property" &&
|
||||
(origin.path === "clone" || origin.path === "cloneDeep") &&
|
||||
origin.base.kind === "import" &&
|
||||
origin.base.name === "types" &&
|
||||
origin.base.source === "@babel/core"
|
||||
) {
|
||||
// imported from @babel/core
|
||||
return report();
|
||||
}
|
||||
|
||||
if (
|
||||
origin.kind === "property" &&
|
||||
(origin.path === "types.clone" ||
|
||||
origin.path === "types.cloneDeep") &&
|
||||
origin.base.kind === "param" &&
|
||||
origin.base.index === 0
|
||||
) {
|
||||
const { functionNode } = origin.base;
|
||||
const { parent } = functionNode;
|
||||
|
||||
if (parent.type === "CallExpression") {
|
||||
const calleeOrigin = getReferenceOrigin(parent.callee, scope);
|
||||
if (
|
||||
calleeOrigin &&
|
||||
calleeOrigin.kind === "import" &&
|
||||
calleeOrigin.name === "declare" &&
|
||||
calleeOrigin.source === "@babel/helper-plugin-utils"
|
||||
) {
|
||||
// Using "declare" from "@babel/helper-plugin-utils"
|
||||
return report();
|
||||
}
|
||||
} else {
|
||||
const exportName = getExportName(functionNode);
|
||||
|
||||
if (exportName === "default" || exportName === "module.exports") {
|
||||
// export default function ({ types: t }) {}
|
||||
// module.exports = function ({ types: t }) {}
|
||||
return report();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
const getReferenceOrigin = require("./get-reference-origin");
|
||||
const getExportName = require("./get-export-name");
|
||||
|
||||
// Check if a ReferenceOrigin (returned by ./get-reference-origin.js)
|
||||
// is a reference to a @babel/types export.
|
||||
module.exports = function isFromBabelTypes(
|
||||
origin /*: ReferenceOrigin */,
|
||||
scope /*: Scope */,
|
||||
) {
|
||||
if (origin.kind === "import" && origin.source === "@babel/types") {
|
||||
// imported from @babel/types
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
origin.kind === "property" &&
|
||||
origin.base.kind === "import" &&
|
||||
origin.base.name === "types" &&
|
||||
origin.base.source === "@babel/core"
|
||||
) {
|
||||
// imported from @babel/core
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
origin.kind !== "property" ||
|
||||
origin.base.kind !== "param" ||
|
||||
origin.base.index !== 0
|
||||
) {
|
||||
// Not a parameter of the plugin factory function
|
||||
return false;
|
||||
}
|
||||
|
||||
const { functionNode } = origin.base;
|
||||
const { parent } = functionNode;
|
||||
|
||||
if (parent.type === "CallExpression") {
|
||||
const calleeOrigin = getReferenceOrigin(parent.callee, scope);
|
||||
|
||||
// Using "declare" from "@babel/helper-plugin-utils"
|
||||
return !!(
|
||||
calleeOrigin &&
|
||||
calleeOrigin.kind === "import" &&
|
||||
calleeOrigin.name === "declare" &&
|
||||
calleeOrigin.source === "@babel/helper-plugin-utils"
|
||||
);
|
||||
}
|
||||
|
||||
const exportName = getExportName(functionNode);
|
||||
|
||||
// export default function ({ types: t }) {}
|
||||
// module.exports = function ({ types: t }) {}
|
||||
return exportName === "default" || exportName === "module.exports";
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user