Skip to content

Commit

Permalink
feat(core): detect more reliably whether a rule requires type informa…
Browse files Browse the repository at this point in the history
…tion (#33)
  • Loading branch information
johnsoncodehk authored Dec 16, 2024
1 parent 5f6e7f7 commit 7ea4a09
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 194 deletions.
29 changes: 21 additions & 8 deletions packages/cli/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let fileNames: string[] = [];
let language: Language<string> | undefined;
let linter: core.Linter;
let linterLanguageService!: ts.LanguageService;
let linterSyntaxOnlyLanguageService!: ts.LanguageService;

const snapshots = new Map<string, ts.IScriptSnapshot>();
const versions = new Map<string, number>();
Expand Down Expand Up @@ -68,6 +69,7 @@ const originalHost: ts.LanguageServiceHost = {
};
const linterHost: ts.LanguageServiceHost = { ...originalHost };
const originalService = ts.createLanguageService(linterHost);
const originalSyntaxOnlyService = ts.createLanguageService(linterHost, undefined, true);

export function createLocal() {
return {
Expand Down Expand Up @@ -171,6 +173,7 @@ async function setup(
}
}
linterLanguageService = originalService;
linterSyntaxOnlyLanguageService = originalSyntaxOnlyService;
language = undefined;

const plugins = await languagePlugins.load(tsconfig, languages);
Expand All @@ -190,9 +193,14 @@ async function setup(
}
);
decorateLanguageServiceHost(ts, language, linterHost);

const proxy = createProxyLanguageService(linterLanguageService);
proxy.initialize(language);
linterLanguageService = proxy.proxy;

const syntaxOnly = createProxyLanguageService(linterSyntaxOnlyLanguageService);
syntaxOnly.initialize(language);
linterSyntaxOnlyLanguageService = syntaxOnly.proxy;
}

projectVersion++;
Expand All @@ -204,13 +212,18 @@ async function setup(
allowNonTsExtensions: true,
}
: _options;
linter = core.createLinter({
configFile,
languageService: linterLanguageService,
languageServiceHost: linterHost,
typescript: ts,
tsconfig: ts.server.toNormalizedPath(tsconfig),
}, config, 'cli');
linter = core.createLinter(
{
configFile,
languageService: linterLanguageService,
languageServiceHost: linterHost,
typescript: ts,
tsconfig: ts.server.toNormalizedPath(tsconfig),
},
config,
'cli',
linterSyntaxOnlyLanguageService
);

return true;
}
Expand All @@ -222,7 +235,7 @@ function lintAndFix(fileName: string, fileCache: core.FileLintCache) {
let diagnostics!: ts.DiagnosticWithLocation[];

while (shouldRetry && retry--) {
if (Object.values(fileCache[1]).some(([fixes]) => fixes > 0)) {
if (Object.values(fileCache[1]).some(([hasFix]) => hasFix)) {
// Reset the cache if there are any fixes applied.
fileCache[1] = {};
fileCache[2] = {};
Expand Down
Loading

0 comments on commit 7ea4a09

Please sign in to comment.