Skip to content

Commit

Permalink
support for node.x = y comments to add metadata, run over traverse too
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Sep 4, 2020
1 parent 170b2ba commit 43b9022
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion babel.config.js
Expand Up @@ -133,7 +133,7 @@ module.exports = function (api) {
].filter(Boolean),
overrides: [
{
test: [/(babel-plugin|babel-helper)-/],
test: [/(babel-plugin|babel-helper|babel-traverse)/],
plugins: [metadataPlugin],
},
{
Expand Down
Expand Up @@ -12,6 +12,7 @@ export default declare(api => {
NumericLiteral({ node }) {
const { extra } = node;
if (extra && /_/.test(extra.raw)) {
// node: node
extra.raw = extra.raw.replace(/_/g, "");
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-block-scoping/src/index.js
Expand Up @@ -147,6 +147,7 @@ function convertBlockScopedToVar(
}
}

// node: node
node[t.BLOCK_SCOPED_SYMBOL] = true;
node.kind = "var";

Expand Down
Expand Up @@ -17,6 +17,7 @@ export default declare(api => {
!t.isValidES3Identifier(key.name)
) {
// default: "bar" -> "default": "bar"
// node: node
node.key = t.stringLiteral(key.name);
}
},
Expand Down
Expand Up @@ -26,6 +26,7 @@ export default declare(api => {

ObjectProperty({ node }) {
if (node.shorthand) {
// node: node
node.shorthand = false;
}
},
Expand Down
1 change: 1 addition & 0 deletions packages/babel-traverse/src/scope/lib/renamer.js
Expand Up @@ -5,6 +5,7 @@ import * as t from "@babel/types";
const renameVisitor = {
ReferencedIdentifier({ node }, state) {
if (node.name === state.oldName) {
// node: node
node.name = state.newName;
}
},
Expand Down
1 change: 1 addition & 0 deletions sandbox/src/components/App.js
Expand Up @@ -307,6 +307,7 @@ function CompiledOutput({
}
});
gzipSize(code).then(s => setGzip(s));
window.ranges = ranges;
setCompiled({
code,
size: new Blob([code], { type: "text/plain" }).size,
Expand Down
103 changes: 77 additions & 26 deletions sandbox/src/metadataPlugin.js
@@ -1,19 +1,86 @@
const path = require("path");
function normalize(src) {
return src.replace(/\//, path.sep);
}
const BABEL_PACKAGES_REGEXP =
path.sep === "/" ? /packages\/(.*)/ : /packages\\(.*)/;

module.exports = function babelPlugin(babel) {
const { types: t, template } = babel;

function getMetaComment(path) {
/*
Allow specifying a node to get correct start/end
(instead of defaulting to this.node).
// node: rest
target.insertBefore(loop);
*/
const hasComment = path.parentPath.node.leadingComments;
let comment;
if (hasComment && hasComment.length) {
comment = hasComment[hasComment.length - 1].value.match(/node: (.*)/);
if (comment) {
// remove the comment?
path.parentPath.node.leadingComments = [];
comment = template.expression(comment[1])();
}
}
return comment;
}

return {
name: "transform-babel-metadata",
pre(state) {
const filename =
state.opts.filename || "babel/packages/babel-plugin-custom/index.js";
this.filePath = filename.match(BABEL_PACKAGES_REGEXP)[1];
this.pluginName = this.filePath.match(/^[^(/|\\)]*/)[0];
},
visitor: {
/*
node.babelPlugin = [{
name: "proposal-numeric-separator",
file: "proposal-numeric-separator\\src\\index.js (16:10)",
start: node.start,
end: node.end
}]
extra.raw = extra.raw.replace(/_/g, "");
*/
AssignmentExpression(path, state) {
const comment = getMetaComment(path);
if (!comment) return;
const props = [
t.objectProperty(
t.identifier("name"),
t.stringLiteral(this.pluginName)
),
t.objectProperty(
t.identifier("file"),
t.stringLiteral(
`${this.filePath} (${path.node.loc.start.line}:${path.node.loc.start.column})`
)
),
t.objectProperty(
t.identifier("start"),
t.memberExpression(comment, t.identifier("start"))
),
t.objectProperty(
t.identifier("end"),
t.memberExpression(comment, t.identifier("end"))
),
];
const metaNode = t.objectExpression(props);

path.insertBefore(
t.assignmentExpression(
"=",
t.memberExpression(comment, t.identifier("babelPlugin")),
t.arrayExpression([metaNode])
)
);
},
// pathX.replaceWith(a) -> pathX.replaceWith(a, { name: "name" })
// TODO: handle nested MemberExpression like a.b.replaceWith
// TODO: CallExpression like path.get("left").replaceWith
CallExpression(path, state) {
state.filename = state.filename || "babel-plugin-custom/src.js";
if (
path.node.callee.type === "MemberExpression" &&
path.node.callee.property.type === "Identifier" &&
Expand All @@ -29,24 +96,7 @@ module.exports = function babelPlugin(babel) {
(path.node.callee.object.type === "Identifier" &&
path.node.callee.object.name) ||
"path";
const pluginName = normalize(state.filename).match(
/babel-(plugin|helper)-((\w+-?)+)/
)[2];
/*
Allow specifying a node to get correct start/end
(instead of defaulting to this.node).
// node: rest
target.insertBefore(loop);
*/
const hasComment = path.parentPath.node.leadingComments;
let comment;
if (hasComment && hasComment.length) {
comment = hasComment[0].value.match(/node: (.*)/);
if (comment) {
comment = template.expression(comment[1])();
}
}
const comment = getMetaComment(path);
// "C:\\Users\\babel\\packages\\babel-plugin-proposal-unicode-property-regex\\src\\index.js".match(/babel-(plugin|helper)-((\w+-?)+)/)
// {
// name: "unicode-property-regex",
Expand All @@ -55,13 +105,14 @@ module.exports = function babelPlugin(babel) {
// end: 1,
// }
const props = [
t.objectProperty(t.identifier("name"), t.stringLiteral(pluginName)),
t.objectProperty(
t.identifier("name"),
t.stringLiteral(this.pluginName)
),
t.objectProperty(
t.identifier("file"),
t.stringLiteral(
`${normalize(state.filename).substr(
normalize(state.filename).indexOf(pluginName)
)} (${path.node.loc.start.line}:${path.node.loc.start.column})`
`${this.filePath} (${path.node.loc.start.line}:${path.node.loc.start.column})`
)
),
];
Expand Down

0 comments on commit 43b9022

Please sign in to comment.