Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make #884 regression test repro correctly; also fix the bug #999

Merged
merged 4 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,13 @@ export function create (rawOptions: CreateOptions = {}): Register {
// Use full language services when the fast option is disabled.
if (!transpileOnly) {
const fileContents = new Map<string, string>()
const rootFileNames = config.fileNames.slice()
const rootFileNames = new Set(config.fileNames)
const cachedReadFile = cachedLookup(debugFn('readFile', readFile))

// Use language services by default (TODO: invert next major version).
if (!options.compilerHost) {
let projectVersion = 1
const fileVersions = new Map(rootFileNames.map(fileName => [fileName, 0]))
const fileVersions = new Map(Array.from(rootFileNames).map(fileName => [fileName, 0]))

const getCustomTransformers = () => {
if (typeof transformers === 'function') {
Expand All @@ -497,7 +497,7 @@ export function create (rawOptions: CreateOptions = {}): Register {
// Create the compiler host for type checking.
const serviceHost: _ts.LanguageServiceHost = {
getProjectVersion: () => String(projectVersion),
getScriptFileNames: () => Array.from(fileVersions.keys()),
getScriptFileNames: () => Array.from(rootFileNames),
getScriptVersion: (fileName: string) => {
const version = fileVersions.get(fileName)
return version ? version.toString() : ''
Expand Down Expand Up @@ -533,9 +533,12 @@ export function create (rawOptions: CreateOptions = {}): Register {
const service = ts.createLanguageService(serviceHost, registry)

const updateMemoryCache = (contents: string, fileName: string) => {
// Add to `rootFiles` when discovered for the first time.
if (!fileVersions.has(fileName)) {
rootFileNames.push(fileName)
// Add to `rootFiles` if not already there
// This is necessary to force TS to emit output
if (!rootFileNames.has(fileName)) {
rootFileNames.add(fileName)
// Increment project version for every change to rootFileNames.
projectVersion++
}

const previousVersion = fileVersions.get(fileName) || 0
Expand Down Expand Up @@ -637,14 +640,14 @@ export function create (rawOptions: CreateOptions = {}): Register {
// Fallback for older TypeScript releases without incremental API.
let builderProgram = ts.createIncrementalProgram
? ts.createIncrementalProgram({
rootNames: rootFileNames.slice(),
rootNames: Array.from(rootFileNames),
options: config.options,
host: host,
configFileParsingDiagnostics: config.errors,
projectReferences: config.projectReferences
})
: ts.createEmitAndSemanticDiagnosticsBuilderProgram(
rootFileNames.slice(),
Array.from(rootFileNames),
config.options,
host,
undefined,
Expand All @@ -665,13 +668,13 @@ export function create (rawOptions: CreateOptions = {}): Register {

// Add to `rootFiles` when discovered by compiler for the first time.
if (sourceFile === undefined) {
rootFileNames.push(fileName)
rootFileNames.add(fileName)
}

// Update program when file changes.
if (sourceFile === undefined || sourceFile.text !== contents) {
builderProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram(
rootFileNames.slice(),
Array.from(rootFileNames),
config.options,
host,
builderProgram,
Expand Down
7 changes: 7 additions & 0 deletions tests/issue-884/index-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export {};

const timeout = setTimeout(() => {}, 0);

if (timeout.unref) {
timeout.unref();
}
8 changes: 3 additions & 5 deletions tests/issue-884/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const timeout = setTimeout(() => {}, 0);

if (timeout.unref) {
timeout.unref();
}
// 2x index files required so that memory cache is populated with all build-in lib and @types
// declarations *before* this require() call.
require('./index-2');