Skip to content

Commit

Permalink
feat(flags): isStatic (#91)
Browse files Browse the repository at this point in the history
Add isStatic flag to various declarations
  • Loading branch information
buehler authored Aug 28, 2018
1 parent 5ffa0e3 commit 617764d
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/declarations/AccessorDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbstractDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
import { AbstractDeclaration, ScopedDeclaration, StaticDeclaration, TypedDeclaration } from './Declaration';
import { DeclarationVisibility } from './DeclarationVisibility';

/**
Expand All @@ -11,12 +11,17 @@ import { DeclarationVisibility } from './DeclarationVisibility';
* @implements {TypedDeclaration}
* @implements {AbstractDeclaration}
*/
export abstract class AccessorDeclaration implements ScopedDeclaration, TypedDeclaration, AbstractDeclaration {
export abstract class AccessorDeclaration implements
ScopedDeclaration,
StaticDeclaration,
TypedDeclaration,
AbstractDeclaration {
constructor(
public name: string,
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public isAbstract: boolean,
public isStatic: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
17 changes: 17 additions & 0 deletions src/declarations/Declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,20 @@ export interface OptionalDeclaration extends Declaration {
*/
isOptional: boolean;
}

/**
* Interface for possible static declarations.
*
* @export
* @interface StaticDeclaration
* @extends {Declaration}
*/
export interface StaticDeclaration extends Declaration {
/**
* Defines if the declaration is static or not.
*
* @type {boolean}
* @memberof StaticDeclaration
*/
isStatic: boolean;
}
3 changes: 3 additions & 0 deletions src/declarations/MethodDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
StaticDeclaration,
TypedDeclaration,
} from './Declaration';
import { DeclarationVisibility } from './DeclarationVisibility';
Expand All @@ -24,6 +25,7 @@ export class MethodDeclaration implements
CallableDeclaration,
OptionalDeclaration,
ScopedDeclaration,
StaticDeclaration,
TypedDeclaration {

public parameters: ParameterDeclaration[] = [];
Expand All @@ -35,6 +37,7 @@ export class MethodDeclaration implements
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public isOptional: boolean,
public isStatic: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
5 changes: 3 additions & 2 deletions src/declarations/PropertyDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OptionalDeclaration, ScopedDeclaration, TypedDeclaration } from './Declaration';
import { OptionalDeclaration, ScopedDeclaration, StaticDeclaration, TypedDeclaration } from './Declaration';
import { DeclarationVisibility } from './DeclarationVisibility';

/**
Expand All @@ -9,12 +9,13 @@ import { DeclarationVisibility } from './DeclarationVisibility';
* @implements {ScopedDeclaration}
* @implements {TypedDeclaration}
*/
export class PropertyDeclaration implements OptionalDeclaration, ScopedDeclaration, TypedDeclaration {
export class PropertyDeclaration implements OptionalDeclaration, ScopedDeclaration, StaticDeclaration, TypedDeclaration {
constructor(
public name: string,
public visibility: DeclarationVisibility | undefined,
public type: string | undefined,
public isOptional: boolean,
public isStatic: boolean,
public start?: number,
public end?: number,
) { }
Expand Down
7 changes: 7 additions & 0 deletions src/node-parser/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import { parseFunctionParts, parseMethodParams } from './function-parser';
import { parseIdentifier } from './identifier-parser';
import {
containsModifier,
getDefaultResourceIdentifier,
getNodeType,
getNodeVisibility,
Expand Down Expand Up @@ -89,6 +90,7 @@ export function parseCtorParams(
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand Down Expand Up @@ -139,6 +141,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand All @@ -151,6 +154,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand All @@ -166,6 +170,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
getNodeVisibility(o),
getNodeType(o.type),
o.modifiers !== undefined && o.modifiers.some(m => m.kind === SyntaxKind.AbstractKeyword),
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand All @@ -179,6 +184,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
getNodeVisibility(o),
getNodeType(o.type),
o.modifiers !== undefined && o.modifiers.some(m => m.kind === SyntaxKind.AbstractKeyword),
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand All @@ -197,6 +203,7 @@ export function parseClass(tsResource: Resource, node: ClassDeclaration): void {
getNodeVisibility(o),
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
);
Expand Down
12 changes: 10 additions & 2 deletions src/node-parser/interface-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Identifier, InterfaceDeclaration } from 'typescript';
import { Identifier, InterfaceDeclaration, SyntaxKind } from 'typescript';

import { DeclarationVisibility } from '../declarations/DeclarationVisibility';
import { DefaultDeclaration } from '../declarations/DefaultDeclaration';
Expand All @@ -8,7 +8,13 @@ import { PropertyDeclaration } from '../declarations/PropertyDeclaration';
import { Resource } from '../resources/Resource';
import { isMethodSignature, isPropertySignature } from '../type-guards/TypescriptGuards';
import { parseMethodParams } from './function-parser';
import { getDefaultResourceIdentifier, getNodeType, isNodeDefaultExported, isNodeExported } from './parse-utilities';
import {
containsModifier,
getDefaultResourceIdentifier,
getNodeType,
isNodeDefaultExported,
isNodeExported,
} from './parse-utilities';

/**
* Parses an interface node into its declaration.
Expand Down Expand Up @@ -38,6 +44,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
),
Expand All @@ -49,6 +56,7 @@ export function parseInterface(resource: Resource, node: InterfaceDeclaration):
DeclarationVisibility.Public,
getNodeType(o.type),
!!o.questionToken,
containsModifier(o, SyntaxKind.StaticKeyword),
o.getStart(),
o.getEnd(),
);
Expand Down
13 changes: 13 additions & 0 deletions src/node-parser/parse-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export function getNodeType(node: TypeNode | undefined): string | undefined {
return node ? node.getText() : undefined;
}

/**
* Checks if a node contains a certain modifier (of a given kind)
*
* @export
* @param {Node} node
* @param {SyntaxKind} modifierKind
* @returns {boolean}
*/
export function containsModifier(node: Node, modifierKind: SyntaxKind): boolean {
if (!node.modifiers) return false;
return node.modifiers.some(mod => mod.kind === modifierKind);
}

/**
* Returns the enum value (visibility) of a node.
*
Expand Down
8 changes: 7 additions & 1 deletion test/TypescriptParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('TypescriptParser', () => {
});

it('should parse a file', () => {
expect(parsed.declarations).toHaveLength(9);
expect(parsed.declarations).toHaveLength(10);
});

it('should parse an abstract class', () => {
Expand Down Expand Up @@ -547,6 +547,12 @@ describe('TypescriptParser', () => {
expect(parsedClass.properties).toMatchSnapshot();
});

it('should parse static class properties and methods', () => {
const parsedClass = parsed.declarations[9] as ClassDeclaration;

expect(parsedClass).toMatchSnapshot();
});

});

describe('Modules', () => {
Expand Down
Loading

0 comments on commit 617764d

Please sign in to comment.