Befor generating
This commit is contained in:
1 parent
3383f4bf4a
commit
1aa1b5f625
6756 files changed
+649946
-1
No files matched your search
Generated
Vendored
+92
@@ -0,0 +1,92 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
||||
var _path = require("path");
|
||||
var _core = require("@babel/core");
|
||||
var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
|
||||
api.assertVersion(7);
|
||||
function addDisplayName(id, call) {
|
||||
const props = call.arguments[0].properties;
|
||||
let safe = true;
|
||||
for (let i = 0; i < props.length; i++) {
|
||||
const prop = props[i];
|
||||
if (_core.types.isSpreadElement(prop)) {
|
||||
continue;
|
||||
}
|
||||
const key = _core.types.toComputedKey(prop);
|
||||
if (_core.types.isStringLiteral(key, {
|
||||
value: "displayName"
|
||||
})) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (safe) {
|
||||
props.unshift(_core.types.objectProperty(_core.types.identifier("displayName"), _core.types.stringLiteral(id)));
|
||||
}
|
||||
}
|
||||
const isCreateClassCallExpression = _core.types.buildMatchMemberExpression("React.createClass");
|
||||
const isCreateClassAddon = callee => _core.types.isIdentifier(callee, {
|
||||
name: "createReactClass"
|
||||
});
|
||||
function isCreateClass(node) {
|
||||
if (!node || !_core.types.isCallExpression(node)) return false;
|
||||
if (!isCreateClassCallExpression(node.callee) && !isCreateClassAddon(node.callee)) {
|
||||
return false;
|
||||
}
|
||||
const args = node.arguments;
|
||||
if (args.length !== 1) return false;
|
||||
const first = args[0];
|
||||
if (!_core.types.isObjectExpression(first)) return false;
|
||||
return true;
|
||||
}
|
||||
return {
|
||||
name: "transform-react-display-name",
|
||||
visitor: {
|
||||
ExportDefaultDeclaration({
|
||||
node
|
||||
}, state) {
|
||||
if (isCreateClass(node.declaration)) {
|
||||
const filename = state.filename || "unknown";
|
||||
let displayName = _path.basename(filename, _path.extname(filename));
|
||||
if (displayName === "index") {
|
||||
displayName = _path.basename(_path.dirname(filename));
|
||||
}
|
||||
addDisplayName(displayName, node.declaration);
|
||||
}
|
||||
},
|
||||
CallExpression(path) {
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
if (!isCreateClass(node)) return;
|
||||
let id;
|
||||
path.find(function (path) {
|
||||
if (path.isAssignmentExpression()) {
|
||||
id = path.node.left;
|
||||
} else if (path.isObjectProperty()) {
|
||||
id = path.node.key;
|
||||
} else if (path.isVariableDeclarator()) {
|
||||
id = path.node.id;
|
||||
} else if (path.isStatement()) {
|
||||
return true;
|
||||
}
|
||||
if (id) return true;
|
||||
});
|
||||
if (!id) return;
|
||||
if (_core.types.isMemberExpression(id)) {
|
||||
id = id.property;
|
||||
}
|
||||
if (_core.types.isIdentifier(id)) {
|
||||
addDisplayName(id.name, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_helperPluginUtils","require","_path","_core","_default","exports","default","declare","api","assertVersion","addDisplayName","id","call","props","arguments","properties","safe","i","length","prop","t","isSpreadElement","key","toComputedKey","isStringLiteral","value","unshift","objectProperty","identifier","stringLiteral","isCreateClassCallExpression","buildMatchMemberExpression","isCreateClassAddon","callee","isIdentifier","name","isCreateClass","node","isCallExpression","args","first","isObjectExpression","visitor","ExportDefaultDeclaration","state","declaration","filename","displayName","path","basename","extname","dirname","CallExpression","find","isAssignmentExpression","left","isObjectProperty","isVariableDeclarator","isStatement","isMemberExpression","property"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport path from \"node:path\";\nimport { types as t } from \"@babel/core\";\n\ntype ReactCreateClassCall = t.CallExpression & {\n arguments: [t.ObjectExpression];\n};\n\nexport default declare(api => {\n api.assertVersion(REQUIRED_VERSION(7));\n\n function addDisplayName(id: string, call: ReactCreateClassCall) {\n const props = call.arguments[0].properties;\n let safe = true;\n\n for (let i = 0; i < props.length; i++) {\n const prop = props[i];\n if (t.isSpreadElement(prop)) {\n continue;\n }\n const key = t.toComputedKey(prop);\n if (t.isStringLiteral(key, { value: \"displayName\" })) {\n safe = false;\n break;\n }\n }\n\n if (safe) {\n props.unshift(\n t.objectProperty(t.identifier(\"displayName\"), t.stringLiteral(id)),\n );\n }\n }\n\n const isCreateClassCallExpression =\n t.buildMatchMemberExpression(\"React.createClass\");\n const isCreateClassAddon = (callee: t.CallExpression[\"callee\"]) =>\n t.isIdentifier(callee, { name: \"createReactClass\" });\n\n function isCreateClass(node?: t.Node): node is ReactCreateClassCall {\n if (!node || !t.isCallExpression(node)) return false;\n\n // not createReactClass nor React.createClass call member object\n if (\n !isCreateClassCallExpression(node.callee) &&\n !isCreateClassAddon(node.callee)\n ) {\n return false;\n }\n\n // no call arguments\n const args = node.arguments;\n if (args.length !== 1) return false;\n\n // first node arg is not an object\n const first = args[0];\n if (!t.isObjectExpression(first)) return false;\n\n return true;\n }\n\n return {\n name: \"transform-react-display-name\",\n\n visitor: {\n ExportDefaultDeclaration({ node }, state) {\n if (isCreateClass(node.declaration)) {\n const filename = state.filename || \"unknown\";\n\n let displayName = path.basename(filename, path.extname(filename));\n\n // ./{module name}/index.js\n if (displayName === \"index\") {\n displayName = path.basename(path.dirname(filename));\n }\n\n addDisplayName(displayName, node.declaration);\n }\n },\n\n CallExpression(path) {\n const { node } = path;\n if (!isCreateClass(node)) return;\n\n let id: t.LVal | t.Expression | t.PrivateName | t.VoidPattern | null;\n\n // crawl up the ancestry looking for possible candidates for displayName inference\n path.find(function (path) {\n if (path.isAssignmentExpression()) {\n id = path.node.left;\n } else if (path.isObjectProperty()) {\n id = path.node.key;\n } else if (path.isVariableDeclarator()) {\n id = path.node.id;\n } else if (path.isStatement()) {\n // we've hit a statement, we should stop crawling up\n return true;\n }\n\n // we've got an id! no need to continue\n if (id) return true;\n });\n\n // ensure that we have an identifier we can inherit from\n if (!id) return;\n\n // foo.bar -> bar\n if (t.isMemberExpression(id)) {\n id = id.property;\n }\n\n // identifiers are the only thing we can reliably get a name from\n if (t.isIdentifier(id)) {\n addDisplayName(id.name, node);\n }\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AAAyC,IAAAG,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAM1B,IAAAC,0BAAO,EAACC,GAAG,IAAI;EAC5BA,GAAG,CAACC,aAAa,CAAkB,CAAE,CAAC;EAEtC,SAASC,cAAcA,CAACC,EAAU,EAAEC,IAA0B,EAAE;IAC9D,MAAMC,KAAK,GAAGD,IAAI,CAACE,SAAS,CAAC,CAAC,CAAC,CAACC,UAAU;IAC1C,IAAIC,IAAI,GAAG,IAAI;IAEf,KAAK,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGJ,KAAK,CAACK,MAAM,EAAED,CAAC,EAAE,EAAE;MACrC,MAAME,IAAI,GAAGN,KAAK,CAACI,CAAC,CAAC;MACrB,IAAIG,WAAC,CAACC,eAAe,CAACF,IAAI,CAAC,EAAE;QAC3B;MACF;MACA,MAAMG,GAAG,GAAGF,WAAC,CAACG,aAAa,CAACJ,IAAI,CAAC;MACjC,IAAIC,WAAC,CAACI,eAAe,CAACF,GAAG,EAAE;QAAEG,KAAKLine truncated
|
||||
Reference in new issue
Block a user