Fix types

This commit is contained in:
syuilo 2019-05-01 19:31:34 +09:00
parent 8b13e3c327
commit a9fc176c3c
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
2 changed files with 9 additions and 9 deletions

View file

@ -44,9 +44,9 @@ export type Variable = Block & {
name: string; name: string;
}; };
export type Type = 'string' | 'number' | 'boolean' | 'stringArray'; export type Type = 'string' | 'number' | 'boolean' | 'stringArray' | null;
export const funcDefs = { export const funcDefs: Record<string, { in: any[]; out: any; category: string; icon: any; }> = {
if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, }, if: { in: ['boolean', 0, 0], out: 0, category: 'flow', icon: faShareAlt, },
for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, }, for: { in: ['number', 'function'], out: null, category: 'flow', icon: faRecycle, },
not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, }, not: { in: ['boolean'], out: 'boolean', category: 'logical', icon: faFlag, },
@ -81,7 +81,7 @@ export const funcDefs = {
seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, }, seedRandomPick: { in: [null, 0], out: 0, category: 'random', icon: faDice, },
}; };
export const literalDefs = { export const literalDefs: Record<string, { out: any; category: string; icon: any; }> = {
text: { out: 'string', category: 'value', icon: faQuoteRight, }, text: { out: 'string', category: 'value', icon: faQuoteRight, },
multiLineText: { out: 'string', category: 'value', icon: faAlignLeft, }, multiLineText: { out: 'string', category: 'value', icon: faAlignLeft, },
textList: { out: 'stringArray', category: 'value', icon: faList, }, textList: { out: 'stringArray', category: 'value', icon: faList, },
@ -105,7 +105,7 @@ export function isFnBlock(block: Block): block is FnBlock {
export type PageVar = { name: string; value: any; type: Type; }; export type PageVar = { name: string; value: any; type: Type; };
export const envVarsDef = { export const envVarsDef: Record<string, Type> = {
AI: 'string', AI: 'string',
URL: 'string', URL: 'string',
VERSION: 'string', VERSION: 'string',

View file

@ -58,7 +58,7 @@ export class ASTypeChecker {
} }
@autobind @autobind
public getExpectedType(v: Block, slot: number): Type | null { public getExpectedType(v: Block, slot: number): Type {
const def = funcDefs[v.type]; const def = funcDefs[v.type];
if (def == null) { if (def == null) {
throw new Error('Unknown type: ' + v.type); throw new Error('Unknown type: ' + v.type);
@ -86,7 +86,7 @@ export class ASTypeChecker {
} }
@autobind @autobind
public infer(v: Block): Type | null { public infer(v: Block): Type {
if (v.type === null) return null; if (v.type === null) return null;
if (v.type === 'text') return 'string'; if (v.type === 'text') return 'string';
if (v.type === 'multiLineText') return 'string'; if (v.type === 'multiLineText') return 'string';
@ -140,19 +140,19 @@ export class ASTypeChecker {
} }
@autobind @autobind
public getVarsByType(type: Type | null): Variable[] { public getVarsByType(type: Type): Variable[] {
if (type == null) return this.variables; if (type == null) return this.variables;
return this.variables.filter(x => (this.infer(x) === null) || (this.infer(x) === type)); return this.variables.filter(x => (this.infer(x) === null) || (this.infer(x) === type));
} }
@autobind @autobind
public getEnvVarsByType(type: Type | null): string[] { public getEnvVarsByType(type: Type): string[] {
if (type == null) return Object.keys(envVarsDef); if (type == null) return Object.keys(envVarsDef);
return Object.entries(envVarsDef).filter(([k, v]) => v === null || type === v).map(([k, v]) => k); return Object.entries(envVarsDef).filter(([k, v]) => v === null || type === v).map(([k, v]) => k);
} }
@autobind @autobind
public getPageVarsByType(type: Type | null): string[] { public getPageVarsByType(type: Type): string[] {
if (type == null) return this.pageVars.map(v => v.name); if (type == null) return this.pageVars.map(v => v.name);
return this.pageVars.filter(v => type === v.type).map(v => v.name); return this.pageVars.filter(v => type === v.type).map(v => v.name);
} }