Skip to content

Commit b27948e

Browse files
committed
refactor(types): rename sourceFile to file in RuleContext and deprecate sourceFile
1 parent b5cf16c commit b27948e

11 files changed

Lines changed: 104 additions & 89 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,26 @@ Here's the code for `[project root]/rules/noConsoleRule.ts`:
7070
import { defineRule } from '@tsslint/config';
7171

7272
export function create() {
73-
return defineRule(({ typescript: ts, sourceFile, report }) => {
74-
ts.forEachChild(sourceFile, function cb(node) {
73+
return defineRule(({ typescript: ts, file, report }) => {
74+
ts.forEachChild(file, function cb(node) {
7575
if (
7676
ts.isPropertyAccessExpression(node) &&
7777
ts.isIdentifier(node.expression) &&
7878
node.expression.text === 'console'
7979
) {
8080
report(
8181
`Calls to 'console.x' are not allowed.`,
82-
node.parent.getStart(sourceFile),
82+
node.parent.getStart(file),
8383
node.parent.getEnd()
8484
).withFix(
8585
'Remove this console expression',
8686
() => [{
87-
fileName: sourceFile.fileName,
87+
fileName: file.fileName,
8888
textChanges: [{
8989
newText: '/* deleted */',
9090
span: {
91-
start: node.parent.getStart(sourceFile),
92-
length: node.parent.getWidth(sourceFile),
91+
start: node.parent.getStart(file),
92+
length: node.parent.getWidth(file),
9393
},
9494
}],
9595
}]

fixtures/define-a-plugin/tsslint.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export default defineConfig({
1515

1616
function createIngorePlugin(pattern: RegExp) {
1717
return definePlugin(() => ({
18-
resolveDiagnostics(sourceFile, results) {
19-
const comments = [...sourceFile.text.matchAll(pattern)];
20-
const lines = new Set(comments.map(comment => sourceFile.getLineAndCharacterOfPosition(comment.index).line));
21-
return results.filter(error => error.source !== 'tsslint' || !lines.has(sourceFile.getLineAndCharacterOfPosition(error.start).line - 1));
18+
resolveDiagnostics(file, results) {
19+
const comments = [...file.text.matchAll(pattern)];
20+
const lines = new Set(comments.map(comment => file.getLineAndCharacterOfPosition(comment.index).line));
21+
return results.filter(error => error.source !== 'tsslint' || !lines.has(file.getLineAndCharacterOfPosition(error.start).line - 1));
2222
},
2323
}));
2424
}

fixtures/noConsoleRule.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { defineRule } from '@tsslint/config';
22

33
export function create() {
4-
return defineRule(({ typescript: ts, sourceFile, report }) => {
5-
ts.forEachChild(sourceFile, function cb(node) {
4+
return defineRule(({ typescript: ts, file, report }) => {
5+
ts.forEachChild(file, function cb(node) {
66
if (
77
ts.isPropertyAccessExpression(node) &&
88
ts.isIdentifier(node.expression) &&
99
node.expression.text === 'console'
1010
) {
1111
report(
1212
`Calls to 'console.x' are not allowed.`,
13-
node.parent.getStart(sourceFile),
13+
node.parent.getStart(file),
1414
node.parent.getEnd()
1515
).withFix(
1616
`Remove 'console.${node.name.text}'`,
1717
() => [{
18-
fileName: sourceFile.fileName,
18+
fileName: file.fileName,
1919
textChanges: [{
2020
newText: '/* deleted */',
2121
span: {
22-
start: node.parent.getStart(sourceFile),
23-
length: node.parent.getWidth(sourceFile),
22+
start: node.parent.getStart(file),
23+
length: node.parent.getWidth(file),
2424
},
2525
}],
2626
}]

packages/config/lib/plugins/diagnostics.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ type CheckMode = 'syntactic' | 'semantic' | 'declaration';
55
export function create(mode: CheckMode | CheckMode[] = 'semantic'): Plugin {
66
const modes = Array.isArray(mode) ? mode : [mode];
77
return ({ languageService }) => ({
8-
resolveDiagnostics(sourceFile, diagnostics) {
8+
resolveDiagnostics(file, diagnostics) {
99
const program = languageService.getProgram()!;
1010
for (const mode of modes) {
1111
const diags = mode === 'syntactic'
12-
? program.getSyntacticDiagnostics(sourceFile)
12+
? program.getSyntacticDiagnostics(file)
1313
: mode === 'semantic'
14-
? program.getSemanticDiagnostics(sourceFile)
14+
? program.getSemanticDiagnostics(file)
1515
: mode === 'declaration'
16-
? program.getDeclarationDiagnostics(sourceFile)
16+
? program.getDeclarationDiagnostics(file)
1717
: [];
1818
for (const diag of diags) {
1919
diag.start ??= 0;

packages/config/lib/plugins/ignore.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function create(
122122
};
123123

124124
return {
125-
resolveDiagnostics(sourceFile, results) {
125+
resolveDiagnostics(file, results) {
126126
if (
127127
!reportsUnusedComments &&
128128
!results.some(error => error.source === 'tsslint')
@@ -132,7 +132,7 @@ export function create(
132132
const comments = new Map<string | undefined, CommentState[]>();
133133
const logs: string[] = [];
134134

135-
forEachComment(sourceFile, (fullText, { pos, end }) => {
135+
forEachComment(file, (fullText, { pos, end }) => {
136136
pos += 2; // Trim the // or /* characters
137137
const commentText = fullText.substring(pos, end);
138138
logs.push(commentText);
@@ -146,13 +146,13 @@ export function create(
146146
comments.set(ruleId, []);
147147
}
148148
const disabledLines = comments.get(ruleId)!;
149-
const line = sourceFile.getLineAndCharacterOfPosition(index).line;
149+
const line = file.getLineAndCharacterOfPosition(index).line;
150150

151151
let startLine = line;
152152

153153
if (mode === 'singleLine') {
154-
const startWithComment = sourceFile.text.slice(
155-
sourceFile.getPositionOfLineAndCharacter(line, 0),
154+
const startWithComment = file.text.slice(
155+
file.getPositionOfLineAndCharacter(line, 0),
156156
index - 2
157157
).trim() === '';
158158
if (startWithComment) {
@@ -173,7 +173,7 @@ export function create(
173173

174174
if (endComment?.index !== undefined) {
175175
const index = endComment.index + pos;
176-
const prevLine = sourceFile.getLineAndCharacterOfPosition(index).line;
176+
const prevLine = file.getLineAndCharacterOfPosition(index).line;
177177
const ruleId = endComment.groups?.ruleId;
178178

179179
const disabledLines = comments.get(ruleId);
@@ -184,18 +184,18 @@ export function create(
184184
}
185185
});
186186

187-
let reportedRules = reportedRulesOfFile.get(sourceFile.fileName);
187+
let reportedRules = reportedRulesOfFile.get(file.fileName);
188188
if (!reportedRules) {
189189
reportedRules = [];
190-
reportedRulesOfFile.set(sourceFile.fileName, reportedRules);
190+
reportedRulesOfFile.set(file.fileName, reportedRules);
191191
}
192192
reportedRules.length = 0;
193193

194194
results = results.filter(error => {
195195
if (error.source !== 'tsslint') {
196196
return true;
197197
}
198-
const line = sourceFile.getLineAndCharacterOfPosition(error.start).line;
198+
const line = file.getLineAndCharacterOfPosition(error.start).line;
199199

200200
reportedRules.push([error.code as any, line]);
201201

@@ -227,7 +227,7 @@ export function create(
227227
for (const state of comment.values()) {
228228
if (!state.used) {
229229
results.push({
230-
file: sourceFile,
230+
file: file,
231231
start: state.commentRange[0],
232232
length: state.commentRange[1] - state.commentRange[0],
233233
code: 'tsslint:unused-ignore-comment' as any,
@@ -241,23 +241,23 @@ export function create(
241241
}
242242
return results;
243243
},
244-
resolveCodeFixes(sourceFile, diagnostic, codeFixes) {
244+
resolveCodeFixes(file, diagnostic, codeFixes) {
245245
if (diagnostic.source !== 'tsslint' || diagnostic.start === undefined) {
246246
return codeFixes;
247247
}
248-
const line = sourceFile.getLineAndCharacterOfPosition(diagnostic.start).line;
248+
const line = file.getLineAndCharacterOfPosition(diagnostic.start).line;
249249
codeFixes.push({
250250
fixName: cmd,
251251
description: `Ignore with ${cmdText}`,
252252
changes: [
253253
{
254-
fileName: sourceFile.fileName,
254+
fileName: file.fileName,
255255
textChanges: [{
256256
newText: reg.test(`${cmdText}${diagnostic.code}`)
257257
? `// ${cmdText}${diagnostic.code}\n`
258258
: `// ${cmdText} ${diagnostic.code}\n`,
259259
span: {
260-
start: sourceFile.getPositionOfLineAndCharacter(line, 0),
260+
start: file.getPositionOfLineAndCharacter(line, 0),
261261
length: 0,
262262
},
263263
}],

packages/core/index.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function createLinter(
4040
const lintResults = new Map<
4141
/* fileName */ string,
4242
[
43-
sourceFile: ts.SourceFile,
43+
file: ts.SourceFile,
4444
diagnostic2Fixes: Map<ts.DiagnosticWithLocation, {
4545
title: string;
4646
getEdits: () => ts.FileTextChanges[];
@@ -76,7 +76,12 @@ export function createLinter(
7676
const rulesContext: RuleContext = typeAwareMode
7777
? {
7878
...ctx,
79-
sourceFile: ctx.languageService.getProgram()!.getSourceFile(fileName)!,
79+
get file() {
80+
return ctx.languageService.getProgram()!.getSourceFile(fileName)!;
81+
},
82+
get sourceFile() {
83+
return ctx.languageService.getProgram()!.getSourceFile(fileName)!;
84+
},
8085
get program() {
8186
return ctx.languageService.getProgram()!;
8287
},
@@ -91,7 +96,12 @@ export function createLinter(
9196
get program(): ts.Program {
9297
throw new Error('Not supported');
9398
},
94-
sourceFile: getNonBoundSourceFile(fileName),
99+
get file() {
100+
return getNonBoundSourceFile(fileName);
101+
},
102+
get sourceFile() {
103+
return getNonBoundSourceFile(fileName);
104+
},
95105
report,
96106
reportError: report,
97107
reportWarning: report,
@@ -100,7 +110,7 @@ export function createLinter(
100110
const token = ctx.languageServiceHost.getCancellationToken?.();
101111
const configs = getConfigsForFile(fileName, cache?.[2]);
102112

103-
lintResults.set(fileName, [rulesContext.sourceFile, new Map(), []]);
113+
lintResults.set(fileName, [rulesContext.file, new Map(), []]);
104114

105115
const lintResult = lintResults.get(fileName)!;
106116

@@ -115,12 +125,12 @@ export function createLinter(
115125
if (ruleCache) {
116126
let lintResult = lintResults.get(fileName);
117127
if (!lintResult) {
118-
lintResults.set(fileName, lintResult = [rulesContext.sourceFile, new Map(), []]);
128+
lintResults.set(fileName, lintResult = [rulesContext.file, new Map(), []]);
119129
}
120130
for (const cacheDiagnostic of ruleCache[1]) {
121131
lintResult[1].set({
122132
...cacheDiagnostic,
123-
file: rulesContext.sourceFile,
133+
file: rulesContext.file,
124134
relatedInformation: cacheDiagnostic.relatedInformation?.map(info => ({
125135
...info,
126136
file: info.file ? (syntaxOnlyLanguageService as any).getNonBoundSourceFile(info.file.fileName) : undefined,
@@ -174,7 +184,7 @@ export function createLinter(
174184
for (const { plugins } of configs) {
175185
for (const { resolveDiagnostics } of plugins) {
176186
if (resolveDiagnostics) {
177-
diagnostics = resolveDiagnostics(rulesContext.sourceFile, diagnostics);
187+
diagnostics = resolveDiagnostics(rulesContext.file, diagnostics);
178188
}
179189
}
180190
}
@@ -203,7 +213,7 @@ export function createLinter(
203213
category: ts.DiagnosticCategory.Message,
204214
code: currentRuleId as any,
205215
messageText: message,
206-
file: rulesContext.sourceFile,
216+
file: rulesContext.file,
207217
start,
208218
length: end - start,
209219
source: 'tsslint',
@@ -226,7 +236,7 @@ export function createLinter(
226236

227237
let lintResult = lintResults.get(fileName);
228238
if (!lintResult) {
229-
lintResults.set(fileName, lintResult = [rulesContext.sourceFile, new Map(), []]);
239+
lintResults.set(fileName, lintResult = [rulesContext.file, new Map(), []]);
230240
}
231241
const diagnostic2Fixes = lintResult[1];
232242
const refactors = lintResult[2];
@@ -281,7 +291,7 @@ export function createLinter(
281291
return [];
282292
}
283293

284-
const sourceFile = lintResult[0];
294+
const file = lintResult[0];
285295
const configs = getConfigsForFile(fileName, minimatchCache);
286296
const result: ts.CodeFixAction[] = [];
287297

@@ -310,7 +320,7 @@ export function createLinter(
310320
for (const { plugins } of configs) {
311321
for (const { resolveCodeFixes } of plugins) {
312322
if (resolveCodeFixes) {
313-
codeFixes = resolveCodeFixes(sourceFile, diagnostic, codeFixes);
323+
codeFixes = resolveCodeFixes(file, diagnostic, codeFixes);
314324
}
315325
}
316326
}

0 commit comments

Comments
 (0)