@tsslint/core 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +7 -3
- package/index.js +153 -159
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { Config, ProjectContext } from '@tsslint/config';
|
|
2
|
+
import type * as ts from 'typescript/lib/tsserverlibrary.js';
|
|
3
|
+
export type Linter = ReturnType<typeof createLinter>;
|
|
4
|
+
export declare function createLinter(ctx: ProjectContext, config: Config, withStack: boolean): {
|
|
5
|
+
lint(fileName: string): ts.Diagnostic[];
|
|
6
|
+
getCodeFixes(fileName: string, start: number, end: number, diagnostics?: ts.Diagnostic[] | undefined): ts.CodeFixAction[];
|
|
7
|
+
};
|
package/index.js
CHANGED
|
@@ -1,174 +1,168 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const ErrorStackParser =
|
|
28
|
-
function
|
|
3
|
+
exports.createLinter = void 0;
|
|
4
|
+
const ErrorStackParser = require("error-stack-parser");
|
|
5
|
+
function createLinter(ctx, config, withStack) {
|
|
29
6
|
if (withStack) {
|
|
30
7
|
require('source-map-support').install();
|
|
31
8
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
9
|
+
const ts = ctx.typescript;
|
|
10
|
+
const fileFixes = new Map();
|
|
11
|
+
const sourceFiles = new Map();
|
|
12
|
+
const configSourceFile = ts.createSourceFile(ctx.configFile, ts.sys.readFile(ctx.configFile) ?? '', ts.ScriptTarget.Latest, true);
|
|
13
|
+
const plugins = (config.plugins ?? []).map(plugin => plugin(ctx));
|
|
14
|
+
let rules = { ...config.rules };
|
|
15
|
+
for (const plugin of plugins) {
|
|
16
|
+
if (plugin.resolveRules) {
|
|
17
|
+
rules = plugin.resolveRules(rules);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
lint(fileName) {
|
|
22
|
+
const sourceFile = ctx.languageService.getProgram()?.getSourceFile(fileName);
|
|
23
|
+
if (!sourceFile) {
|
|
24
|
+
throw new Error(`No source file found for ${fileName}`);
|
|
25
|
+
}
|
|
26
|
+
const rulesContext = {
|
|
27
|
+
...ctx,
|
|
28
|
+
sourceFile,
|
|
29
|
+
reportError,
|
|
30
|
+
reportWarning,
|
|
31
|
+
reportSuggestion,
|
|
32
|
+
};
|
|
33
|
+
const token = ctx.languageServiceHost.getCancellationToken?.();
|
|
34
|
+
const fixes = getFileFixes(sourceFile.fileName);
|
|
35
|
+
let result = [];
|
|
36
|
+
let currentRuleId;
|
|
37
|
+
fixes.clear();
|
|
38
|
+
for (const [id, rule] of Object.entries(rules)) {
|
|
39
|
+
if (token?.isCancellationRequested()) {
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
currentRuleId = id;
|
|
43
|
+
rule(rulesContext);
|
|
44
|
+
}
|
|
45
|
+
for (const plugin of plugins) {
|
|
46
|
+
if (plugin.resolveDiagnostics) {
|
|
47
|
+
result = plugin.resolveDiagnostics(fileName, result);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
function reportError(message, start, end, trace = true) {
|
|
52
|
+
return report(ts.DiagnosticCategory.Error, message, start, end, trace);
|
|
53
|
+
}
|
|
54
|
+
function reportWarning(message, start, end, trace = true) {
|
|
55
|
+
return report(ts.DiagnosticCategory.Warning, message, start, end, trace);
|
|
56
|
+
}
|
|
57
|
+
function reportSuggestion(message, start, end, trace = true) {
|
|
58
|
+
return report(ts.DiagnosticCategory.Suggestion, message, start, end, trace);
|
|
59
|
+
}
|
|
60
|
+
function report(category, message, start, end, trace) {
|
|
61
|
+
const error = {
|
|
62
|
+
category,
|
|
63
|
+
code: currentRuleId,
|
|
64
|
+
messageText: message,
|
|
65
|
+
file: sourceFile,
|
|
66
|
+
start,
|
|
67
|
+
length: end - start,
|
|
68
|
+
source: 'tsslint',
|
|
69
|
+
relatedInformation: [],
|
|
70
|
+
};
|
|
71
|
+
const stacks = trace ? ErrorStackParser.parse(new Error()) : [];
|
|
72
|
+
if (stacks.length >= 3) {
|
|
73
|
+
const stack = stacks[2];
|
|
74
|
+
if (stack.fileName && stack.lineNumber !== undefined && stack.columnNumber !== undefined) {
|
|
75
|
+
let fileName = stack.fileName.replace(/\\/g, '/');
|
|
76
|
+
if (fileName.startsWith('file://')) {
|
|
77
|
+
fileName = fileName.substring('file://'.length);
|
|
55
78
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
};
|
|
80
|
-
const stacks = trace ? ErrorStackParser.parse(new Error()) : [];
|
|
81
|
-
if (stacks.length >= 3) {
|
|
82
|
-
const stack = stacks[2];
|
|
83
|
-
if (stack.fileName && stack.lineNumber !== undefined && stack.columnNumber !== undefined) {
|
|
84
|
-
let fileName = stack.fileName.replace(/\\/g, '/');
|
|
85
|
-
if (fileName.startsWith('file://')) {
|
|
86
|
-
fileName = fileName.substring('file://'.length);
|
|
87
|
-
}
|
|
88
|
-
if (!sourceFiles.has(fileName)) {
|
|
89
|
-
const text = ctx.languageServiceHost.readFile(fileName) ?? '';
|
|
90
|
-
sourceFiles.set(fileName, ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true));
|
|
91
|
-
}
|
|
92
|
-
const stackFile = sourceFiles.get(fileName);
|
|
93
|
-
const pos = stackFile?.getPositionOfLineAndCharacter(stack.lineNumber - 1, stack.columnNumber - 1);
|
|
94
|
-
if (withStack) {
|
|
95
|
-
error.relatedInformation?.push({
|
|
96
|
-
category: ts.DiagnosticCategory.Message,
|
|
97
|
-
code: 0,
|
|
98
|
-
file: stackFile,
|
|
99
|
-
start: pos,
|
|
100
|
-
length: 0,
|
|
101
|
-
messageText: '',
|
|
102
|
-
});
|
|
103
|
-
error.relatedInformation?.push({
|
|
104
|
-
category: ts.DiagnosticCategory.Message,
|
|
105
|
-
code: 0,
|
|
106
|
-
file: configSourceFile,
|
|
107
|
-
start: 0,
|
|
108
|
-
length: 0,
|
|
109
|
-
messageText: '',
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
79
|
+
if (!sourceFiles.has(fileName)) {
|
|
80
|
+
const text = ctx.languageServiceHost.readFile(fileName) ?? '';
|
|
81
|
+
sourceFiles.set(fileName, ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true));
|
|
82
|
+
}
|
|
83
|
+
const stackFile = sourceFiles.get(fileName);
|
|
84
|
+
const pos = stackFile?.getPositionOfLineAndCharacter(stack.lineNumber - 1, stack.columnNumber - 1);
|
|
85
|
+
if (withStack) {
|
|
86
|
+
error.relatedInformation?.push({
|
|
87
|
+
category: ts.DiagnosticCategory.Message,
|
|
88
|
+
code: 0,
|
|
89
|
+
file: stackFile,
|
|
90
|
+
start: pos,
|
|
91
|
+
length: 0,
|
|
92
|
+
messageText: '',
|
|
93
|
+
});
|
|
94
|
+
error.relatedInformation?.push({
|
|
95
|
+
category: ts.DiagnosticCategory.Message,
|
|
96
|
+
code: 0,
|
|
97
|
+
file: configSourceFile,
|
|
98
|
+
start: 0,
|
|
99
|
+
length: 0,
|
|
100
|
+
messageText: '',
|
|
101
|
+
});
|
|
113
102
|
}
|
|
114
|
-
result.push(error);
|
|
115
|
-
return {
|
|
116
|
-
withDeprecated() {
|
|
117
|
-
error.reportsDeprecated = true;
|
|
118
|
-
return this;
|
|
119
|
-
},
|
|
120
|
-
withUnnecessary() {
|
|
121
|
-
error.reportsUnnecessary = true;
|
|
122
|
-
return this;
|
|
123
|
-
},
|
|
124
|
-
withFix(title, getEdits) {
|
|
125
|
-
if (!fixes.has(currentRuleId)) {
|
|
126
|
-
fixes.set(currentRuleId, []);
|
|
127
|
-
}
|
|
128
|
-
fixes.get(currentRuleId).push(({
|
|
129
|
-
diagnostic: error,
|
|
130
|
-
title,
|
|
131
|
-
start,
|
|
132
|
-
end,
|
|
133
|
-
getEdits,
|
|
134
|
-
}));
|
|
135
|
-
return this;
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
103
|
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
result.push({
|
|
154
|
-
fixName: `tsslint: ${fix.title}`,
|
|
155
|
-
description: fix.title,
|
|
156
|
-
changes: fix.getEdits(),
|
|
157
|
-
});
|
|
158
|
-
}
|
|
104
|
+
}
|
|
105
|
+
result.push(error);
|
|
106
|
+
return {
|
|
107
|
+
withDeprecated() {
|
|
108
|
+
error.reportsDeprecated = true;
|
|
109
|
+
return this;
|
|
110
|
+
},
|
|
111
|
+
withUnnecessary() {
|
|
112
|
+
error.reportsUnnecessary = true;
|
|
113
|
+
return this;
|
|
114
|
+
},
|
|
115
|
+
withFix(title, getEdits) {
|
|
116
|
+
if (!fixes.has(currentRuleId)) {
|
|
117
|
+
fixes.set(currentRuleId, []);
|
|
159
118
|
}
|
|
119
|
+
fixes.get(currentRuleId).push(({
|
|
120
|
+
diagnostic: error,
|
|
121
|
+
title,
|
|
122
|
+
start,
|
|
123
|
+
end,
|
|
124
|
+
getEdits,
|
|
125
|
+
}));
|
|
126
|
+
return this;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
getCodeFixes(fileName, start, end, diagnostics) {
|
|
132
|
+
const fixesMap = getFileFixes(fileName);
|
|
133
|
+
let result = [];
|
|
134
|
+
for (const [_ruleId, fixes] of fixesMap) {
|
|
135
|
+
for (let i = 0; i < fixes.length; i++) {
|
|
136
|
+
const fix = fixes[i];
|
|
137
|
+
if (diagnostics && !diagnostics.includes(fix.diagnostic)) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if ((fix.start >= start && fix.start <= end) ||
|
|
141
|
+
(fix.end >= start && fix.end <= end) ||
|
|
142
|
+
(start >= fix.start && start <= fix.end) ||
|
|
143
|
+
(end >= fix.start && end <= fix.end)) {
|
|
144
|
+
result.push({
|
|
145
|
+
fixName: `tsslint: ${fix.title}`,
|
|
146
|
+
description: fix.title,
|
|
147
|
+
changes: fix.getEdits(),
|
|
148
|
+
});
|
|
160
149
|
}
|
|
161
|
-
return result;
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
function getFileFixes(fileName) {
|
|
165
|
-
if (!fileFixes.has(fileName)) {
|
|
166
|
-
fileFixes.set(fileName, new Map());
|
|
167
150
|
}
|
|
168
|
-
return fileFixes.get(fileName);
|
|
169
151
|
}
|
|
152
|
+
for (const plugin of plugins) {
|
|
153
|
+
if (plugin.resolveCodeFixes) {
|
|
154
|
+
result = plugin.resolveCodeFixes(fileName, result);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
170
158
|
},
|
|
171
|
-
|
|
159
|
+
};
|
|
160
|
+
function getFileFixes(fileName) {
|
|
161
|
+
if (!fileFixes.has(fileName)) {
|
|
162
|
+
fileFixes.set(fileName, new Map());
|
|
163
|
+
}
|
|
164
|
+
return fileFixes.get(fileName);
|
|
165
|
+
}
|
|
172
166
|
}
|
|
173
|
-
exports.
|
|
167
|
+
exports.createLinter = createLinter;
|
|
174
168
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsslint/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"**/*.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"source-map-support": "^0.5.19"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@tsslint/config": "0.0.
|
|
19
|
+
"@tsslint/config": "0.0.6"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "a9a381f7c5e84819c99b70f6b429150ce766739c"
|
|
22
22
|
}
|