1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TslintConfigurationProvider = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const inversify_1 = require("inversify");
|
6 | const wotan_1 = require("@fimbul/wotan");
|
7 | const TSLint = require("tslint");
|
8 | const path = require("path");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const OFFSET_TO_NODE_MODULES = 3;
|
17 | let TslintConfigurationProvider = class TslintConfigurationProvider {
|
18 | constructor(resolver, fs, cacheFactory, builtinResolver, directories, options) {
|
19 | this.resolver = resolver;
|
20 | this.fs = fs;
|
21 | this.cacheFactory = cacheFactory;
|
22 | this.builtinResolver = builtinResolver;
|
23 | this.directories = directories;
|
24 | this.options = options;
|
25 | this.tslintConfigDir = undefined;
|
26 | this.baseConfig = undefined;
|
27 | this.cache = cacheFactory.create();
|
28 | }
|
29 | find(fileName) {
|
30 | fileName = path.dirname(fileName);
|
31 | let result = this.cache.get(fileName);
|
32 | if (result === undefined && !this.cache.has(fileName)) {
|
33 | result = TSLint.Configuration.findConfigurationPath(null, fileName);
|
34 | const { root } = path.parse(fileName);
|
35 |
|
36 | const configDirname = result === undefined || root !== path.parse(result).root ? root : path.dirname(result);
|
37 | this.cache.set(fileName, result);
|
38 | while (fileName !== configDirname) {
|
39 | this.cache.set(fileName, result);
|
40 | fileName = path.dirname(fileName);
|
41 | }
|
42 | }
|
43 | return result;
|
44 | }
|
45 | resolve(name, basedir) {
|
46 | var _a;
|
47 | const extensions = [...this.resolver.getDefaultExtensions(), '.json'];
|
48 | if (name.startsWith('tslint:')) {
|
49 | try {
|
50 | (_a = this.tslintConfigDir) !== null && _a !== void 0 ? _a : (this.tslintConfigDir = path.join(this.resolver.resolve('tslint', path.dirname(__dirname), extensions), '../configs'));
|
51 | return this.resolver.resolve(path.join(this.tslintConfigDir, name.substr('tslint:'.length)), '', extensions);
|
52 | }
|
53 | catch {
|
54 | throw new Error(`'${name}' is not a valid builtin configuration, try 'tslint:recommended.'`);
|
55 | }
|
56 | }
|
57 | return this.resolver.resolve(name, basedir, extensions, module.paths.slice(OFFSET_TO_NODE_MODULES));
|
58 | }
|
59 | load(filename) {
|
60 | return this.parse(TSLint.Configuration.loadConfigurationFromPath(filename), filename);
|
61 | }
|
62 | parse(raw, filename) {
|
63 | const rulesDirectories = raw.rulesDirectory.length === 0 ? undefined : raw.rulesDirectory;
|
64 | const overrides = [];
|
65 | if (raw.rules.size !== 0)
|
66 | overrides.push({
|
67 | files: ['*', '!*.js?(x)'],
|
68 | rules: new Map(Array.from(raw.rules, mapRules)),
|
69 | });
|
70 | if (raw.jsRules.size !== 0)
|
71 | overrides.push({
|
72 | files: ['*.js?(x)'],
|
73 | rules: new Map(Array.from(raw.jsRules, mapRules)),
|
74 | });
|
75 | return {
|
76 | overrides,
|
77 | filename,
|
78 | extends: this.getBaseConfiguration(),
|
79 | exclude: raw.linterOptions && raw.linterOptions.exclude && mapExcludes(raw.linterOptions.exclude, path.dirname(filename)),
|
80 | };
|
81 | function mapRules([rule, config]) {
|
82 | return [
|
83 | rule,
|
84 | {
|
85 | rule,
|
86 | rulesDirectories,
|
87 | severity: config.ruleSeverity,
|
88 | options: config.ruleArguments,
|
89 | },
|
90 | ];
|
91 | }
|
92 | }
|
93 | getBaseConfiguration() {
|
94 | if (this.baseConfig !== undefined)
|
95 | return this.baseConfig;
|
96 | if (!this.options.valtyr)
|
97 | return this.baseConfig = [];
|
98 | try {
|
99 | const fullPath = path.join(this.directories.getCurrentDirectory(), '.fimbullinter.yaml');
|
100 | const configProvider = new wotan_1.DefaultConfigurationProvider(this.fs, this.resolver, this.builtinResolver, this.cacheFactory);
|
101 | const config = configProvider.parse(this.options.valtyr, fullPath, {
|
102 | stack: [],
|
103 | load() {
|
104 | throw new Error('Global configuration is not allowed to extend other configs.');
|
105 | },
|
106 | });
|
107 | validateGlobalConfig(config);
|
108 | return this.baseConfig = [config];
|
109 | }
|
110 | catch (e) {
|
111 | throw new wotan_1.ConfigurationError(`Error parsing global configuration for 'valtyr': ${e.message}`);
|
112 | }
|
113 | }
|
114 | };
|
115 | TslintConfigurationProvider = tslib_1.__decorate([
|
116 | inversify_1.injectable(),
|
117 | tslib_1.__metadata("design:paramtypes", [wotan_1.Resolver,
|
118 | wotan_1.CachedFileSystem,
|
119 | wotan_1.CacheFactory,
|
120 | wotan_1.BuiltinResolver,
|
121 | wotan_1.DirectoryService,
|
122 | wotan_1.GlobalOptions])
|
123 | ], TslintConfigurationProvider);
|
124 | exports.TslintConfigurationProvider = TslintConfigurationProvider;
|
125 | function mapExcludes(excludes, configDir) {
|
126 | const result = [];
|
127 | for (const e of excludes)
|
128 | result.push(path.relative(configDir, e));
|
129 | return result;
|
130 | }
|
131 | function validateGlobalConfig(config) {
|
132 | checkNonExistence(config, 'exclude');
|
133 | checkNonExistence(config, 'rules');
|
134 | checkNonExistence(config, 'rulesDirectories');
|
135 | checkNonExistence(config, 'aliases');
|
136 | if (config.overrides !== undefined)
|
137 | for (const override of config.overrides)
|
138 | checkNonExistence(override, 'rules');
|
139 | }
|
140 | function checkNonExistence(config, key) {
|
141 | if (config[key] !== undefined)
|
142 | throw new Error(`'${key}' is not allowed in global configuration.`);
|
143 | }
|
144 |
|
\ | No newline at end of file |