Skip to content

Commit

Permalink
chore(compiler): improve logging (#1443)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Mar 24, 2020
1 parent cced137 commit 0eb468d
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 75 deletions.
16 changes: 8 additions & 8 deletions e2e/__tests__/__snapshots__/logger.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Array [
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
]
Expand Down Expand Up @@ -65,15 +65,15 @@ Array [
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] calling babel-jest processor",
Expand Down Expand Up @@ -109,15 +109,15 @@ Array [
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] calling babel-jest processor",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] calling babel-jest processor",
Expand Down Expand Up @@ -148,14 +148,14 @@ Array [
"[level:20] compileUsingLanguageService(): creating language service",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
"[level:20] computing cache key for <cwd>/Hello.ts",
"[level:20] processing <cwd>/Hello.ts",
"[level:20] readThrough(): no cache",
"[level:20] compileFn(): compiling using language service",
"[level:20] updateMemoryCache(): update memory cache",
"[level:20] updateMemoryCache(): update memory cache for language service",
"[level:20] visitSourceFileNode(): hoisting",
"[level:20] compileFn(): computing diagnostics for language service",
]
Expand Down
1 change: 0 additions & 1 deletion src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const rootDir = filePath('')
export function tsJestConfig(options?: Partial<TsJestConfig>): TsJestConfig {
return {
compilerHost: false,
emit: false,
incremental: false,
isolatedModules: false,
compiler: 'typescript',
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,5 @@ export const createCompiler = (configs: ConfigSet): TsCompiler => {
}
const compile = readThrough(cachedir, memoryCache, compileResult.compileFn, getExtension, logger)

return { cwd: configs.cwd, compile, extensions, cachedir, ts, program: compileResult.program }
return { cwd: configs.cwd, compile, program: compileResult.program }
}
2 changes: 1 addition & 1 deletion src/compiler/language-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('language service', () => {
",
"[level:20] compileFn(): compiling using language service
",
"[level:20] updateMemoryCache(): update memory cache
"[level:20] updateMemoryCache(): update memory cache for language service
",
"[level:20] visitSourceFileNode(): hoisting
",
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const compileUsingLanguageService = (
}
// Set the file contents into cache.
const updateMemoryCache = (code: string, fileName: string) => {
logger.debug({ fileName }, `updateMemoryCache(): update memory cache`)
logger.debug({ fileName }, `updateMemoryCache(): update memory cache for language service`)

const fileVersion = memoryCache.versions[fileName] ?? 0,
isFileInCache = fileVersion !== 0
Expand Down Expand Up @@ -85,7 +85,7 @@ export const compileUsingLanguageService = (
}

logger.debug('compileUsingLanguageService(): creating language service')
const service = ts.createLanguageService(serviceHost)
const service: _ts.LanguageService = ts.createLanguageService(serviceHost)

return {
compileFn: (code: string, fileName: string): SourceOutput => {
Expand All @@ -103,7 +103,6 @@ export const compileUsingLanguageService = (
.getCompilerOptionsDiagnostics()
.concat(service.getSyntacticDiagnostics(normalizedFileName))
.concat(service.getSemanticDiagnostics(normalizedFileName))

// will raise or just warn diagnostics depending on config
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('cache', () => {
",
"[level:20] compileFn(): compiling using program
",
"[level:20] updateMemoryCache(): update memory cache
"[level:20] updateMemoryCache(): update memory cache for program
",
"[level:20] visitSourceFileNode(): hoisting
",
Expand Down Expand Up @@ -207,7 +207,7 @@ Array [
",
"[level:20] compileFn(): compiling using incremental program
",
"[level:20] updateMemoryCache(): update memory cache
"[level:20] updateMemoryCache(): update memory cache for incremental program
",
"[level:20] visitSourceFileNode(): hoisting
",
Expand Down
30 changes: 18 additions & 12 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ConfigSet } from '../config/config-set'
import { CompileResult, MemoryCache, SourceOutput } from '../types'
import { Errors, interpolate } from '../util/messages'

const hasOwn = Object.prototype.hasOwnProperty

/**
* @internal
*/
Expand Down Expand Up @@ -38,11 +40,9 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
}
let builderProgram: _ts.EmitAndSemanticDiagnosticsBuilderProgram, program: _ts.Program, host: _ts.CompilerHost
if (incremental) {
// TODO: Find a way to trigger typescript to build project when there are project references.
// At the moment this Incremental Program doesn't work with project references
host = ts.createIncrementalCompilerHost(options, sys)
builderProgram = ts.createIncrementalProgram({
rootNames: Object.keys(memoryCache.versions).slice(),
rootNames: Object.keys(memoryCache.versions),
options,
host,
configFileParsingDiagnostics: errors,
Expand All @@ -64,7 +64,7 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames,
}
program = ts.createProgram({
rootNames: Object.keys(memoryCache.versions).slice(),
rootNames: Object.keys(memoryCache.versions),
options,
host,
configFileParsingDiagnostics: errors,
Expand All @@ -74,12 +74,13 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
// Read and cache custom transformers.
const customTransformers = configs.tsCustomTransformers,
updateMemoryCache = (code: string, fileName: string): void => {
logger.debug({ fileName }, `updateMemoryCache(): update memory cache`)
logger.debug(
{ fileName },
`updateMemoryCache(): update memory cache for ${incremental ? 'incremental program' : 'program'}`,
)

const fileVersion = memoryCache.versions[fileName] ?? 0,
isFileInCache = fileVersion !== 0,
sourceFile = incremental ? builderProgram.getSourceFile(fileName) : program.getSourceFile(fileName)
if (!isFileInCache) {
const sourceFile = incremental ? builderProgram.getSourceFile(fileName) : program.getSourceFile(fileName)
if (!hasOwn.call(memoryCache.versions, fileName)) {
memoryCache.versions[fileName] = 1
}
if (memoryCache.contents[fileName] !== code) {
Expand All @@ -89,7 +90,7 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
// Update program when file changes.
if (sourceFile === undefined || sourceFile.text !== code || program.isSourceFileFromExternalLibrary(sourceFile)) {
const programOptions = {
rootNames: Object.keys(memoryCache.versions).slice(),
rootNames: Object.keys(memoryCache.versions),
options,
host,
configFileParsingDiagnostics: errors,
Expand All @@ -99,7 +100,10 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
builderProgram = ts.createIncrementalProgram(programOptions)
program = builderProgram.getProgram()
} else {
program = ts.createProgram(programOptions)
program = ts.createProgram({
...programOptions,
oldProgram: program,
})
}
}
}
Expand Down Expand Up @@ -146,7 +150,9 @@ export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCa
`compileFn(): computing diagnostics for ${incremental ? 'incremental program' : 'program'}`,
)

const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile).slice()
const diagnostics = program
.getSemanticDiagnostics(sourceFile)
.concat(program.getSyntacticDiagnostics(sourceFile))
// will raise or just warn diagnostics depending on config
configs.raiseDiagnostics(diagnostics, normalizedFileName, logger)
}
Expand Down
2 changes: 0 additions & 2 deletions src/config/__snapshots__/config-set.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ Object {
"pretty": true,
"throws": true,
},
"emit": false,
"incremental": true,
"isolatedModules": false,
"packageJson": Object {
Expand Down Expand Up @@ -143,7 +142,6 @@ Object {
"pretty": true,
"throws": true,
},
"emit": false,
"incremental": true,
"isolatedModules": false,
"packageJson": Object {
Expand Down
12 changes: 2 additions & 10 deletions src/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { resolve } from 'path' // tslint:disable-next-line:no-duplicate-imports
import * as ts from 'typescript'

import * as _myModule from '..'
import { mocked } from '../../utils'
import * as fakers from '../__helpers__/fakers'
import { logTargetMock } from '../__helpers__/mocks'
import { TsJestGlobalOptions } from '../types'
import * as _backports from '../util/backports'
import { getPackageVersion } from '../util/get-package-version'
import { normalizeSlashes } from '../util/normalize-slashes'
import { mocked } from '../util/testing'

import { ConfigSet, IGNORE_DIAGNOSTIC_CODES, MATCH_NOTHING } from './config-set'

Expand Down Expand Up @@ -884,14 +884,6 @@ describe('tsCompiler', () => {
const cs = createConfigSet({ tsJestConfig: { tsConfig: false } as any })
const compiler = cs.tsCompiler
expect(compiler.cwd).toBe(cs.cwd)
expect(compiler.cachedir).toBe(cs.tsCacheDir)
expect(compiler.extensions).toMatchInlineSnapshot(`
Array [
".ts",
".tsx",
]
`)
expect(compiler.ts).toBe(cs.compilerModule)
expect(typeof compiler.compile).toBe('function')
})
}) // tsCompiler
Expand Down Expand Up @@ -966,7 +958,7 @@ describe('cacheKey', () => {
cs.jsonValue.value = val
// digest is mocked in src/__mocks__/index.ts
expect(cs.cacheKey).toMatchInlineSnapshot(
`"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"compilerHost\\":false,\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"emit\\":false,\\"incremental\\":true,\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`,
`"{\\"digest\\":\\"a0d51ca854194df8191d0e65c0ca4730f510f332\\",\\"jest\\":{\\"__backported\\":true,\\"globals\\":{}},\\"projectDepVersions\\":{\\"dev\\":\\"1.2.5\\",\\"opt\\":\\"1.2.3\\",\\"peer\\":\\"1.2.4\\",\\"std\\":\\"1.2.6\\"},\\"transformers\\":[\\"hoisting-jest-mock@1\\"],\\"tsJest\\":{\\"compiler\\":\\"typescript\\",\\"compilerHost\\":false,\\"diagnostics\\":{\\"ignoreCodes\\":[6059,18002,18003],\\"pretty\\":true,\\"throws\\":true},\\"incremental\\":true,\\"isolatedModules\\":false,\\"packageJson\\":{\\"kind\\":\\"file\\"},\\"transformers\\":[]},\\"tsconfig\\":{\\"declaration\\":false,\\"inlineSourceMap\\":false,\\"inlineSources\\":true,\\"module\\":1,\\"noEmit\\":false,\\"removeComments\\":false,\\"sourceMap\\":true,\\"target\\":1}}"`,
)
})
}) // cacheKey
Expand Down
1 change: 0 additions & 1 deletion src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ export class ConfigSet {
tsConfig,
compilerHost: options.compilerHost ?? false,
incremental: options.incremental ?? true,
emit: options.emit ?? false,
packageJson,
babelConfig,
diagnostics,
Expand Down
34 changes: 0 additions & 34 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,10 @@ export interface TsJestGlobalOptions {

/**
* Use TypeScript's Incremental Program. This option only works when `compilerHost` is `true`
* TODO: Remove this flag when we can make project references working with our Incremental Program
* @default false
*/
incremental?: boolean

/**
* Emit compiled files into `.ts-jest` directory
*
* @default false
*/
emit?: boolean

/**
* Compiler to use (default to 'typescript'):
*/
Expand Down Expand Up @@ -150,7 +142,6 @@ export interface TsJestConfig {
isolatedModules: boolean
compilerHost: boolean
incremental: boolean // TODO: Remove this flag when we can make project references working with our Incremental Program
emit: boolean
compiler: string
diagnostics: TsJestConfig$diagnostics
babelConfig: TsJestConfig$babelConfig
Expand All @@ -168,33 +159,8 @@ export interface TsJestHooksMap {
*/
export type ModulePatcher<T = any> = (module: T) => T

/**
* Common TypeScript interfaces between versions.
*/
export interface TSCommon {
version: typeof _ts.version
sys: typeof _ts.sys
ScriptSnapshot: typeof _ts.ScriptSnapshot
displayPartsToString: typeof _ts.displayPartsToString
createLanguageService: typeof _ts.createLanguageService
getDefaultLibFilePath: typeof _ts.getDefaultLibFilePath
getPreEmitDiagnostics: typeof _ts.getPreEmitDiagnostics
flattenDiagnosticMessageText: typeof _ts.flattenDiagnosticMessageText
transpileModule: typeof _ts.transpileModule
ModuleKind: typeof _ts.ModuleKind
ScriptTarget: typeof _ts.ScriptTarget
findConfigFile: typeof _ts.findConfigFile
readConfigFile: typeof _ts.readConfigFile
parseJsonConfigFileContent: typeof _ts.parseJsonConfigFileContent
formatDiagnostics: typeof _ts.formatDiagnostics
formatDiagnosticsWithColorAndContext: typeof _ts.formatDiagnosticsWithColorAndContext
}

export interface TsCompiler {
cwd: string
extensions: string[]
cachedir: string | undefined
ts: TSCommon
compile(code: string, fileName: string, lineOffset?: number): string
program: _ts.Program | undefined
}
Expand Down

0 comments on commit 0eb468d

Please sign in to comment.