1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.wrapRuleForTslint = exports.wrapTslintFormatter = exports.wrapTslintRule = void 0;
|
4 | const ymir_1 = require("@fimbul/ymir");
|
5 | const TSLint = require("tslint");
|
6 | const ts = require("typescript");
|
7 | const getCaller = require("get-caller-file");
|
8 | const path = require("path");
|
9 | const tsutils_1 = require("tsutils");
|
10 | const tslib_1 = require("tslib");
|
11 |
|
12 | function wrapTslintRule(Rule, name = inferName(Rule)) {
|
13 | class R extends ymir_1.AbstractRule {
|
14 | constructor(context) {
|
15 | super(context);
|
16 | this.delegate = new Rule({
|
17 | ruleArguments: TSLint.Utils.arrayify(context.options),
|
18 | ruleSeverity: 'error',
|
19 | ruleName: name,
|
20 | disabledIntervals: [],
|
21 | });
|
22 | }
|
23 | apply() {
|
24 | if (!this.delegate.isEnabled())
|
25 | return;
|
26 | let result;
|
27 | if (TSLint.isTypedRule(this.delegate) && this.program !== undefined) {
|
28 | result = this.delegate.applyWithProgram(this.sourceFile, this.program);
|
29 | }
|
30 | else {
|
31 | result = this.delegate.apply(this.sourceFile);
|
32 | }
|
33 | const { fileName } = this.sourceFile;
|
34 | for (const failure of result) {
|
35 | if (failure.getFileName() !== fileName)
|
36 | throw new Error(`Adding findings for a different SourceFile is not supported. Expected '${fileName}' but received '${failure.getFileName()}' from rule '${this.delegate.getOptions().ruleName}'.`);
|
37 | this.addFinding(failure.getStartPosition().getPosition(), failure.getEndPosition().getPosition(), failure.getFailure(), arrayify(failure.getFix()).map((r) => ({ start: r.start, end: r.end, text: r.text })));
|
38 | }
|
39 | }
|
40 | }
|
41 | R.requiresTypeInformation = !!(Rule.metadata && Rule.metadata.requiresTypeInfo) ||
|
42 | Rule.prototype instanceof TSLint.Rules.TypedRule;
|
43 | R.deprecated = Rule.metadata && typeof Rule.metadata.deprecationMessage === 'string'
|
44 | ? Rule.metadata.deprecationMessage || true
|
45 | : false;
|
46 | return Rule.metadata && Rule.metadata.typescriptOnly ? tslib_1.__decorate([ymir_1.typescriptOnly], R) : R;
|
47 | }
|
48 | exports.wrapTslintRule = wrapTslintRule;
|
49 | function inferName(Rule) {
|
50 | if (Rule.metadata !== undefined && Rule.metadata.ruleName)
|
51 | return Rule.metadata.ruleName;
|
52 | const caller = getCaller(3);
|
53 | return path.basename(caller, path.extname(caller));
|
54 | }
|
55 | function wrapTslintFormatter(Formatter) {
|
56 | return class extends ymir_1.AbstractFormatter {
|
57 | constructor() {
|
58 | super();
|
59 | this.fileNames = [];
|
60 | this.failures = [];
|
61 | this.fixed = [];
|
62 | this.delegate = new Formatter();
|
63 | }
|
64 | format(fileName, summary) {
|
65 | this.fileNames.push(fileName);
|
66 | let sourceFile;
|
67 | for (let i = 0; i < summary.fixes; ++i)
|
68 | this.fixed.push(new TSLint.RuleFailure(getSourceFile(), 0, 0, '', '', TSLint.Replacement.appendText(0, '')));
|
69 | if (summary.findings.length === 0)
|
70 | return;
|
71 | this.failures.push(...summary.findings.map((f) => {
|
72 | const failure = new TSLint.RuleFailure(getSourceFile(), f.start.position, f.end.position, f.message, f.ruleName, f.fix && f.fix.replacements.map(convertToTslintReplacement));
|
73 | failure.setRuleSeverity(f.severity === 'suggestion' ? 'warning' : f.severity);
|
74 | return failure;
|
75 | }));
|
76 | return;
|
77 | function getSourceFile() {
|
78 | return sourceFile !== null && sourceFile !== void 0 ? sourceFile : (sourceFile = ts.createSourceFile(fileName, summary.content, ts.ScriptTarget.Latest));
|
79 | }
|
80 | }
|
81 | flush() {
|
82 | return this.delegate.format(this.failures, this.fixed, this.fileNames).trim();
|
83 | }
|
84 | };
|
85 | }
|
86 | exports.wrapTslintFormatter = wrapTslintFormatter;
|
87 |
|
88 | function wrapRuleForTslint(Rule) {
|
89 | var _a, _b;
|
90 | const metadata = {
|
91 | ruleName: 'who-cares',
|
92 | typescriptOnly: false,
|
93 | description: '',
|
94 | options: undefined,
|
95 | optionsDescription: '',
|
96 | type: 'functionality',
|
97 | deprecationMessage: !Rule.deprecated ? undefined : Rule.deprecated === true ? '' : Rule.deprecated,
|
98 | };
|
99 | function apply(options, sourceFile, program) {
|
100 | const args = options.ruleArguments.length < 2 ? options.ruleArguments[0] : options.ruleArguments;
|
101 | const failures = [];
|
102 | const context = {
|
103 | sourceFile,
|
104 | program,
|
105 | compilerOptions: program && program.getCompilerOptions(),
|
106 | options: args,
|
107 | settings: new Map(),
|
108 | getFlatAst() {
|
109 | return tsutils_1.convertAst(sourceFile).flat;
|
110 | },
|
111 | getWrappedAst() {
|
112 | return tsutils_1.convertAst(sourceFile).wrapped;
|
113 | },
|
114 | addFinding(start, end, message, fix) {
|
115 | failures.push(new TSLint.RuleFailure(sourceFile, start, end, message, options.ruleName, fix && arrayify(fix).map(convertToTslintReplacement)));
|
116 | },
|
117 | };
|
118 | if (Rule.supports === undefined || Rule.supports(sourceFile, context) === true)
|
119 | new Rule(context).apply();
|
120 | return failures;
|
121 | }
|
122 | if (Rule.requiresTypeInformation)
|
123 | return _a = class extends TSLint.Rules.TypedRule {
|
124 | applyWithProgram(sourceFile, program) {
|
125 | return apply(this.getOptions(), sourceFile, program);
|
126 | }
|
127 | },
|
128 | _a.metadata = metadata,
|
129 | _a;
|
130 | return _b = class extends TSLint.Rules.OptionallyTypedRule {
|
131 | apply(sourceFile) {
|
132 | return apply(this.getOptions(), sourceFile);
|
133 | }
|
134 | applyWithProgram(sourceFile, program) {
|
135 | return apply(this.getOptions(), sourceFile, program);
|
136 | }
|
137 | },
|
138 | _b.metadata = metadata,
|
139 | _b;
|
140 | }
|
141 | exports.wrapRuleForTslint = wrapRuleForTslint;
|
142 | function convertToTslintReplacement(r) {
|
143 | return new TSLint.Replacement(r.start, r.end - r.start, r.text);
|
144 | }
|
145 | function arrayify(maybeArr) {
|
146 | return Array.isArray(maybeArr)
|
147 | ? maybeArr
|
148 | : maybeArr === undefined
|
149 | ? []
|
150 | : [maybeArr];
|
151 | }
|
152 |
|
\ | No newline at end of file |