Skip to content

Commit

Permalink
Generate better builder names for JSX* and TS* (#6967)
Browse files Browse the repository at this point in the history
e.g. JSXIdentifier -> jsxIdentifier.
The jSXIdentifier alias isn't removed, so this commit doesn't introduce breaking changes.
  • Loading branch information
nicolo-ribaudo committed Dec 7, 2017
1 parent fcfa987 commit a2aabbd
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-react-jsx-self/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const TRACE_ID = "__self";
export default function() {
const visitor = {
JSXOpeningElement({ node }) {
const id = t.jSXIdentifier(TRACE_ID);
const id = t.jsxIdentifier(TRACE_ID);
const trace = t.thisExpression();

node.attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
node.attributes.push(t.jsxAttribute(id, t.jsxExpressionContainer(trace)));
},
};

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-react-jsx-source/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function() {

const visitor = {
JSXOpeningElement(path, state) {
const id = t.jSXIdentifier(TRACE_ID);
const id = t.jsxIdentifier(TRACE_ID);
const location = path.container.openingElement.loc;
if (!location) {
// the element was generated and doesn't have location information
Expand Down Expand Up @@ -64,7 +64,7 @@ export default function() {
}

const trace = makeTrace(state.fileNameIdentifier, location.start.line);
attributes.push(t.jSXAttribute(id, t.jSXExpressionContainer(trace)));
attributes.push(t.jsxAttribute(id, t.jsxExpressionContainer(trace)));
},
};

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-react-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default function(api, options) {

visitor.JSXAttribute = function(path) {
if (t.isJSXElement(path.node.value)) {
path.node.value = t.jSXExpressionContainer(path.node.value);
path.node.value = t.jsxExpressionContainer(path.node.value);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-traverse/src/path/conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function hoistFunctionEnvironment(
thisPaths.forEach(thisChild => {
thisChild.replaceWith(
thisChild.isJSX()
? t.jSXIdentifier(thisBinding)
? t.jsxIdentifier(thisBinding)
: t.identifier(thisBinding),
);
});
Expand Down
19 changes: 17 additions & 2 deletions packages/babel-types/scripts/generators/generateBuilders.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
const definitions = require("../../lib/definitions");
const formatBuilderName = require("../utils/formatBuilderName");
const lowerFirst = require("../utils/lowerFirst");

module.exports = function generateBuilders() {
Expand All @@ -12,7 +13,14 @@ import builder from "../builder";\n\n`;

Object.keys(definitions.BUILDER_KEYS).forEach(type => {
output += `export function ${type}(...args: Array<any>): Object { return builder("${type}", ...args); }
export { ${type} as ${lowerFirst(type)} };\n`;
export { ${type} as ${formatBuilderName(type)} };\n`;

// This is needed for backwards compatibility.
// It should be removed in the next major version.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
output += `export { ${type} as ${lowerFirst(type)} }\n`;
}
});

Object.keys(definitions.DEPRECATED_KEYS).forEach(type => {
Expand All @@ -21,7 +29,14 @@ export { ${type} as ${lowerFirst(type)} };\n`;
console.trace("The node type ${type} has been renamed to ${newType}");
return ${type}("${type}", ...args);
}
export { ${type} as ${lowerFirst(type)} };\n`;
export { ${type} as ${formatBuilderName(type)} };\n`;

// This is needed for backwards compatibility.
// It should be removed in the next major version.
// JSXIdentifier -> jSXIdentifier
if (/^[A-Z]{2}/.test(type)) {
output += `export { ${type} as ${lowerFirst(type)} }\n`;
}
});

return output;
Expand Down
9 changes: 9 additions & 0 deletions packages/babel-types/scripts/utils/formatBuilderName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

const toLowerCase = Function.call.bind("".toLowerCase);

module.exports = function formatBuilderName(type) {
// FunctionExpression -> functionExpression
// JSXIdentifier -> jsxIdentifier
return type.replace(/^([A-Z](?=[a-z])|[A-Z]+(?=[A-Z]))/, toLowerCase);
};
Loading

0 comments on commit a2aabbd

Please sign in to comment.