UNPKG

5.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Lint = require("tslint");
4const ConfigFactory_1 = require("./config/ConfigFactory");
5const GeneralRuleUtils_1 = require("./utils/GeneralRuleUtils");
6const ImportRuleUtils_1 = require("./utils/ImportRuleUtils");
7exports.IMPORTS_BETWEEN_PACKAGES_RULE_ID = "tsf-folders-imports-between-packages";
8const DISALLOW_IMPORT_FROM_SELF_MESSAGE = "do not import a package from itself - use a relative path";
9class Rule extends Lint.Rules.AbstractRule {
10 apply(sourceFile) {
11 const walker = new ImportsWalker(sourceFile, this.getOptions());
12 this.applyWithWalker(walker);
13 return walker.getFailures();
14 }
15}
16exports.Rule = Rule;
17class ImportsWalker extends Lint.RuleWalker {
18 visitImportDeclaration(node) {
19 this.validate(node, node.moduleSpecifier.getText());
20 }
21 visitImportEqualsDeclaration(node) {
22 this.validate(node, node.moduleReference.getText());
23 super.visitImportEqualsDeclaration(node);
24 }
25 validate(node, text) {
26 // algorithm:
27 /*
28 - determine this files PackageFolder, PackageSubFolder
29 - if ThirdParty then skip
30 - determine the PackageFolder, PackageSubFolder of the import
31 - if ThirdParty then skip
32 - disallowImportFromSelf -> disallow if same
33 - check if the import is allowed
34 */
35 const config = ConfigFactory_1.ConfigFactory.createForBetweenPackages(this.getOptions());
36 const thisPackageLocation = ImportRuleUtils_1.ImportRuleUtils.determinePackageLocationFromPath(node.getSourceFile().fileName, exports.IMPORTS_BETWEEN_PACKAGES_RULE_ID, config, ImportRuleUtils_1.PathSource.SourceFilePath);
37 if (ImportRuleUtils_1.ImportRuleUtils.isThisPackageThirdParty(thisPackageLocation, node)) {
38 return;
39 }
40 if (!thisPackageLocation.packageFolder) {
41 throw new Error("unexpected: this package is not ThirdParty, but has no packageFolder in its location");
42 }
43 const importPackageLocation = ImportRuleUtils_1.ImportRuleUtils.determinePackageLocationFromPath(text, exports.IMPORTS_BETWEEN_PACKAGES_RULE_ID, config, ImportRuleUtils_1.PathSource.ImportText, thisPackageLocation);
44 ImportRuleUtils_1.ImportRuleUtils.logPackageAndImport(node, thisPackageLocation, importPackageLocation);
45 const isImportRecognised = !ImportRuleUtils_1.ImportRuleUtils.isPackageThirdParty(importPackageLocation);
46 if (isImportRecognised &&
47 importPackageLocation.packageName === thisPackageLocation.packageName &&
48 config.disallowImportFromSelf.enabled &&
49 !ImportRuleUtils_1.ImportRuleUtils.shouldIgnoreFile(node, config.disallowImportFromSelf.ignorePaths)) {
50 this.addFailureAtNode(node, GeneralRuleUtils_1.GeneralRuleUtils.buildFailureString(DISALLOW_IMPORT_FROM_SELF_MESSAGE, exports.IMPORTS_BETWEEN_PACKAGES_RULE_ID));
51 return;
52 }
53 if (isImportRecognised &&
54 config.checkImportsBetweenPackages.enabled &&
55 !ImportRuleUtils_1.ImportRuleUtils.shouldIgnoreFile(node, config.checkImportsBetweenPackages.ignorePaths)) {
56 if (!thisPackageLocation.packageFolder ||
57 !importPackageLocation.packageFolder) {
58 return;
59 }
60 if (importPackageLocation.packageFolder ===
61 thisPackageLocation.packageFolder) {
62 if (!config.checkImportsBetweenPackages.checkSubFoldersEnabled) {
63 return;
64 }
65 // TODO xxx extract fun?
66 // check sub-folders
67 if (thisPackageLocation.packageSubFolder &&
68 importPackageLocation.packageSubFolder &&
69 thisPackageLocation.packageSubFolder.importPath !==
70 importPackageLocation.packageSubFolder.importPath) {
71 if (thisPackageLocation.packageSubFolder.allowedToImport.some(allowed => allowed === "*")) {
72 return;
73 }
74 if (!thisPackageLocation.packageSubFolder.allowedToImport.some(allowed => {
75 return (importPackageLocation.packageSubFolder.importPath === allowed);
76 })) {
77 const failureMessage = `'${thisPackageLocation.packageName}' sub folder '${thisPackageLocation.packageSubFolder.importPath}' is not allowed to import from '${importPackageLocation.packageSubFolder.importPath}'`;
78 this.addFailureAtNodeWithMessage(node, failureMessage);
79 }
80 }
81 }
82 else {
83 const thisPackageFolder = thisPackageLocation.packageFolder;
84 if (thisPackageFolder.allowedToImport.find(allowed => allowed === "*")) {
85 return;
86 }
87 if (!thisPackageFolder.allowedToImport.find(allowed => allowed === importPackageLocation.packageName)) {
88 const failureMessage = `'${thisPackageLocation.packageName}' is not allowed to import from '${importPackageLocation.packageName}'`;
89 this.addFailureAtNodeWithMessage(node, failureMessage);
90 }
91 }
92 }
93 }
94 addFailureAtNodeWithMessage(node, failureMessage) {
95 this.addFailureAtNode(node, GeneralRuleUtils_1.GeneralRuleUtils.buildFailureString(failureMessage, exports.IMPORTS_BETWEEN_PACKAGES_RULE_ID));
96 }
97}
98//# sourceMappingURL=tsfFoldersImportsBetweenPackagesRule.js.map
\No newline at end of file