Skip to content

Commit

Permalink
chore: rename some files and methods in compiler area (#1540)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Apr 18, 2020
1 parent 322780c commit e27c3d6
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Config } from '@jest/types'
import { resolve } from 'path'

import { createCompiler } from '../compiler/instance'
import { createCompilerInstance } from '../compiler/instance'
import { ConfigSet } from '../config/config-set'
import { BabelConfig, TsJestConfig, TsJestGlobalOptions } from '../types'
import { ImportReasons } from '../util/messages'
Expand Down Expand Up @@ -76,5 +76,5 @@ export function makeCompiler({
}
const cs = new ConfigSet(getJestConfig(jestConfig, tsJestConfig), parentConfig)

return createCompiler(cs)
return createCompilerInstance(cs)
}
14 changes: 7 additions & 7 deletions src/compiler/__snapshots__/language-service.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`language service should compile js file for allowJs true with outDir 1`] = `
exports[`Language service should compile js file for allowJs true with outDir 1`] = `
===[ FILE: test-allow-js-with-outDir.js ]=======================================
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand All @@ -18,7 +18,7 @@ exports[`language service should compile js file for allowJs true with outDir 1`
================================================================================
`;

exports[`language service should compile js file for allowJs true without outDir 1`] = `
exports[`Language service should compile js file for allowJs true without outDir 1`] = `
===[ FILE: test-allow-js-no-outDir.js ]=========================================
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand All @@ -36,7 +36,7 @@ exports[`language service should compile js file for allowJs true without outDir
================================================================================
`;

exports[`language service should compile tsx file for jsx preserve 1`] = `
exports[`Language service should compile tsx file for jsx preserve 1`] = `
===[ FILE: test-jsx-preserve.tsx ]==============================================
"use strict";
var App = function () {
Expand All @@ -60,7 +60,7 @@ exports[`language service should compile tsx file for jsx preserve 1`] = `
================================================================================
`;

exports[`language service should compile tsx file for other jsx options 1`] = `
exports[`Language service should compile tsx file for other jsx options 1`] = `
===[ FILE: test-jsx-options.tsx ]===============================================
"use strict";
var App = function () {
Expand All @@ -84,14 +84,14 @@ exports[`language service should compile tsx file for other jsx options 1`] = `
================================================================================
`;

exports[`language service should report diagnostics related to typings with pathRegex config matches file name 1`] = `"test-match-regex-diagnostics.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."`;
exports[`Language service should report diagnostics related to typings with pathRegex config matches file name 1`] = `"test-match-regex-diagnostics.ts(3,7): error TS2322: Type 'number' is not assignable to type 'string'."`;

exports[`language service should throw error when cannot compile 1`] = `
exports[`Language service should throw error when cannot compile 1`] = `
"Unable to require \`.d.ts\` file for file: test-cannot-compile.d.ts.
This is usually the result of a faulty configuration or import. Make sure there is a \`.js\`, \`.json\` or another executable extension available alongside \`test-cannot-compile.d.ts\`."
`;

exports[`language service should use the cache 3`] = `
exports[`Language service should use the cache 3`] = `
===[ FILE: test-cache.ts ]======================================================
console.log("hello");
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoidGVzdC1jYWNoZS50cyIsIm1hcHBpbmdzIjoiQUFBQSxPQUFPLENBQUMsR0FBRyxDQUFDLE9BQU8sQ0FBQyxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbInRlc3QtY2FjaGUudHMiXSwic291cmNlc0NvbnRlbnQiOlsiY29uc29sZS5sb2coXCJoZWxsb1wiKSJdLCJ2ZXJzaW9uIjozfQ==
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`transpile module with isolatedModule: true should compile js file for allowJs true 1`] = `
===[ FILE: src/compiler/transpile-module.spec.ts.test.js ]======================
exports[`Transpiler should compile js file for allowJs true 1`] = `
===[ FILE: src/compiler/transpiler.spec.ts.test.js ]============================
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 42;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vc3JjL2NvbXBpbGVyL3RyYW5zcGlsZS1tb2R1bGUuc3BlYy50cy50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L3NyYy9jb21waWxlci90cmFuc3BpbGUtbW9kdWxlLnNwZWMudHMudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ==
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vc3JjL2NvbXBpbGVyL3RyYW5zcGlsZXIuc3BlYy50cy50ZXN0LmpzIiwibWFwcGluZ3MiOiI7O0FBQUEsa0JBQWUsRUFBRSxDQUFBIiwibmFtZXMiOltdLCJzb3VyY2VzIjpbIjxjd2Q+L3NyYy9jb21waWxlci90cmFuc3BpbGVyLnNwZWMudHMudGVzdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZGVmYXVsdCA0MiJdLCJ2ZXJzaW9uIjozfQ==
===[ INLINE SOURCE MAPS ]=======================================================
file: <cwd>/src/compiler/transpile-module.spec.ts.test.js
file: <cwd>/src/compiler/transpiler.spec.ts.test.js
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
names: []
sources:
- <cwd>/src/compiler/transpile-module.spec.ts.test.js
- <cwd>/src/compiler/transpiler.spec.ts.test.js
sourcesContent:
- export default 42
version: 3
================================================================================
`;
exports[`transpile module with isolatedModule: true should compile tsx file for jsx preserve 1`] = `
exports[`Transpiler should compile tsx file for jsx preserve 1`] = `
===[ FILE: foo.tsx ]============================================================
"use strict";
var App = function () {
Expand All @@ -42,7 +42,7 @@ exports[`transpile module with isolatedModule: true should compile tsx file for
================================================================================
`;
exports[`transpile module with isolatedModule: true should compile tsx file for other jsx options 1`] = `
exports[`Transpiler should compile tsx file for other jsx options 1`] = `
===[ FILE: foo.tsx ]============================================================
"use strict";
var App = function () {
Expand All @@ -66,24 +66,24 @@ exports[`transpile module with isolatedModule: true should compile tsx file for
================================================================================
`;
exports[`transpile module with isolatedModule: true should compile using transpileModule and not use cache 1`] = `
===[ FILE: src/compiler/transpile-module.spec.ts ]==============================
exports[`Transpiler should compile using transpileModule and not use cache 1`] = `
===[ FILE: src/compiler/transpiler.spec.ts ]====================================
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 42;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vc3JjL2NvbXBpbGVyL3RyYW5zcGlsZS1tb2R1bGUuc3BlYy50cyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9zcmMvY29tcGlsZXIvdHJhbnNwaWxlLW1vZHVsZS5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJmaWxlIjoiPGN3ZD4vc3JjL2NvbXBpbGVyL3RyYW5zcGlsZXIuc3BlYy50cyIsIm1hcHBpbmdzIjoiOztBQUFBLGtCQUFlLEVBQUUsQ0FBQSIsIm5hbWVzIjpbXSwic291cmNlcyI6WyI8Y3dkPi9zcmMvY29tcGlsZXIvdHJhbnNwaWxlci5zcGVjLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImV4cG9ydCBkZWZhdWx0IDQyIl0sInZlcnNpb24iOjN9
===[ INLINE SOURCE MAPS ]=======================================================
file: <cwd>/src/compiler/transpile-module.spec.ts
file: <cwd>/src/compiler/transpiler.spec.ts
mappings: ';;AAAA,kBAAe,EAAE,CAAA'
names: []
sources:
- <cwd>/src/compiler/transpile-module.spec.ts
- <cwd>/src/compiler/transpiler.spec.ts
sourcesContent:
- export default 42
version: 3
================================================================================
`;
exports[`transpile module with isolatedModule: true should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
exports[`Transpiler should report diagnostics related to codes with pathRegex config is undefined 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
exports[`transpile module with isolatedModule: true should report diagnostics related to codes with pathRegex config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
exports[`Transpiler should report diagnostics related to codes with pathRegex config matches file name 1`] = `"foo.ts(2,23): error TS1005: '=>' expected."`;
23 changes: 12 additions & 11 deletions src/compiler/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import { CompileFn, CompilerInstance, MemoryCache, TsCompiler } from '../types'
import { sha1 } from '../util/sha1'

import { getResolvedModulesCache } from './compiler-utils'
import { compileUsingLanguageService } from './language-service'
import { compileUsingProgram } from './program'
import { compileUsingTranspileModule } from './transpile-module'
import { initializeLanguageServiceInstance } from './language-service'
import { initializeProgramInstance } from './program'
import { initializeTranspilerInstance } from './transpiler'

/**
* Update the output remapping the source map.
Expand Down Expand Up @@ -89,9 +89,10 @@ const isValidCacheContent = (contents: string): boolean => {
}

/**
* Wrap the function with caching.
* Compile files which are provided by jest via transform config and cache the result in file system if users run with
* cache mode
*/
const readThrough = (
const compileAndCacheResult = (
cachedir: string | undefined,
memoryCache: MemoryCache,
compileFn: CompileFn,
Expand Down Expand Up @@ -150,10 +151,10 @@ const readThrough = (
}

/**
* Register TypeScript compiler.
* Register TypeScript compiler instance.
* @internal
*/
export const createCompiler = (configs: ConfigSet): TsCompiler => {
export const createCompilerInstance = (configs: ConfigSet): TsCompiler => {
const logger = configs.logger.child({ namespace: 'ts-compiler' })
const {
typescript: { options: compilerOptions, fileNames },
Expand Down Expand Up @@ -186,12 +187,12 @@ export const createCompiler = (configs: ConfigSet): TsCompiler => {
if (!tsJest.isolatedModules) {
// Use language services by default
compilerInstance = !tsJest.compilerHost
? compileUsingLanguageService(configs, logger, memoryCache)
: compileUsingProgram(configs, logger, memoryCache)
? initializeLanguageServiceInstance(configs, logger, memoryCache)
: initializeProgramInstance(configs, logger, memoryCache)
} else {
compilerInstance = compileUsingTranspileModule(configs, logger)
compilerInstance = initializeTranspilerInstance(configs, logger)
}
const compile = readThrough(cachedir, memoryCache, compilerInstance.compileFn, getExtension, logger)
const compile = compileAndCacheResult(cachedir, memoryCache, compilerInstance.compileFn, getExtension, logger)

return { cwd: configs.cwd, compile, program: compilerInstance.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 @@ -11,7 +11,7 @@ import * as compilerUtils from './compiler-utils'

const logTarget = logTargetMock()

describe('language service', () => {
describe('Language service', () => {
beforeEach(() => {
logTarget.clear()
})
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/language-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function doTypeChecking(configs: ConfigSet, fileName: string, service: _ts.Langu
/**
* @internal
*/
export const compileUsingLanguageService = (
export const initializeLanguageServiceInstance = (
configs: ConfigSet,
logger: Logger,
memoryCache: MemoryCache,
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ function doTypeChecking(configs: ConfigSet, fileName: string, program: _ts.Progr
/**
* @internal
*/
export const compileUsingProgram = (configs: ConfigSet, logger: Logger, memoryCache: MemoryCache): CompilerInstance => {
export const initializeProgramInstance = (
configs: ConfigSet,
logger: Logger,
memoryCache: MemoryCache,
): CompilerInstance => {
logger.debug('compileUsingProgram(): create typescript compiler')

const ts = configs.compilerModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TS_JEST_OUT_DIR } from '../config/config-set'

const logTarget = logTargetMock()

describe('transpile module with isolatedModule: true', () => {
describe('Transpiler', () => {
const baseTsJestConfig = {
isolatedModules: true,
}
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/transpile-module.ts → src/compiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { getAndCacheOutputJSFileName, getAndCacheProjectReference } from './comp
/**
* @internal
*/
export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger): CompilerInstance => {
export const initializeTranspilerInstance = (configs: ConfigSet, logger: Logger): CompilerInstance => {
logger.debug('compileUsingTranspileModule(): create typescript compiler')

const { options, projectReferences, fileNames } = configs.typescript
const files: TSFiles = new Map<string, TSFile>()
const compiler = configs.compilerModule
const ts = configs.compilerModule
fileNames.forEach(filePath => {
const normalizedFilePath = normalize(filePath)
files.set(normalizedFilePath, {
Expand All @@ -25,12 +25,12 @@ export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger):
})
})
const program = projectReferences
? compiler.createProgram({
? ts.createProgram({
rootNames: fileNames,
options,
projectReferences,
})
: compiler.createProgram([], options)
: ts.createProgram([], options)
const updateFileInCache = (contents: string, filePath: string) => {
const file = files.get(filePath)
if (file && file.text !== contents) {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger):

const jsFileName = getAndCacheOutputJSFileName(normalizedFileName, referencedProject, files)
const relativeJSFileName = configs.resolvePath(jsFileName)
if (!compiler.sys.fileExists(jsFileName)) {
if (!ts.sys.fileExists(jsFileName)) {
throw new Error(
// tslint:disable-next-line:prefer-template
`Could not find output JavaScript file for input ` +
Expand All @@ -75,12 +75,12 @@ export const compileUsingTranspileModule = (configs: ConfigSet, logger: Logger):
}

const mapFileName = `${jsFileName}.map`
const outputText = compiler.sys.readFile(jsFileName)
const sourceMapText = compiler.sys.readFile(mapFileName)
const outputText = ts.sys.readFile(jsFileName)
const sourceMapText = ts.sys.readFile(mapFileName)

return [outputText!, sourceMapText!]
} else {
const result: _ts.TranspileOutput = compiler.transpileModule(code, {
const result: _ts.TranspileOutput = ts.transpileModule(code, {
fileName: normalizedFileName,
transformers: configs.tsCustomTransformers,
compilerOptions: options,
Expand Down
4 changes: 2 additions & 2 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from 'typescript'

import { digest as MY_DIGEST, version as MY_VERSION } from '..'
import { createCompiler } from '../compiler/instance'
import { createCompilerInstance } from '../compiler/instance'
import { internals as internalAstTransformers } from '../transformers'
import {
AstTransformerDesc,
Expand Down Expand Up @@ -435,7 +435,7 @@ export class ConfigSet {

@Memoize()
get tsCompiler(): TsCompiler {
return createCompiler(this)
return createCompilerInstance(this)
}

/**
Expand Down

0 comments on commit e27c3d6

Please sign in to comment.