Generate better builder names for JSX* and TS* (#6967)
e.g. JSXIdentifier -> jsxIdentifier. The jSXIdentifier alias isn't removed, so this commit doesn't introduce breaking changes.
This commit is contained in:
@@ -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() {
|
||||
@@ -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 => {
|
||||
@@ -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;
|
||||
|
||||
9
packages/babel-types/scripts/utils/formatBuilderName.js
Normal file
9
packages/babel-types/scripts/utils/formatBuilderName.js
Normal file
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user