-
-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(typescript): refactor compiler host (#214)
- Loading branch information
Showing
9 changed files
with
135 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { PluginContext } from 'rollup'; | ||
|
||
import { DiagnosticsHost } from './host'; | ||
import diagnosticToWarning from './toWarning'; | ||
|
||
// `Cannot compile modules into 'es6' when targeting 'ES5' or lower.` | ||
const CANNOT_COMPILE_ESM = 1204; | ||
|
||
/** | ||
* For each type error reported by Typescript, emit a Rollup warning or error. | ||
*/ | ||
export default function emitDiagnostics( | ||
ts: typeof import('typescript'), | ||
context: PluginContext, | ||
host: DiagnosticsHost, | ||
diagnostics: readonly import('typescript').Diagnostic[] | undefined | ||
) { | ||
if (!diagnostics) return; | ||
const { noEmitOnError } = host.getCompilationSettings(); | ||
|
||
diagnostics | ||
.filter((diagnostic) => diagnostic.code !== CANNOT_COMPILE_ESM) | ||
.forEach((diagnostic) => { | ||
// Build a Rollup warning object from the diagnostics object. | ||
const warning = diagnosticToWarning(ts, host, diagnostic); | ||
|
||
// Errors are fatal. Otherwise emit warnings. | ||
if (noEmitOnError && diagnostic.category === ts.DiagnosticCategory.Error) { | ||
context.error(warning); | ||
} else { | ||
context.warn(warning); | ||
} | ||
}); | ||
} |
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,38 @@ | ||
type FormatDiagnosticsHost = import('typescript').FormatDiagnosticsHost; | ||
|
||
export interface DiagnosticsHost extends FormatDiagnosticsHost { | ||
getCompilationSettings(): import('typescript').CompilerOptions; | ||
} | ||
|
||
/** | ||
* Create a format diagnostics host to use with the Typescript type checking APIs. | ||
* Typescript hosts are used to represent the user's system, | ||
* with an API for checking case sensitivity etc. | ||
* @param compilerOptions Typescript compiler options. Affects functions such as `getNewLine`. | ||
* @see https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API | ||
*/ | ||
export default function createFormattingHost( | ||
ts: typeof import('typescript'), | ||
compilerOptions: import('typescript').CompilerOptions | ||
): DiagnosticsHost { | ||
return { | ||
/** Returns the compiler options for the project. */ | ||
getCompilationSettings: () => compilerOptions, | ||
/** Returns the current working directory. */ | ||
getCurrentDirectory: () => process.cwd(), | ||
/** Returns the string that corresponds with the selected `NewLineKind`. */ | ||
getNewLine() { | ||
switch (compilerOptions.newLine) { | ||
case ts.NewLineKind.CarriageReturnLineFeed: | ||
return '\r\n'; | ||
case ts.NewLineKind.LineFeed: | ||
return '\n'; | ||
default: | ||
return ts.sys.newLine; | ||
} | ||
}, | ||
/** Returns a lower case name on case insensitive systems, otherwise the original name. */ | ||
getCanonicalFileName: (fileName) => | ||
ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase() | ||
}; | ||
} |
35 changes: 2 additions & 33 deletions
35
packages/typescript/src/diagnostics.ts → ...s/typescript/src/diagnostics/toWarning.ts
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,19 @@ | ||
export type ModuleResolutionHost = import('typescript').ModuleResolutionHost; | ||
|
||
/** | ||
* Creates a module resolution host to use with the Typescript compiler API. | ||
* Typescript hosts are used to represent the user's system, | ||
* with an API for reading files, checking directories and case sensitivity etc. | ||
* @see https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API | ||
*/ | ||
export default function createModuleResolutionHost( | ||
ts: typeof import('typescript') | ||
): ModuleResolutionHost { | ||
return { | ||
fileExists: ts.sys.fileExists, | ||
readFile: ts.sys.readFile, | ||
directoryExists: ts.sys.directoryExists, | ||
realpath: ts.sys.realpath, | ||
getDirectories: ts.sys.getDirectories | ||
}; | ||
} |
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