Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into public-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Dec 3, 2020
2 parents 7f1a7a2 + 286c294 commit 5267c0b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
7 changes: 1 addition & 6 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import { join, resolve, dirname } from 'path'
import { inspect } from 'util'
import Module = require('module')
import arg = require('arg')
import {
parse,
register,
TSError,
VERSION
} from './index'
import {
EVAL_FILENAME,
EvalState,
createRepl,
ReplService
} from './repl'
import { VERSION, TSError, parse, register } from './index'

/**
* Main `bin` functionality.
Expand Down
6 changes: 3 additions & 3 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ describe('ts-node', function () {
})

describe('register', function () {
let registered: tsNodeTypes.Register
let registered: tsNodeTypes.Service
let moduleTestPath: string
before(() => {
registered = register({
Expand Down Expand Up @@ -825,7 +825,7 @@ describe('ts-node', function () {
})

describe('create', () => {
let service: tsNodeTypes.Register
let service: tsNodeTypes.Service
before(() => {
service = create({ compilerOptions: { target: 'es5' }, skipProject: true })
})
Expand All @@ -852,7 +852,7 @@ describe('ts-node', function () {
})

describe('issue #1098', () => {
function testIgnored (ignored: tsNodeTypes.Register['ignored'], allowed: string[], disallowed: string[]) {
function testIgnored (ignored: tsNodeTypes.Service['ignored'], allowed: string[], disallowed: string[]) {
for (const ext of allowed) {
expect(ignored(join(__dirname, `index${ext}`))).equal(false, `should accept ${ext} files`)
}
Expand Down
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const REGISTER_INSTANCE = Symbol.for('ts-node.register.instance')
declare global {
namespace NodeJS {
interface Process {
[REGISTER_INSTANCE]?: Register
[REGISTER_INSTANCE]?: Service
}
}
}
Expand Down Expand Up @@ -356,9 +356,9 @@ export class TSError extends BaseError {
}

/**
* Return type for registering `ts-node`.
* Primary ts-node service, which wraps the TypeScript API and can compile TypeScript to JavaScript
*/
export interface Register {
export interface Service {
ts: TSCommon
config: _ts.ParsedCommandLine
options: RegisterOptions
Expand All @@ -368,6 +368,13 @@ export interface Register {
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
}

/**
* Re-export of `Service` interface for backwards-compatibility
* @deprecated use `Service` instead
* @see Service
*/
export type Register = Service

/**
* Cached fs operation wrapper.
*/
Expand Down Expand Up @@ -398,7 +405,7 @@ export function getExtensions (config: _ts.ParsedCommandLine) {
/**
* Register TypeScript compiler instance onto node.js
*/
export function register (opts: RegisterOptions = {}): Register {
export function register (opts: RegisterOptions = {}): Service {
const originalJsHandler = require.extensions['.js'] // tslint:disable-line
const service = create(opts)
const { tsExtensions, jsExtensions } = getExtensions(service.config)
Expand All @@ -419,7 +426,7 @@ export function register (opts: RegisterOptions = {}): Register {
/**
* Create TypeScript compiler instance.
*/
export function create (rawOptions: CreateOptions = {}): Register {
export function create (rawOptions: CreateOptions = {}): Service {
const dir = rawOptions.dir ?? DEFAULTS.dir
const compilerName = rawOptions.compiler ?? DEFAULTS.compiler
const cwd = dir ? resolve(dir) : process.cwd()
Expand Down Expand Up @@ -1008,12 +1015,12 @@ function reorderRequireExtension (ext: string) {
function registerExtensions (
preferTsExts: boolean | null | undefined,
extensions: string[],
register: Register,
service: Service,
originalJsHandler: (m: NodeModule, filename: string) => any
) {
// Register new extensions.
for (const ext of extensions) {
registerExtension(ext, register, originalJsHandler)
registerExtension(ext, service, originalJsHandler)
}

if (preferTsExts) {
Expand All @@ -1029,15 +1036,15 @@ function registerExtensions (
*/
function registerExtension (
ext: string,
register: Register,
service: Service,
originalHandler: (m: NodeModule, filename: string) => any
) {
const old = require.extensions[ext] || originalHandler // tslint:disable-line

require.extensions[ext] = function (m: any, filename) { // tslint:disable-line
if (register.ignored(filename)) return old(m, filename)
if (service.ignored(filename)) return old(m, filename)

if (register.options.experimentalEsmLoader) {
if (service.options.experimentalEsmLoader) {
assertScriptCanLoadAsCJS(filename)
}

Expand All @@ -1046,7 +1053,7 @@ function registerExtension (
m._compile = function (code: string, fileName: string) {
debug('module._compile', fileName)

return _compile.call(this, register.compile(code, fileName), fileName)
return _compile.call(this, service.compile(code, fileName), fileName)
}

return old(m, filename)
Expand Down
12 changes: 6 additions & 6 deletions src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { homedir } from 'os'
import { join } from 'path'
import { Recoverable, start } from 'repl'
import { Script } from 'vm'
import { Register, CreateOptions, TSError } from './index'
import { Service, CreateOptions, TSError } from './index'
import { readFileSync, statSync } from 'fs'
import { Console } from 'console'
import * as tty from 'tty'
Expand All @@ -19,7 +19,7 @@ export interface ReplService {
/**
* Bind this REPL to a ts-node compiler service. A compiler service must be bound before `eval`-ing code or starting the REPL
*/
setService (service: Register): void
setService (service: Service): void
evalCode (code: string): void
/**
* `eval` implementation compatible with node's REPL API
Expand All @@ -39,7 +39,7 @@ export interface ReplService {
}

export interface CreateReplOptions {
service?: Register
service?: Service
state?: EvalState
stdin?: NodeJS.ReadableStream
stdout?: NodeJS.WritableStream
Expand Down Expand Up @@ -69,7 +69,7 @@ export function createRepl (options: CreateReplOptions = {}) {
}
return replService

function setService (_service: Register) {
function setService (_service: Service) {
service = _service
}

Expand Down Expand Up @@ -160,7 +160,7 @@ export function createEvalAwarePartialHost (state: EvalState): EvalAwarePartialH
/**
* Evaluate the code snippet.
*/
function _eval (service: Register, state: EvalState, input: string) {
function _eval (service: Service, state: EvalState, input: string) {
const lines = state.lines
const isCompletion = !/\n$/.test(input)
const undo = appendEval(state, input)
Expand Down Expand Up @@ -199,7 +199,7 @@ function exec (code: string, filename: string) {
/**
* Start a CLI REPL.
*/
function startRepl (replService: ReplService, service: Register, state: EvalState, code?: string) {
function startRepl (replService: ReplService, service: Service, state: EvalState, code?: string) {
// Eval incoming code before the REPL starts.
if (code) {
try {
Expand Down

0 comments on commit 5267c0b

Please sign in to comment.