2019-04-12 14:19:32 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
|
|
|
/** Name of the Injectable decorator. */
|
|
|
|
export const INJECTABLE_DECORATOR_NAME = 'Injectable';
|
|
|
|
|
|
|
|
/**
|
2019-04-27 08:00:21 -04:00
|
|
|
* Adds an import to a named import node, if the import does not exist already.
|
2019-04-12 14:19:32 -04:00
|
|
|
* @param node Node to which to add the import.
|
|
|
|
* @param importName Name of the import that should be added.
|
|
|
|
*/
|
2019-04-27 08:00:21 -04:00
|
|
|
export function addImport(node: ts.NamedImports, importName: string) {
|
|
|
|
const elements = node.elements;
|
|
|
|
const isAlreadyImported = elements.some(element => element.name.text === importName);
|
2019-04-12 14:19:32 -04:00
|
|
|
|
2019-04-27 08:00:21 -04:00
|
|
|
if (!isAlreadyImported) {
|
|
|
|
return ts.updateNamedImports(
|
|
|
|
node, [...elements, ts.createImportSpecifier(undefined, ts.createIdentifier(importName))]);
|
2019-04-12 14:19:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the named imports node from an import declaration. */
|
|
|
|
export function getNamedImports(node: ts.ImportDeclaration): ts.NamedImports|null {
|
|
|
|
const importClause = node.importClause;
|
|
|
|
const namedImports = importClause && importClause.namedBindings;
|
|
|
|
return (namedImports && ts.isNamedImports(namedImports)) ? namedImports : null;
|
|
|
|
}
|