Skip to content

Commit

Permalink
refactor(schematics): merge ast-utils and route-utils into ast-utils (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ukrukarg authored and brandonroberts committed Mar 25, 2019
1 parent 3de9972 commit 89c8b56
Show file tree
Hide file tree
Showing 28 changed files with 721 additions and 805 deletions.
2 changes: 1 addition & 1 deletion modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
getDecoratorMetadata,
getContentOfKeyLiteral,
insertAfterLastOccurrence,
insertImport,
addBootstrapToModule,
addDeclarationToModule,
addExportToModule,
Expand Down Expand Up @@ -49,7 +50,6 @@ export {
} from './utility/ngrx-utils';

export { getProjectPath, getProject, isLib } from './utility/project';
export { insertImport } from './utility/route-utils';

export const stringUtils = {
dasherize,
Expand Down
103 changes: 101 additions & 2 deletions modules/effects/schematics-core/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { Change, InsertChange } from './change';
import { insertImport } from './route-utils';
import { Change, InsertChange, NoopChange } from './change';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
Expand Down Expand Up @@ -537,3 +536,103 @@ export function addBootstrapToModule(
importPath
);
}

/**
* Add Import `import { symbolName } from fileName` if the import doesn't exit
* already. Assumes fileToEdit can be resolved and accessed.
* @param fileToEdit (file we want to add import to)
* @param symbolName (item to import)
* @param fileName (path to the file)
* @param isDefault (if true, import follows style for importing default exports)
* @return Change
*/

export function insertImport(
source: ts.SourceFile,
fileToEdit: string,
symbolName: string,
fileName: string,
isDefault = false
): Change {
const rootNode = source;
const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);

// get nodes that map to import statements from the file fileName
const relevantImports = allImports.filter(node => {
// StringLiteral of the ImportDeclaration is the import file (fileName in this case).
const importFiles = node
.getChildren()
.filter(child => child.kind === ts.SyntaxKind.StringLiteral)
.map(n => (n as ts.StringLiteral).text);

return importFiles.filter(file => file === fileName).length === 1;
});

if (relevantImports.length > 0) {
let importsAsterisk = false;
// imports from import file
const imports: ts.Node[] = [];
relevantImports.forEach(n => {
Array.prototype.push.apply(
imports,
findNodes(n, ts.SyntaxKind.Identifier)
);
if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
importsAsterisk = true;
}
});

// if imports * from fileName, don't add symbolName
if (importsAsterisk) {
return new NoopChange();
}

const importTextNodes = imports.filter(
n => (n as ts.Identifier).text === symbolName
);

// insert import if it's not there
if (importTextNodes.length === 0) {
const fallbackPos =
findNodes(
relevantImports[0],
ts.SyntaxKind.CloseBraceToken
)[0].getStart() ||
findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();

return insertAfterLastOccurrence(
imports,
`, ${symbolName}`,
fileToEdit,
fallbackPos
);
}

return new NoopChange();
}

// no such import declaration exists
const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(
n => n.getText() === 'use strict'
);
let fallbackPos = 0;
if (useStrict.length > 0) {
fallbackPos = useStrict[0].end;
}
const open = isDefault ? '' : '{ ';
const close = isDefault ? '' : ' }';
// if there are no imports or 'use strict' statement, insert import at beginning of file
const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
const separator = insertAtBeginning ? '' : ';\n';
const toInsert =
`${separator}import ${open}${symbolName}${close}` +
` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;

return insertAfterLastOccurrence(
allImports,
toInsert,
fileToEdit,
fallbackPos,
ts.SyntaxKind.StringLiteral
);
}
3 changes: 1 addition & 2 deletions modules/effects/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { InsertChange, Change, NoopChange } from './change';
import { Tree, SchematicsException, Rule } from '@angular-devkit/schematics';
import { normalize } from '@angular-devkit/core';
import { buildRelativePath } from './find-module';
import { insertImport } from './route-utils';
import { addImportToModule } from './ast-utils';
import { addImportToModule, insertImport } from './ast-utils';

export function addReducerToState(options: any): Rule {
return (host: Tree) => {
Expand Down
110 changes: 0 additions & 110 deletions modules/effects/schematics-core/utility/route-utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
getDecoratorMetadata,
getContentOfKeyLiteral,
insertAfterLastOccurrence,
insertImport,
addBootstrapToModule,
addDeclarationToModule,
addExportToModule,
Expand Down Expand Up @@ -49,7 +50,6 @@ export {
} from './utility/ngrx-utils';

export { getProjectPath, getProject, isLib } from './utility/project';
export { insertImport } from './utility/route-utils';

export const stringUtils = {
dasherize,
Expand Down
103 changes: 101 additions & 2 deletions modules/entity/schematics-core/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { Change, InsertChange } from './change';
import { insertImport } from './route-utils';
import { Change, InsertChange, NoopChange } from './change';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
Expand Down Expand Up @@ -537,3 +536,103 @@ export function addBootstrapToModule(
importPath
);
}

/**
* Add Import `import { symbolName } from fileName` if the import doesn't exit
* already. Assumes fileToEdit can be resolved and accessed.
* @param fileToEdit (file we want to add import to)
* @param symbolName (item to import)
* @param fileName (path to the file)
* @param isDefault (if true, import follows style for importing default exports)
* @return Change
*/

export function insertImport(
source: ts.SourceFile,
fileToEdit: string,
symbolName: string,
fileName: string,
isDefault = false
): Change {
const rootNode = source;
const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);

// get nodes that map to import statements from the file fileName
const relevantImports = allImports.filter(node => {
// StringLiteral of the ImportDeclaration is the import file (fileName in this case).
const importFiles = node
.getChildren()
.filter(child => child.kind === ts.SyntaxKind.StringLiteral)
.map(n => (n as ts.StringLiteral).text);

return importFiles.filter(file => file === fileName).length === 1;
});

if (relevantImports.length > 0) {
let importsAsterisk = false;
// imports from import file
const imports: ts.Node[] = [];
relevantImports.forEach(n => {
Array.prototype.push.apply(
imports,
findNodes(n, ts.SyntaxKind.Identifier)
);
if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
importsAsterisk = true;
}
});

// if imports * from fileName, don't add symbolName
if (importsAsterisk) {
return new NoopChange();
}

const importTextNodes = imports.filter(
n => (n as ts.Identifier).text === symbolName
);

// insert import if it's not there
if (importTextNodes.length === 0) {
const fallbackPos =
findNodes(
relevantImports[0],
ts.SyntaxKind.CloseBraceToken
)[0].getStart() ||
findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();

return insertAfterLastOccurrence(
imports,
`, ${symbolName}`,
fileToEdit,
fallbackPos
);
}

return new NoopChange();
}

// no such import declaration exists
const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(
n => n.getText() === 'use strict'
);
let fallbackPos = 0;
if (useStrict.length > 0) {
fallbackPos = useStrict[0].end;
}
const open = isDefault ? '' : '{ ';
const close = isDefault ? '' : ' }';
// if there are no imports or 'use strict' statement, insert import at beginning of file
const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
const separator = insertAtBeginning ? '' : ';\n';
const toInsert =
`${separator}import ${open}${symbolName}${close}` +
` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;

return insertAfterLastOccurrence(
allImports,
toInsert,
fileToEdit,
fallbackPos,
ts.SyntaxKind.StringLiteral
);
}
3 changes: 1 addition & 2 deletions modules/entity/schematics-core/utility/ngrx-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { InsertChange, Change, NoopChange } from './change';
import { Tree, SchematicsException, Rule } from '@angular-devkit/schematics';
import { normalize } from '@angular-devkit/core';
import { buildRelativePath } from './find-module';
import { insertImport } from './route-utils';
import { addImportToModule } from './ast-utils';
import { addImportToModule, insertImport } from './ast-utils';

export function addReducerToState(options: any): Rule {
return (host: Tree) => {
Expand Down
Loading

0 comments on commit 89c8b56

Please sign in to comment.