Skip to content

Commit

Permalink
feat(typings): emit declaration files, filtering out internals
Browse files Browse the repository at this point in the history
Closes #745
  • Loading branch information
huafu committed Sep 21, 2018
1 parent d9c5b45 commit 4f10f7e
Show file tree
Hide file tree
Showing 25 changed files with 233 additions and 56 deletions.
6 changes: 6 additions & 0 deletions src/cli/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { Arguments } from 'yargs'
import { CliCommand } from '..'
import { createJestPreset } from '../../config/create-jest-preset'

/**
* @internal
*/
export const run: CliCommand = async (args: Arguments /* , logger: Logger */) => {
const file = args._[0] || 'jest.config.js'
const filePath = join(process.cwd(), file)
Expand Down Expand Up @@ -94,6 +97,9 @@ Jest configuration written to "${filePath}".
`)
}

/**
* @internal
*/
export const help: CliCommand = async () => {
process.stdout.write(`
Usage:
Expand Down
6 changes: 6 additions & 0 deletions src/cli/config/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { CliCommand } from '..'
import { createJestPreset } from '../../config/create-jest-preset'
import { backportJestConfig } from '../../util/backports'

/**
* @internal
*/
export const run: CliCommand = async (args: Arguments /*, logger: Logger*/) => {
const nullLogger = createLogger({ targets: [] })
const file = args._[0]
Expand Down Expand Up @@ -153,6 +156,9 @@ function dedupSort(arr: any[]) {
.sort((a, b) => (a.toString() > b.toString() ? 1 : a.toString() < b.toString() ? -1 : 0))
}

/**
* @internal
*/
export const help: CliCommand = async () => {
process.stdout.write(`
Usage:
Expand Down
8 changes: 7 additions & 1 deletion src/cli/help.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Arguments } from 'yargs'

/**
* @internal
*/
export const run = async (_: Arguments) => {
process.stdout.write(`
Usage:
Expand All @@ -15,4 +18,7 @@ Example:
`)
}

export { run as help }
/**
* @internal
*/
export const help = run
6 changes: 6 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const VALID_COMMANDS = ['help', 'config:migrate', 'config:init']

const logger = rootLogger.child({ [LogContexts.namespace]: 'cli', [LogContexts.application]: 'ts-jest' })

/**
* @internal
*/
export type CliCommand = (argv: Arguments, logger: Logger) => Promise<void>

async function cli(args: string[]): Promise<void> {
Expand All @@ -33,6 +36,9 @@ async function cli(args: string[]): Promise<void> {
return cmd(parsedArgv, logger)
}

/**
* @internal
*/
export async function processArgv(): Promise<void> {
try {
await cli(process.argv.slice(2))
Expand Down
1 change: 1 addition & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const hasOwn = Object.prototype.hasOwnProperty

/**
* Register TypeScript compiler.
* @internal
*/
export function createCompiler(configs: ConfigSet): TsCompiler {
const logger = configs.logger.child({ namespace: 'ts-compiler' })
Expand Down
106 changes: 71 additions & 35 deletions src/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,23 @@ interface ReadTsConfigResult {
resolved: ParsedCommandLine
}

/**
* @internal
*/
// this regex MUST match nothing, it's the goal ;-)
export const MATCH_NOTHING = /a^/
/**
* @internal
*/
export const IGNORE_DIAGNOSTIC_CODES = [
6059, // "'rootDir' is expected to contain all source files."
18002, // "The 'files' list in config file is empty."
18003, // "No inputs were found in config file."
]

/**
* @internal
*/
// WARNING: DO NOT CHANGE THE ORDER OF CODE NAMES!
// ONLY APPEND IF YOU NEED TO ADD SOME
export enum DiagnosticCodes {
Expand Down Expand Up @@ -103,16 +112,6 @@ const toDiagnosticCodeList = (items: any, into: number[] = []): number[] => {
}

export class ConfigSet {
readonly logger: Logger

constructor(
private readonly _jestConfig: jest.ProjectConfig,
readonly parentOptions?: TsJestGlobalOptions,
parentLogger?: Logger,
) {
this.logger = parentLogger ? parentLogger.child({ [LogContexts.namespace]: 'config' }) : logger
}

@Memoize()
get jest(): jest.ProjectConfig {
const config = backportJestConfig(this.logger, this._jestConfig)
Expand Down Expand Up @@ -234,6 +233,9 @@ export class ConfigSet {
)
}

/**
* @internal
*/
@Memoize()
private get _typescript(): ReadTsConfigResult {
const {
Expand All @@ -252,6 +254,9 @@ export class ConfigSet {
return result
}

/**
* @internal
*/
@Memoize()
get raiseDiagnostics() {
const {
Expand Down Expand Up @@ -349,6 +354,9 @@ export class ConfigSet {
return {}
}

/**
* @internal
*/
@Memoize()
get filterDiagnostics() {
const {
Expand Down Expand Up @@ -392,6 +400,9 @@ export class ConfigSet {
}
}

/**
* @internal
*/
@Memoize()
get createTsError() {
const {
Expand Down Expand Up @@ -471,10 +482,54 @@ export class ConfigSet {
return normalize(this.jest.cwd || process.cwd())
}

/**
* @internal
*/
get isDoctoring() {
return !!process.env.TS_JEST_DOCTOR
}

/**
* @internal
*/
@Memoize()
get jsonValue() {
const jest = { ...this.jest }
const globals = (jest.globals = { ...jest.globals } as any)
// we need to remove some stuff from jest config
// this which does not depend on config
delete jest.name
delete jest.cacheDirectory
// we do not need this since its normalized version is in tsJest
delete globals['ts-jest']

return new JsonableValue({
versions: this.versions,
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
jest,
tsJest: this.tsJest,
babel: this.babel,
tsconfig: this.tsconfig,
})
}

get cacheKey(): string {
return this.jsonValue.serialized
}
readonly logger: Logger
/**
* @internal
*/
private readonly _jestConfig: jest.ProjectConfig

constructor(jestConfig: jest.ProjectConfig, readonly parentOptions?: TsJestGlobalOptions, parentLogger?: Logger) {
this._jestConfig = jestConfig
this.logger = parentLogger ? parentLogger.child({ [LogContexts.namespace]: 'config' }) : logger
}

/**
* @internal
*/
makeDiagnostic(
code: number,
messageText: string,
Expand All @@ -491,6 +546,9 @@ export class ConfigSet {
}
}

/**
* @internal
*/
readTsConfig(
compilerOptions?: object,
resolvedConfigFile?: string | null,
Expand Down Expand Up @@ -608,31 +666,9 @@ export class ConfigSet {
return path
}

@Memoize()
get jsonValue() {
const jest = { ...this.jest }
const globals = (jest.globals = { ...jest.globals } as any)
// we need to remove some stuff from jest config
// this which does not depend on config
delete jest.name
delete jest.cacheDirectory
// we do not need this since its normalized version is in tsJest
delete globals['ts-jest']

return new JsonableValue({
versions: this.versions,
transformers: this.astTransformers.map(t => `${t.name}@${t.version}`),
jest,
tsJest: this.tsJest,
babel: this.babel,
tsconfig: this.tsconfig,
})
}

get cacheKey(): string {
return this.jsonValue.serialized
}

/**
* @internal
*/
toJSON() {
return this.jsonValue.value
}
Expand Down
38 changes: 22 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,53 @@ import { TsJestGlobalOptions } from './types'
import { VersionCheckers } from './util/version-checkers'

// tslint:disable-next-line:no-var-requires
const version: string = require('../package.json').version
export const version: string = require('../package.json').version

let transformer!: TsJestTransformer
function defaultTransformer(): TsJestTransformer {
return transformer || (transformer = createTransformer())
}

function createTransformer(baseConfig?: TsJestGlobalOptions) {
export function createTransformer(baseConfig?: TsJestGlobalOptions) {
VersionCheckers.jest.warn()
return new TsJestTransformer(baseConfig)
}
function tsProcess(...args: any[]): any {
/**
* @internal
*/
export function process(...args: any[]): any {
return (defaultTransformer().process as any)(...args)
}
function getCacheKey(...args: any[]): any {
/**
* @internal
*/
export function getCacheKey(...args: any[]): any {
return (defaultTransformer().getCacheKey as any)(...args)
}

/**
* @internal
*/
// we let jest doing the instrumentation, it does it well
const canInstrument = false
export const canInstrument = false

const jestPreset = createJestPreset()

/**
* @internal
*/
// for tests
// tslint:disable-next-line:variable-name
const __singleton = () => transformer
export const __singleton = () => transformer
/**
* @internal
*/
// tslint:disable-next-line:variable-name
const __resetModule = () => (transformer = undefined as any)
export const __resetModule = () => (transformer = undefined as any)

export {
version,
// jest API ===============
createTransformer,
tsProcess as process,
getCacheKey,
canInstrument,
// extra ==================
createJestPreset,
jestPreset,
pathsToModuleNameMapper,
// tests ==================
__singleton,
__resetModule,
}
7 changes: 7 additions & 0 deletions src/transformers/hoist-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ import { ConfigSet } from '../config/config-set'
*/
const HOIST_METHODS = ['mock', 'unmock']

/**
* @internal
*/
export const name = 'hoisting-jest-mock'
// increment this each time the code is modified
/**
* @internal
*/
export const version = 1

/**
* The factory of hoisting transformer factory
* @param cs Current jest configuration-set
* @internal
*/
export function factory(cs: ConfigSet) {
const logger = cs.logger.child({ namespace: 'ts-hoisting' })
Expand Down
3 changes: 3 additions & 0 deletions src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ import { AstTransformerDesc } from '../types'

import * as hoisting from './hoist-jest'

/**
* @internal
*/
export const internals: AstTransformerDesc[] = [hoisting]
12 changes: 12 additions & 0 deletions src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ interface ConfigSetIndexItem {
}

export class TsJestTransformer implements jest.Transformer {
/**
* @internal
*/
private static readonly _configSetsIndex: ConfigSetIndexItem[] = []
/**
* @internal
*/
private static _lastTransformerId = 0
static get lastTransformerId() {
return TsJestTransformer._lastTransformerId
}
/**
* @internal
*/
private static get _nextTransformerId() {
return ++TsJestTransformer._lastTransformerId
}
Expand All @@ -43,6 +52,9 @@ export class TsJestTransformer implements jest.Transformer {
this.logger.debug({ baseOptions }, 'created new transformer')
}

/**
* @internal
*/
/* istanbul ignore next */
[INSPECT_CUSTOM]() {
return `[object TsJestTransformer<#${this.id}>]`
Expand Down
Loading

0 comments on commit 4f10f7e

Please sign in to comment.