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

Rename Register to Service; re-export as Register for backwards… #1158

Merged
merged 3 commits into from Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/bin.ts
Expand Up @@ -9,7 +9,7 @@ import { diffLines } from 'diff'
import { Script } from 'vm'
import { readFileSync, statSync, realpathSync } from 'fs'
import { homedir } from 'os'
import { VERSION, TSError, parse, Register, register } from './index'
import { VERSION, TSError, parse, Service, register } from './index'

/**
* Eval filename for REPL/debug.
Expand Down Expand Up @@ -284,7 +284,7 @@ function getCwd (dir?: string, scriptMode?: boolean, scriptPath?: string) {
/**
* Evaluate a script.
*/
function evalAndExit (service: Register, state: EvalState, module: Module, code: string, isPrinted: boolean) {
function evalAndExit (service: Service, state: EvalState, module: Module, code: string, isPrinted: boolean) {
let result: any

;(global as any).__filename = module.filename
Expand Down Expand Up @@ -312,7 +312,7 @@ function evalAndExit (service: Register, state: EvalState, module: Module, code:
/**
* 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 @@ -351,7 +351,7 @@ function exec (code: string, filename: string) {
/**
* Start a CLI REPL.
*/
function startRepl (service: Register, state: EvalState, code?: string) {
function startRepl (service: Service, state: EvalState, code?: string) {
// Eval incoming code before the REPL starts.
if (code) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/index.spec.ts
Expand Up @@ -643,7 +643,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 @@ -796,7 +796,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 @@ -823,7 +823,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
23 changes: 15 additions & 8 deletions src/index.ts
Expand Up @@ -37,7 +37,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 @@ -351,9 +351,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 @@ -363,6 +363,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 @@ -393,7 +400,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 @@ -414,7 +421,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 @@ -1003,12 +1010,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 @@ -1024,7 +1031,7 @@ function registerExtensions (
*/
function registerExtension (
ext: string,
register: Register,
register: Service,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this variable too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch.

originalHandler: (m: NodeModule, filename: string) => any
) {
const old = require.extensions[ext] || originalHandler // tslint:disable-line
Expand Down