Fix generate interfaces script (#6031)

* Fix typo in TSPropertySignature type definition

* Sort fields in generate-interfaces script
This commit is contained in:
Brian Ng 2017-08-01 13:38:46 -05:00 committed by Henry Zhu
parent 889f4e7791
commit 21eeed8a8c
4 changed files with 675 additions and 181 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2183,7 +2183,7 @@ Aliases: `TSTypeElement`
- `key`: `Expression` (required) - `key`: `Expression` (required)
- `typeAnnotation`: `TypeAnnotation` (default: `null`) - `typeAnnotation`: `TypeAnnotation` (default: `null`)
- `initializer`: `Expresssion` (default: `null`) - `initializer`: `Expression` (default: `null`)
- `computed`: `boolean` (default: `null`) - `computed`: `boolean` (default: `null`)
- `optional`: `boolean` (default: `null`) - `optional`: `boolean` (default: `null`)
- `readonly`: `boolean` (default: `null`) - `readonly`: `boolean` (default: `null`)

View File

@ -115,7 +115,7 @@ defineType("TSPropertySignature", {
...namedTypeElementCommon, ...namedTypeElementCommon,
readonly: validateOptional(bool), readonly: validateOptional(bool),
typeAnnotation: validateOptionalType("TypeAnnotation"), typeAnnotation: validateOptionalType("TypeAnnotation"),
initializer: validateOptionalType("Expresssion"), initializer: validateOptionalType("Expression"),
}, },
}); });

View File

@ -54,56 +54,67 @@ for (const type in t.NODE_FIELDS) {
const struct = ['type: "' + type + '";']; const struct = ['type: "' + type + '";'];
const args = []; const args = [];
for (const fieldName in fields) { Object.keys(t.NODE_FIELDS[type])
const field = fields[fieldName]; .sort((fieldA, fieldB) => {
const indexA = t.BUILDER_KEYS[type].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[type].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(fieldName => {
const field = fields[fieldName];
let suffix = ""; let suffix = "";
if (field.optional || field.default != null) suffix += "?"; if (field.optional || field.default != null) suffix += "?";
let typeAnnotation = "any"; let typeAnnotation = "any";
const validate = field.validate; const validate = field.validate;
if (validate) { if (validate) {
if (validate.oneOf) { if (validate.oneOf) {
typeAnnotation = validate.oneOf typeAnnotation = validate.oneOf
.map(function(val) { .map(function(val) {
return JSON.stringify(val); return JSON.stringify(val);
}) })
.join(" | "); .join(" | ");
} }
if (validate.type) { if (validate.type) {
typeAnnotation = validate.type; typeAnnotation = validate.type;
if (typeAnnotation === "array") { if (typeAnnotation === "array") {
typeAnnotation = "Array<any>"; typeAnnotation = "Array<any>";
}
}
if (validate.oneOfNodeTypes) {
const types = validate.oneOfNodeTypes.map(
type => `${NODE_PREFIX}${type}`
);
typeAnnotation = types.join(" | ");
if (suffix === "?") typeAnnotation = "?" + typeAnnotation;
} }
} }
if (validate.oneOfNodeTypes) { if (typeAnnotation) {
const types = validate.oneOfNodeTypes.map( suffix += ": " + typeAnnotation;
type => `${NODE_PREFIX}${type}`
);
typeAnnotation = types.join(" | ");
if (suffix === "?") typeAnnotation = "?" + typeAnnotation;
} }
}
if (typeAnnotation) { args.push(t.toBindingIdentifierName(fieldName) + suffix);
suffix += ": " + typeAnnotation;
}
args.push(t.toBindingIdentifierName(fieldName) + suffix); if (t.isValidIdentifier(fieldName)) {
if (!t.isValidIdentifier(fieldName)) continue; struct.push(fieldName + suffix + ";");
struct.push(fieldName + suffix + ";"); }
} });
code += `declare class ${NODE_PREFIX}${type} extends ${NODE_PREFIX} { code += `declare class ${NODE_PREFIX}${type} extends ${NODE_PREFIX} {
${struct.join("\n ").trim()} ${struct.join("\n ").trim()}
}\n\n`; }\n\n`;
// Flow chokes on super() :/ // Flow chokes on super() and import() :/
if (type !== "Super") { if (type !== "Super" && type !== "Import") {
lines.push( lines.push(
`declare function ${type[0].toLowerCase() + type.slice(1)}(${args.join( `declare function ${type[0].toLowerCase() + type.slice(1)}(${args.join(
", " ", "