Skip to content

Commit

Permalink
fix: Fix out of date TypeGuards by regenerating them.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Apr 8, 2018
1 parent c76a4d3 commit 23eca82
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
2 changes: 1 addition & 1 deletion scripts/createCompilerApiLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function createCompilerApiLayer(factory: InspectorFactory) {
writer.write("ObjectUtils.assign((ts as any), tsCompiler);");
});

sourceFile.replaceWithText(sourceFile.getFullText().replace(/\r?\n/g, "\r\n").replace(/(\r\n)+$/, "\r\n"));
sourceFile.replaceWithText(sourceFile.getFullText().replace(/ *\r?\n/g, "\r\n").replace(/(\r\n)+$/, "\r\n"));

function addSeparatedDeclarations() {
for (const enumDec of allEnums.filter(e => enumsToSeparate.indexOf(e.getName()) >= 0))
Expand Down
2 changes: 1 addition & 1 deletion scripts/createGetStructureFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {Structure} from "./inspectors";
// todo: a lot of this code was written before this library supported manipulation

export function createGetStructureFunctions(structures: Structure[]) {
const writer = new CodeBlockWriter();
const writer = new CodeBlockWriter({ newLine: "\r\n" });

writer.writeLine("/* tslint:disable */");
writer.writeLine("// DO NOT MANUALLY EDIT!! File generated via: npm run code-generate").newLine();
Expand Down
10 changes: 9 additions & 1 deletion scripts/createTypeGuardsUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function createTypeGuardsUtility(inspector: TsSimpleAstInspector) {
return ArrayUtils.sortByProperty(methodInfos.getValuesAsArray(), info => info.name);

function fillBase(node: WrappedNode, nodeBase: WrappedNode) {
if (nodeBase.getName() === "Node")
if (!isAllowedName(nodeBase.getName()))
return;
const nodeBaseBase = nodeBase.getBase();
if (nodeBaseBase != null)
Expand All @@ -95,6 +95,8 @@ export function createTypeGuardsUtility(inspector: TsSimpleAstInspector) {

function fillMixinable(node: WrappedNode, mixinable: WrappedNode | Mixin) {
for (const mixin of mixinable.getMixins()) {
if (!isAllowedName(mixin.getName()))
continue;
getMethodInfoForMixin(mixin).syntaxKinds.push(...getSyntaxKindsForName(node.getName()));
fillMixinable(node, mixin);
}
Expand Down Expand Up @@ -137,6 +139,12 @@ export function createTypeGuardsUtility(inspector: TsSimpleAstInspector) {
}
}

function isAllowedName(name: string) {
if (name === "Node" || name.endsWith("Specific"))
return false;
return true;
}

function isAllowedClass(name: string) {
switch (name) {
// todo: should support these classes eventually (they probably need to be customly implemented)
Expand Down
20 changes: 10 additions & 10 deletions src/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@ export namespace ts {
*/
export interface Iterator<T> {
next(): {
value: T;
done: false;
} | {
value: never;
done: true;
};
value: T;
done: false;
} | {
value: never;
done: true;
};
}

/**
Expand Down Expand Up @@ -1882,8 +1882,8 @@ export namespace ts {
export interface JSDocAugmentsTag extends JSDocTag {
kind: SyntaxKind.JSDocAugmentsTag;
class: ExpressionWithTypeArguments & {
expression: Identifier | PropertyAccessEntityNameExpression;
};
expression: Identifier | PropertyAccessEntityNameExpression;
};
}

export interface JSDocClassTag extends JSDocTag {
Expand Down Expand Up @@ -2173,8 +2173,8 @@ export namespace ts {
* Note that the resulting nodes cannot be checked.
*/
signatureToSignatureDeclaration(signature: Signature, kind: SyntaxKind, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): SignatureDeclaration & {
typeArguments?: NodeArray<TypeNode>;
} | undefined;
typeArguments?: NodeArray<TypeNode>;
} | undefined;
/**
* Note that the resulting nodes cannot be checked.
*/
Expand Down
44 changes: 42 additions & 2 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,7 +1403,13 @@ export class TypeGuards {
* @param node - Node to check.
*/
static isNameableNode(node: compiler.Node): node is compiler.NameableNode & compiler.Node {
return node.getKind() === SyntaxKind.FunctionExpression;
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.FunctionExpression:
return true;
default:
return false;
}
}

/**
Expand All @@ -1412,7 +1418,6 @@ export class TypeGuards {
*/
static isNamedNode(node: compiler.Node): node is compiler.NamedNode & compiler.Node {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.MetaProperty:
case SyntaxKind.PropertyAccessExpression:
Expand Down Expand Up @@ -1749,6 +1754,41 @@ export class TypeGuards {
}
}

/**
* Gets if the node is a ReferenceFindableNode.
* @param node - Node to check.
*/
static isReferenceFindableNode(node: compiler.Node): node is compiler.ReferenceFindableNode & compiler.Node {
switch (node.getKind()) {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SetAccessor:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumMember:
case SyntaxKind.MetaProperty:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.Parameter:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.PropertySignature:
case SyntaxKind.JsxAttribute:
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.VariableDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.TypeParameter:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.ShorthandPropertyAssignment:
return true;
default:
return false;
}
}

/**
* Gets if the node is a RegularExpressionLiteral.
* @param node - Node to check.
Expand Down

0 comments on commit 23eca82

Please sign in to comment.