Skip to content

Commit

Permalink
feat: Add ObjectLiteralElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Apr 6, 2019
1 parent 4e1464e commit a40a46d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 90 deletions.
28 changes: 3 additions & 25 deletions lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,11 +853,6 @@ export declare class TypeGuards {
* @param node - Node to check.
*/
static isClassDeclaration(node: Node): node is ClassDeclaration;
/**
* Gets if the node is a ClassElement.
* @param node - Node to check.
*/
static isClassElement(node: Node): node is ClassElement;
/**
* Gets if the node is a ClassExpression.
* @param node - Node to check.
Expand Down Expand Up @@ -1048,11 +1043,6 @@ export declare class TypeGuards {
* @param node - Node to check.
*/
static isFunctionLikeDeclaration(node: Node): node is FunctionLikeDeclaration & FunctionLikeDeclarationExtensionType;
/**
* Gets if the node is a FunctionOrConstructorTypeNodeBase.
* @param node - Node to check.
*/
static isFunctionOrConstructorTypeNodeBase(node: Node): node is FunctionOrConstructorTypeNodeBase;
/**
* Gets if the node is a FunctionTypeNode.
* @param node - Node to check.
Expand Down Expand Up @@ -5654,7 +5644,7 @@ export declare class ObjectLiteralExpression extends ObjectLiteralExpressionBase
insertSetAccessors(index: number, structures: ReadonlyArray<OptionalKind<SetAccessorDeclarationStructure>>): SetAccessorDeclaration[];
}

declare const PropertyAssignmentBase: Constructor<InitializerExpressionGetableNode> & Constructor<QuestionTokenableNode> & Constructor<PropertyNamedNode> & typeof Node;
declare const PropertyAssignmentBase: Constructor<InitializerExpressionGetableNode> & Constructor<QuestionTokenableNode> & Constructor<PropertyNamedNode> & typeof ObjectLiteralElement;

export declare class PropertyAssignment extends PropertyAssignmentBase<ts.PropertyAssignment> {
/**
Expand All @@ -5668,10 +5658,6 @@ export declare class PropertyAssignment extends PropertyAssignmentBase<ts.Proper
* @param textOrWriterFunction - New text ot set for the initializer.
*/
setInitializer(textOrWriterFunction: string | WriterFunction): this;
/**
* Removes this property.
*/
remove(): void;
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand All @@ -5683,7 +5669,7 @@ export declare class PropertyAssignment extends PropertyAssignmentBase<ts.Proper
getStructure(): PropertyAssignmentStructure;
}

declare const ShorthandPropertyAssignmentBase: Constructor<InitializerExpressionGetableNode> & Constructor<QuestionTokenableNode> & Constructor<NamedNode> & typeof Node;
declare const ShorthandPropertyAssignmentBase: Constructor<InitializerExpressionGetableNode> & Constructor<QuestionTokenableNode> & Constructor<NamedNode> & typeof ObjectLiteralElement;

export declare class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase<ts.ShorthandPropertyAssignment> {
/**
Expand Down Expand Up @@ -5719,10 +5705,6 @@ export declare class ShorthandPropertyAssignment extends ShorthandPropertyAssign
* @param text - New text to set for the initializer.
*/
setInitializer(text: string): PropertyAssignment;
/**
* Removes this property.
*/
remove(): void;
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand All @@ -5734,13 +5716,9 @@ export declare class ShorthandPropertyAssignment extends ShorthandPropertyAssign
getStructure(): ShorthandPropertyAssignmentStructure;
}

declare const SpreadAssignmentBase: Constructor<ExpressionedNode> & typeof Node;
declare const SpreadAssignmentBase: Constructor<ExpressionedNode> & typeof ObjectLiteralElement;

export declare class SpreadAssignment extends SpreadAssignmentBase<ts.SpreadAssignment> {
/**
* Removes this property.
*/
remove(): void;
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand Down
8 changes: 6 additions & 2 deletions scripts/generation/createTypeGuardsUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
.filter(m => m.getName().startsWith("is"))
.forEach(m => m.remove());

typeGuardsClass.addMethods(getMethodInfos().map(method => ({
typeGuardsClass.addMethods(getMethodInfos().filter(n => isAllowedClass(n.name.replace(/^is/, ""))).map(method => ({
name: `is${method.name}`,
isStatic: true,
docs: [{
Expand All @@ -54,7 +54,7 @@ export function createTypeGuardsUtility(inspector: TsMorphInspector) {
function getMethodInfos() {
const methodInfos = new KeyValueCache<string, MethodInfo>();

for (const node of inspector.getWrappedNodes().filter(n => isAllowedClass(n.getName()))) {
for (const node of inspector.getWrappedNodes()) {
// todo: what is going on here? Why does this need to be filled
getMethodInfoForNode(node); // fill this
const nodeBase = node.getBase();
Expand Down Expand Up @@ -183,6 +183,10 @@ function isAllowedClass(name: string) {
case "AssignmentExpression":
case "SuperElementAccessExpression":
case "SuperPropertyAccessExpression":
case "SuperExpressionedNode":
// ignore these for now because they're not implemented properly
case "ClassElement":
case "ObjectLiteralElement":
return false;
default:
return true;
Expand Down
14 changes: 14 additions & 0 deletions src/compiler/ast/expression/object/ObjectLiteralElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { removeCommaSeparatedChild } from "../../../../manipulation";
import { ts } from "../../../../typescript";
import { Node } from "../../common";

// todo: There are ClassElement nodes like MethodDeclaration that should implement this as well so what's done here isn't really correct

export class ObjectLiteralElement<T extends ts.ObjectLiteralElement = ts.ObjectLiteralElement> extends Node<T> {
/**
* Removes the object literal element from the object literal expression.
*/
remove() {
removeCommaSeparatedChild(this);
}
}
11 changes: 2 additions & 9 deletions src/compiler/ast/expression/object/PropertyAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { WriterFunction } from "../../../../types";
import { SyntaxKind, ts } from "../../../../typescript";
import { getTextFromStringOrWriter } from "../../../../utils";
import { InitializerExpressionGetableNode, PropertyNamedNode, QuestionTokenableNode } from "../../base";
import { Node } from "../../common";
import { ShorthandPropertyAssignment } from "./ShorthandPropertyAssignment";
import { PropertyAssignmentStructure, PropertyAssignmentSpecificStructure, StructureKind } from "../../../../structures";
import { callBaseSet } from "../../callBaseSet";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { ObjectLiteralElement } from "./ObjectLiteralElement";

// This node only has a question token in order to tell the user about bad code.
// (See https://github.com/Microsoft/TypeScript/pull/5121/files)

export const PropertyAssignmentBase = InitializerExpressionGetableNode(QuestionTokenableNode(PropertyNamedNode(Node)));
export const PropertyAssignmentBase = InitializerExpressionGetableNode(QuestionTokenableNode(PropertyNamedNode(ObjectLiteralElement)));
export class PropertyAssignment extends PropertyAssignmentBase<ts.PropertyAssignment> {
/**
* Removes the initializer and returns the new shorthand property assignment.
Expand Down Expand Up @@ -58,13 +58,6 @@ export class PropertyAssignment extends PropertyAssignmentBase<ts.PropertyAssign
return this;
}

/**
* Removes this property.
*/
remove() {
removeCommaSeparatedChild(this);
}

/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand Down
13 changes: 3 additions & 10 deletions src/compiler/ast/expression/object/ShorthandPropertyAssignment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as errors from "../../../../errors";
import { insertIntoParentTextRange, removeChildren, removeCommaSeparatedChild } from "../../../../manipulation";
import { insertIntoParentTextRange, removeChildren } from "../../../../manipulation";
import { SyntaxKind, ts } from "../../../../typescript";
import { InitializerExpressionGetableNode, NamedNode, QuestionTokenableNode } from "../../base";
import { Node } from "../../common/Node";
import { Expression } from "../Expression";
import { PropertyAssignment } from "./PropertyAssignment";
import { ShorthandPropertyAssignmentStructure, ShorthandPropertyAssignmentSpecificStructure, QuestionTokenableNodeStructure, StructureKind } from "../../../../structures";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { callBaseSet } from "../../callBaseSet";
import { ObjectLiteralElement } from "./ObjectLiteralElement";

// This node only has an object assignment initializer, equals token, and question token, in order to tell the user about bad code
// (See https://github.com/Microsoft/TypeScript/pull/5121/files)

export const ShorthandPropertyAssignmentBase = InitializerExpressionGetableNode(QuestionTokenableNode(NamedNode(Node)));
export const ShorthandPropertyAssignmentBase = InitializerExpressionGetableNode(QuestionTokenableNode(NamedNode(ObjectLiteralElement)));
export class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase<ts.ShorthandPropertyAssignment> {
/**
* Gets if the shorthand property assignment has an object assignment initializer.
Expand Down Expand Up @@ -91,13 +91,6 @@ export class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase
return parent.getChildAtIndexIfKindOrThrow(childIndex, SyntaxKind.PropertyAssignment) as PropertyAssignment;
}

/**
* Removes this property.
*/
remove() {
removeCommaSeparatedChild(this);
}

/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand Down
12 changes: 2 additions & 10 deletions src/compiler/ast/expression/object/SpreadAssignment.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { ts } from "../../../../typescript";
import { Node } from "../../common/Node";
import { ExpressionedNode } from "../expressioned";
import { removeCommaSeparatedChild } from "../../../../manipulation";
import { SpreadAssignmentStructure, SpreadAssignmentSpecificStructure, StructureKind, ExpressionedNodeStructure } from "../../../../structures";
import { callBaseSet } from "../../callBaseSet";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { ObjectLiteralElement } from "./ObjectLiteralElement";

export const SpreadAssignmentBase = ExpressionedNode(Node);
export const SpreadAssignmentBase = ExpressionedNode(ObjectLiteralElement);
export class SpreadAssignment extends SpreadAssignmentBase<ts.SpreadAssignment> {
/**
* Removes this property.
*/
remove() {
removeCommaSeparatedChild(this);
}

/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
Expand Down
31 changes: 0 additions & 31 deletions src/utils/TypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,23 +382,6 @@ export class TypeGuards {
return node.getKind() === SyntaxKind.ClassDeclaration;
}

/**
* Gets if the node is a ClassElement.
* @param node - Node to check.
*/
static isClassElement(node: compiler.Node): node is compiler.ClassElement {
switch (node.getKind()) {
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.SetAccessor:
return true;
default:
return false;
}
}

/**
* Gets if the node is a ClassExpression.
* @param node - Node to check.
Expand Down Expand Up @@ -829,20 +812,6 @@ export class TypeGuards {
}
}

/**
* Gets if the node is a FunctionOrConstructorTypeNodeBase.
* @param node - Node to check.
*/
static isFunctionOrConstructorTypeNodeBase(node: compiler.Node): node is compiler.FunctionOrConstructorTypeNodeBase {
switch (node.getKind()) {
case SyntaxKind.ConstructorType:
case SyntaxKind.FunctionType:
return true;
default:
return false;
}
}

/**
* Gets if the node is a FunctionTypeNode.
* @param node - Node to check.
Expand Down
7 changes: 4 additions & 3 deletions wrapped-nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method

## Exist

**Total:** 172
**Total:** 173

* [ArrayBindingPattern](src/compiler/ast/binding/ArrayBindingPattern.ts)
* :heavy_check_mark: elements
Expand Down Expand Up @@ -276,6 +276,8 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* :heavy_check_mark: elements
* [ObjectDestructuringAssignment](src/compiler/ast/expression/object/ObjectDestructuringAssignment.ts)
* :heavy_check_mark: left
* [ObjectLiteralElement](src/compiler/ast/expression/object/ObjectLiteralElement.ts)
* :heavy_check_mark: name
* [ObjectLiteralExpression](src/compiler/ast/expression/object/ObjectLiteralExpression.ts)
* [OmittedExpression](src/compiler/ast/expression/OmittedExpression.ts)
* [ParameterDeclaration](src/compiler/ast/function/ParameterDeclaration.ts)
Expand Down Expand Up @@ -432,7 +434,7 @@ The disadvantage to a node not being wrapped is that it won't have helper method

## Not Exist

**Total:** 56
**Total:** 55

* BigIntLiteral
* Bundle
Expand Down Expand Up @@ -468,7 +470,6 @@ The disadvantage to a node not being wrapped is that it won't have helper method
* ModuleDeclaration
* NamespaceExportDeclaration
* NodeWithTypeArguments
* ObjectLiteralElement
* ObjectLiteralExpressionBase
* OptionalTypeNode
* PropertyAccessEntityNameExpression
Expand Down

0 comments on commit a40a46d

Please sign in to comment.