diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/noImplicitDependenciesRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noImplicitDependenciesRule.js | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/node_modules/tslint/lib/rules/noImplicitDependenciesRule.js b/node_modules/tslint/lib/rules/noImplicitDependenciesRule.js index 212baf8ae..6aa697a33 100644 --- a/node_modules/tslint/lib/rules/noImplicitDependenciesRule.js +++ b/node_modules/tslint/lib/rules/noImplicitDependenciesRule.js @@ -35,27 +35,42 @@ var Rule = /** @class */ (function (_super) { return "Module '" + module + "' is not listed as dependency in package.json"; }; Rule.prototype.apply = function (sourceFile) { + var whitelist = this.ruleArguments.find(function (arg) { return Array.isArray(arg); }); + if (whitelist === null || whitelist === undefined) { + whitelist = []; + } return this.applyWithFunction(sourceFile, walk, { dev: this.ruleArguments.indexOf(OPTION_DEV) !== -1, optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1, + whitelist: whitelist, }); }; /* tslint:disable:object-literal-sort-keys */ Rule.metadata = { ruleName: "no-implicit-dependencies", description: "Disallows importing modules that are not listed as dependency in the project's package.json", - descriptionDetails: (_a = ["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "], _a.raw = ["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "], Lint.Utils.dedent(_a)), - optionsDescription: (_b = ["\n By default the rule looks at `\"dependencies\"` and `\"peerDependencies\"`.\n By adding the `\"", "\"` option the rule looks at `\"devDependencies\"` instead of `\"peerDependencies\"`.\n By adding the `\"", "\"` option the rule also looks at `\"optionalDependencies\"`.\n "], _b.raw = ["\n By default the rule looks at \\`\"dependencies\"\\` and \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule looks at \\`\"devDependencies\"\\` instead of \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"optionalDependencies\"\\`.\n "], Lint.Utils.dedent(_b, OPTION_DEV, OPTION_OPTIONAL)), + descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "], ["\n Disallows importing transient dependencies and modules installed above your package's root directory.\n "]))), + optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n By default the rule looks at `\"dependencies\"` and `\"peerDependencies\"`.\n By adding the `\"", "\"` option the rule also looks at `\"devDependencies\"`.\n By adding the `\"", "\"` option the rule also looks at `\"optionalDependencies\"`.\n An array of whitelisted modules can be added to skip checking their existence in package.json.\n "], ["\n By default the rule looks at \\`\"dependencies\"\\` and \\`\"peerDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"devDependencies\"\\`.\n By adding the \\`\"", "\"\\` option the rule also looks at \\`\"optionalDependencies\"\\`.\n An array of whitelisted modules can be added to skip checking their existence in package.json.\n "])), OPTION_DEV, OPTION_OPTIONAL), options: { type: "array", - items: { - type: "string", - enum: [OPTION_DEV, OPTION_OPTIONAL], - }, + items: [ + { + type: "string", + enum: [OPTION_DEV, OPTION_OPTIONAL], + }, + { + type: "array", + }, + ], minItems: 0, - maxItems: 2, + maxItems: 3, }, - optionExamples: [true, [true, OPTION_DEV], [true, OPTION_OPTIONAL]], + optionExamples: [ + true, + [true, OPTION_DEV], + [true, OPTION_OPTIONAL], + [true, ["src", "app"]], + ], type: "functionality", typescriptOnly: false, }; @@ -65,11 +80,12 @@ exports.Rule = Rule; function walk(ctx) { var options = ctx.options; var dependencies; - for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 31 /* All */); _i < _a.length; _i++) { + var whitelist = new Set(options.whitelist); + for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 63 /* All */); _i < _a.length; _i++) { var name = _a[_i]; if (!ts.isExternalModuleNameRelative(name.text)) { var packageName = getPackageName(name.text); - if (builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) { + if (!whitelist.has(packageName) && builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) { ctx.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(packageName)); } } @@ -92,20 +108,25 @@ function getDependencies(fileName, options) { var result = new Set(); var packageJsonPath = findPackageJson(path.resolve(path.dirname(fileName))); if (packageJsonPath !== undefined) { - // don't use require here to avoid caching - // remove BOM from file content before parsing - var content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, "")); - if (content.dependencies !== undefined) { - addDependencies(result, content.dependencies); - } - if (!options.dev && content.peerDependencies !== undefined) { - addDependencies(result, content.peerDependencies); - } - if (options.dev && content.devDependencies !== undefined) { - addDependencies(result, content.devDependencies); + try { + // don't use require here to avoid caching + // remove BOM from file content before parsing + var content = JSON.parse(fs.readFileSync(packageJsonPath, "utf8").replace(/^\uFEFF/, "")); + if (content.dependencies !== undefined) { + addDependencies(result, content.dependencies); + } + if (content.peerDependencies !== undefined) { + addDependencies(result, content.peerDependencies); + } + if (options.dev && content.devDependencies !== undefined) { + addDependencies(result, content.devDependencies); + } + if (options.optional && content.optionalDependencies !== undefined) { + addDependencies(result, content.optionalDependencies); + } } - if (options.optional && content.optionalDependencies !== undefined) { - addDependencies(result, content.optionalDependencies); + catch (_a) { + // treat malformed package.json files as empty } } return result; @@ -129,4 +150,4 @@ function findPackageJson(current) { } while (prev !== current); return undefined; } -var _a, _b; +var templateObject_1, templateObject_2; |