From ea6e1da071e4ee82a551ff59e6e5ffa91c5858d4 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 26 Nov 2019 21:56:09 -0800 Subject: [PATCH] Make `--ignore` relative --- src/index.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5bb6a71f4..a4f32385b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -252,13 +252,8 @@ export function create (options: CreateOptions = {}): Register { ...(options.ignoreDiagnostics || []) ].map(Number) - const ignore = options.skipIgnore ? [] : ( - options.ignore || ['/node_modules/'] - ).map(str => new RegExp(str)) - // Require the TypeScript compiler and configuration. const cwd = options.dir ? resolve(options.dir) : process.cwd() - const isScoped = options.scope ? (fileName: string) => relative(cwd, fileName).charAt(0) !== '.' : () => true const typeCheck = options.typeCheck === true || options.transpileOnly !== true const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] }) const ts: typeof _ts = require(compiler) @@ -269,6 +264,11 @@ export function create (options: CreateOptions = {}): Register { const configDiagnosticList = filterDiagnostics(config.errors, ignoreDiagnostics) const outputCache = new Map() + const isScoped = options.scope ? (relname: string) => relname.charAt(0) !== '.' : () => true + const shouldIgnore = createIgnore(options.skipIgnore ? [] : ( + options.ignore || ['/node_modules/'] + ).map(str => new RegExp(str))) + const diagnosticHost: _ts.FormatDiagnosticsHost = { getNewLine: () => ts.sys.newLine, getCurrentDirectory: () => cwd, @@ -462,7 +462,11 @@ export function create (options: CreateOptions = {}): Register { let active = true const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled) - const ignored = (fileName: string) => !active || !isScoped(fileName) || shouldIgnore(fileName, ignore) + const ignored = (fileName: string) => { + if (!active) return true + const relname = relative(cwd, fileName) + return !isScoped(relname) || shouldIgnore(relname) + } return { ts, config, compile, getTypeInfo, ignored, enabled } } @@ -470,10 +474,12 @@ export function create (options: CreateOptions = {}): Register { /** * Check if the filename should be ignored. */ -function shouldIgnore (filename: string, ignore: RegExp[]) { - const relname = normalizeSlashes(filename) +function createIgnore (ignore: RegExp[]) { + return (relname: string) => { + const path = normalizeSlashes(relname) - return ignore.some(x => x.test(relname)) + return ignore.some(x => x.test(path)) + } } /**