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:
Nicolò Ribaudo
2017-12-07 12:17:40 +01:00
committed by GitHub
parent fcfa987926
commit a2aabbd33d
7 changed files with 101 additions and 8 deletions

View File

@@ -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;

View 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);
};