Skip to content

Commit

Permalink
Reworked getVariableDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
egervari committed Oct 25, 2018
1 parent 2fde65a commit 423813f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/ast/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ export function getInterfaceDeclarationByType(sourceFile: typescript.SourceFile,
);
}

export function getVariableDeclaration(sourceFile: typescript.SourceFile, type: string) {
return sourceFile.statements
export function getVariableDeclaration(
sourceFile: typescript.SourceFile,
type: string
): typescript.VariableDeclaration {
const variableDeclarations = sourceFile.statements
.filter(statement => statement.kind === typescript.SyntaxKind.VariableStatement)
.map(statement => (statement as typescript.VariableStatement).declarationList.declarations[0])
.find(
declaration => (declaration.type as typescript.TypeReferenceNode).typeName.getText() === type
.map(statement => (statement as typescript.VariableStatement).declarationList.declarations)
.filter(declarations =>
declarations.some(
declaration =>
(declaration.type as typescript.TypeReferenceNode).typeName.getText() === type
)
);

return variableDeclarations.length > 0 ? variableDeclarations[0][0] : null;
}

export function getTypeArgumentOfVariableDeclaration(
Expand Down
1 change: 1 addition & 0 deletions src/ast/rework-app-reducer/rework-app-reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ describe('reworkAppReducer()', () => {
);
expect(changes[1].order).toEqual(195);
expect(changes[2].order).toEqual(272);
expect(changes[3].order).toEqual(327);
});
});
7 changes: 2 additions & 5 deletions src/rules/tree-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as strings from '@angular-devkit/core/src/utils/strings';
import { DirEntry, Tree } from '@angular-devkit/schematics';
import {
Change,
InsertChange
} from '@schematics/angular/utility/change';
import { Change, InsertChange } from '@schematics/angular/utility/change';
import * as typescript from 'typescript';

import { openSourceFile } from '../ast/ast-helpers';
Expand Down Expand Up @@ -68,7 +65,7 @@ export function applyChangesToTreeFile(tree: Tree, filename: string, changes: Ch
changes.forEach(change => {
if (change instanceof InsertChange) {
updateRecorder.insertLeft(change.pos, change.toAdd);
/*} else if (change instanceof ReplaceChange) {
/*} else if (change instanceof ReplaceChange) {
updateRecorder.remove(change.pos, change.oldText.length);
updateRecorder.insertLeft(change.order, change.newText);
} else if (change instanceof RemoveChange) {
Expand Down

0 comments on commit 423813f

Please sign in to comment.