Extract isFromBabelTypes helper
This commit is contained in:
parent
6285cb9b64
commit
1f7197ec16
@ -3,21 +3,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const getReferenceOrigin = require("../utils/get-reference-origin");
|
const getReferenceOrigin = require("../utils/get-reference-origin");
|
||||||
const getExportName = require("../utils/get-export-name");
|
const isFromBabelTypes = require("../utils/is-from-babel-types");
|
||||||
|
|
||||||
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.`,
|
|
||||||
fix(fixer) {
|
|
||||||
if (isMemberExpression) {
|
|
||||||
return fixer.replaceText(id, "cloneNode");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
meta: {
|
meta: {
|
||||||
@ -27,63 +13,29 @@ module.exports = {
|
|||||||
create(context) {
|
create(context) {
|
||||||
return {
|
return {
|
||||||
CallExpression(node) {
|
CallExpression(node) {
|
||||||
|
const { callee } = node;
|
||||||
const scope = context.getScope();
|
const scope = context.getScope();
|
||||||
const origin = getReferenceOrigin(node.callee, scope);
|
|
||||||
|
|
||||||
const report = () => reportError(context, node.callee, origin.name);
|
|
||||||
|
|
||||||
|
const origin = getReferenceOrigin(callee, scope);
|
||||||
if (!origin) return;
|
if (!origin) return;
|
||||||
|
|
||||||
|
const { name } = origin;
|
||||||
if (
|
if (
|
||||||
origin.kind === "import" &&
|
(name === "clone" || name === "cloneDeep") &&
|
||||||
(origin.name === "clone" || origin.name === "cloneDeep") &&
|
isFromBabelTypes(origin, scope)
|
||||||
origin.source === "@babel/types"
|
|
||||||
) {
|
) {
|
||||||
// imported from @babel/types
|
const isMemberExpression = callee.type === "MemberExpression";
|
||||||
return report();
|
const id = isMemberExpression ? callee.property : callee;
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
context.report({
|
||||||
origin.kind === "property" &&
|
node: id,
|
||||||
(origin.path === "clone" || origin.path === "cloneDeep") &&
|
message: `t.${name}() is deprecated. Use t.cloneNode() instead.`,
|
||||||
origin.base.kind === "import" &&
|
fix(fixer) {
|
||||||
origin.base.name === "types" &&
|
if (isMemberExpression) {
|
||||||
origin.base.source === "@babel/core"
|
return fixer.replaceText(id, "cloneNode");
|
||||||
) {
|
}
|
||||||
// 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