Skip to content

Commit

Permalink
feat(sdfe-to-dts): support <class />
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Apr 15, 2018
1 parent 011e222 commit ca127fb
Show file tree
Hide file tree
Showing 4 changed files with 3,319 additions and 338 deletions.
47 changes: 41 additions & 6 deletions packages/@jxa/sdef-to-dts/src/sdef-to-dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const isCommand = (node: Node): node is Command => {
const isRecord = (node: Node): node is Record => {
return node.name === "record-type";
};
const isClass = (node: Node): node is Class => {
return node.name === "class";
};
const isClassExtend = (node: Node): node is ClassExtend => {
return node.name === "class-extend";
};

interface Command extends RootNode {
name: "command";
Expand All @@ -55,6 +61,13 @@ interface Record extends RootNode {
name: "record";
}

interface ClassExtend extends RootNode {
name: "class-extend";
}

interface Class extends RootNode {
name: "class";
}

const convertJSONSchemaType = (type: string): string => {
switch (type) {
Expand Down Expand Up @@ -125,7 +138,7 @@ const createOptionalParameter = (name: string, parameters: Node[]): Promise<stri
};


const recordToJSONSchema = (command: Record): JSONSchema => {
const recordToJSONSchema = (command: Record | Class): JSONSchema => {
// https://www.npmjs.com/package/json-schema-to-typescript
const pascalCaseName = pascalCase(command.attributes.name);
const description = command.attributes.description;
Expand Down Expand Up @@ -222,6 +235,18 @@ ${directParametersDocs.join("\n")}${
}
};


const schemaToInterfaces = async (schemas: JSONSchema[]): Promise<string> => {
const results = await Promise.all(schemas.map(schema => {
const title = schema.title!;
return compile(schema, title).then(schema => {
// TODO: prevent UIElement -> UiElement
// https://github.com/bcherny/json-schema-to-typescript/blob/fadb879a5373f20fd9d1f441168494003e825239/src/utils.ts#L56
return schema.replace(new RegExp(`interface ${title}`, "i"), `interface ${title}`);
});
}));
return results.join("\n");
};
export const transform = async (namespace: string, sdefContent: string) => {
if (!isVarName(namespace)) {
throw new Error(`${namespace} can not to be used for namespace, because it includes invalid string.`);
Expand All @@ -231,32 +256,42 @@ export const transform = async (namespace: string, sdefContent: string) => {
const suites = dictionary.children.filter(node => node.name === "suite");
const commands: Command[] = [];
const records: Record[] = [];
const classes: Class[] = [];
const classExtends: ClassExtend[] = [];
// TODO: support enum
suites.forEach(suite => {
suite.children.forEach((node: Node) => {
if (isCommand(node)) {
commands.push(node);
} else if (isRecord(node)) {
records.push(node);
} else if (isClass(node)) {
classes.push(node)
} else if (isClassExtend(node)) {
classExtends.push(node);
}
})
});
const recordSchema = records.map(record => {
return recordToJSONSchema(record);
});
const recordDefinitions = await Promise.all(recordSchema.map(schema => {
return compile(schema, schema.title!)
}));
const classSchema = classes.map(node => {
return recordToJSONSchema(node);
});
const recordDefinitions = await schemaToInterfaces(recordSchema);
const classDefinitions = await schemaToInterfaces(classSchema);
const optionalBindingMap = new Map<string, number>();
const functionDefinitions = await Promise.all(commands.map(command => {
return commandToDeclare(namespace, command, recordSchema, optionalBindingMap);
return commandToDeclare(namespace, command, recordSchema.concat(classSchema), optionalBindingMap);
}));
const functionDefinitionHeaders = functionDefinitions.map(def => def.header);
const functionDefinitionBodies = functionDefinitions.map(def => def.body);
return `
export namespace ${namespace} {
// Class
${indentString(classDefinitions)}
// Records
${indentString(recordDefinitions.join("\n"), 4)}
${indentString(recordDefinitions)}
// Function options
${indentString(functionDefinitionHeaders.join("\n"), 4)}
}
Expand Down
4 changes: 0 additions & 4 deletions packages/@jxa/sdef-to-dts/test/example.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { StandardAdditions } from "./fixtures/StandardAdditions/output";
import { SystemEvents,StandardAdditions} from "./fixtures/System Events/output";

export interface Application extends StandardAdditions {
id(): string;
}

declare global {
export function Application(name: string): Application
export function SystemEvents(name: string): SystemEvents
}

Application("").currentDate();
Loading

0 comments on commit ca127fb

Please sign in to comment.