generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use TypeScript LanguageService (#518)
Switching from just a `Program` to the `LanguageService` has several benefits: - Adding/changing source files is possible without having to create a new program manually while keeping all unchanged files cached - Host implementation is more lightweight - A shared `LanguageService` can be used to improve performance when linting multiple independent files/projects within the same process/thread. This is currently only used internally by the tests, but a new public API can be easily provided.
- Loading branch information
Showing
14 changed files
with
403 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import ts from "typescript"; | ||
|
||
export default class LanguageServiceHostProxy implements ts.LanguageServiceHost { | ||
private readonly emptyLanguageServiceHost: ts.LanguageServiceHost; | ||
private languageServiceHost: ts.LanguageServiceHost; | ||
|
||
constructor() { | ||
this.emptyLanguageServiceHost = this.languageServiceHost = new EmptyLanguageServiceHost(); | ||
} | ||
|
||
setHost(languageServiceHostImpl: ts.LanguageServiceHost | null) { | ||
this.languageServiceHost = languageServiceHostImpl ?? this.emptyLanguageServiceHost; | ||
} | ||
|
||
// ts.LanguageServiceHost implementation: | ||
|
||
getCompilationSettings() { | ||
return this.languageServiceHost.getCompilationSettings(); | ||
} | ||
|
||
getScriptFileNames() { | ||
return this.languageServiceHost.getScriptFileNames(); | ||
} | ||
|
||
getScriptVersion(fileName: string) { | ||
return this.languageServiceHost.getScriptVersion(fileName); | ||
} | ||
|
||
getScriptSnapshot(fileName: string) { | ||
return this.languageServiceHost.getScriptSnapshot(fileName); | ||
} | ||
|
||
fileExists(filePath: string) { | ||
return this.languageServiceHost.fileExists(filePath); | ||
} | ||
|
||
readFile(filePath: string) { | ||
return this.languageServiceHost.readFile(filePath); | ||
} | ||
|
||
getDefaultLibFileName(options: ts.CompilerOptions) { | ||
return this.languageServiceHost.getDefaultLibFileName(options); | ||
} | ||
|
||
getCurrentDirectory() { | ||
return this.languageServiceHost.getCurrentDirectory(); | ||
} | ||
} | ||
|
||
export class EmptyLanguageServiceHost implements ts.LanguageServiceHost { | ||
getCompilationSettings() { | ||
return {}; | ||
} | ||
|
||
getScriptFileNames() { | ||
return []; | ||
} | ||
|
||
getScriptVersion() { | ||
return "0"; | ||
} | ||
|
||
getScriptSnapshot() { | ||
return undefined; | ||
} | ||
|
||
fileExists() { | ||
return false; | ||
} | ||
|
||
readFile() { | ||
return undefined; | ||
} | ||
|
||
getCurrentDirectory() { | ||
return "/"; | ||
} | ||
|
||
getDefaultLibFileName(options: ts.CompilerOptions) { | ||
return ts.getDefaultLibFileName(options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import ts from "typescript"; | ||
import LanguageServiceHostProxy from "./LanguageServiceHostProxy.js"; | ||
|
||
export default class SharedLanguageService { | ||
private readonly languageServiceHostProxy: LanguageServiceHostProxy; | ||
private readonly languageService: ts.LanguageService; | ||
private acquired = false; | ||
private projectScriptVersion = 0; | ||
|
||
constructor() { | ||
this.languageServiceHostProxy = new LanguageServiceHostProxy(); | ||
this.languageService = ts.createLanguageService(this.languageServiceHostProxy, ts.createDocumentRegistry()); | ||
} | ||
|
||
acquire(languageServiceHost: ts.LanguageServiceHost) { | ||
if (this.acquired) { | ||
throw new Error("SharedCompiler is already acquired"); | ||
} | ||
this.acquired = true; | ||
|
||
// Set actual LanguageServiceHost implementation | ||
this.languageServiceHostProxy.setHost(languageServiceHost); | ||
} | ||
|
||
getProgram() { | ||
if (!this.acquired) { | ||
throw new Error("SharedCompiler is not acquired"); | ||
} | ||
|
||
const program = this.languageService.getProgram(); | ||
if (!program) { | ||
throw new Error("SharedCompiler failed to create a program"); | ||
} | ||
return program; | ||
} | ||
|
||
release() { | ||
if (!this.acquired) { | ||
throw new Error("SharedCompiler is not acquired"); | ||
} | ||
|
||
// Remove previously set LanguageServiceHost implementation | ||
this.languageServiceHostProxy.setHost(null); | ||
|
||
this.acquired = false; | ||
} | ||
|
||
getNextProjectScriptVersion() { | ||
this.projectScriptVersion++; | ||
return this.projectScriptVersion.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.