ngx-bootstrap
Version:
Angular Bootstrap
101 lines • 4.14 kB
JavaScript
;
/**
* @license
* Copyright Google LLC 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
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasNgModuleImport = hasNgModuleImport;
const ts = __importStar(require("typescript"));
/**
* Whether the Angular module in the given path imports the specifed module class name.
*/
function hasNgModuleImport(tree, modulePath, className) {
const moduleFileContent = tree.read(modulePath);
if (!moduleFileContent) {
throw new Error(`Could not read Angular module file: ${modulePath}`);
}
const parsedFile = ts.createSourceFile(modulePath, moduleFileContent.toString(), ts.ScriptTarget.Latest, true);
let ngModuleMetadata = null;
const findModuleDecorator = (node) => {
if (ts.isDecorator(node) && ts.isCallExpression(node.expression) &&
isNgModuleCallExpression(node.expression)) {
ngModuleMetadata = node.expression.arguments[0];
return;
}
ts.forEachChild(node, findModuleDecorator);
};
ts.forEachChild(parsedFile, findModuleDecorator);
if (!ngModuleMetadata) {
throw new Error(`Could not find NgModule declaration inside: "${modulePath}"`);
}
for (const property of ngModuleMetadata.properties) {
if (!ts.isPropertyAssignment(property) || property.name.getText() !== 'imports' ||
!ts.isArrayLiteralExpression(property.initializer)) {
continue;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (property.initializer.elements.some((element) => element.getText() === className)) {
return true;
}
}
return false;
}
/**
* Resolves the last identifier that is part of the given expression. This helps resolving
* identifiers of nested property access expressions (e.g. myNamespace.core.NgModule).
*/
function resolveIdentifierOfExpression(expression) {
if (ts.isIdentifier(expression)) {
return expression;
}
else if (ts.isPropertyAccessExpression(expression)) {
return resolveIdentifierOfExpression(expression.expression);
}
return null;
}
/** Whether the specified call expression is referring to a NgModule definition. */
function isNgModuleCallExpression(callExpression) {
if (!callExpression.arguments.length ||
!ts.isObjectLiteralExpression(callExpression.arguments[0])) {
return false;
}
const decoratorIdentifier = resolveIdentifierOfExpression(callExpression.expression);
return decoratorIdentifier ? decoratorIdentifier.text === 'NgModule' : false;
}
//# sourceMappingURL=ng-module-imports.js.map