1
1
import * as cp from 'child_process' ;
2
2
import * as fs from 'fs' ;
3
3
import * as minimatch from 'minimatch' ;
4
- import { dirname , delimiter } from 'path' ;
4
+ import { dirname , delimiter , relative } from 'path' ;
5
5
import * as tslint from 'tslint' ; // this is a dev dependency only
6
6
import * as typescript from 'typescript' ; // this is a dev dependency only
7
7
import * as util from 'util' ;
@@ -208,14 +208,19 @@ export class TsLintRunner {
208
208
this . trace ( 'start doValidate ' + filePath ) ;
209
209
const uri = filePath ;
210
210
211
- if ( this . fileIsExcluded ( configuration , filePath ) ) {
211
+ let cwd = configuration . workspaceFolderPath ;
212
+ if ( ! cwd && typeof contents === "object" ) {
213
+ cwd = contents . getCurrentDirectory ( ) ;
214
+ }
215
+
216
+ if ( this . fileIsExcluded ( configuration , filePath , cwd ) ) {
212
217
this . trace ( `No linting: file ${ filePath } is excluded` ) ;
213
218
return emptyResult ;
214
219
}
215
220
216
- if ( configuration . workspaceFolderPath ) {
217
- this . trace ( `Changed directory to ${ configuration . workspaceFolderPath } ` ) ;
218
- process . chdir ( configuration . workspaceFolderPath ) ;
221
+ if ( cwd ) {
222
+ this . trace ( `Changed directory to ${ cwd } ` ) ;
223
+ process . chdir ( cwd ) ;
219
224
}
220
225
221
226
const configFile = configuration . configFile || null ;
@@ -338,7 +343,7 @@ export class TsLintRunner {
338
343
return this . configCache . configuration ;
339
344
}
340
345
341
- private fileIsExcluded ( settings : RunConfiguration , filePath : string ) : boolean {
346
+ private fileIsExcluded ( settings : RunConfiguration , filePath : string , cwd : string | undefined ) : boolean {
342
347
if ( settings . ignoreDefinitionFiles ) {
343
348
if ( filePath . endsWith ( '.d.ts' ) ) {
344
349
return true ;
@@ -348,11 +353,11 @@ export class TsLintRunner {
348
353
if ( settings . exclude ) {
349
354
if ( Array . isArray ( settings . exclude ) ) {
350
355
for ( const pattern of settings . exclude ) {
351
- if ( testForExclusionPattern ( filePath , pattern ) ) {
356
+ if ( testForExclusionPattern ( filePath , pattern , cwd ) ) {
352
357
return true ;
353
358
}
354
359
}
355
- } else if ( testForExclusionPattern ( filePath , settings . exclude ) ) {
360
+ } else if ( testForExclusionPattern ( filePath , settings . exclude , cwd ) ) {
356
361
return true ;
357
362
}
358
363
}
@@ -390,7 +395,18 @@ export class TsLintRunner {
390
395
}
391
396
}
392
397
393
- function testForExclusionPattern ( filePath : string , pattern : string ) : boolean {
398
+ function testForExclusionPattern ( filePath : string , pattern : string , cwd : string | undefined ) : boolean {
399
+ if ( cwd ) {
400
+ // try first as relative
401
+ const relPath = relative ( cwd , filePath ) ;
402
+ if ( minimatch ( relPath , pattern , { dot : true } ) ) {
403
+ return true ;
404
+ }
405
+ if ( relPath === filePath ) {
406
+ return false ;
407
+ }
408
+ }
409
+
394
410
return minimatch ( filePath , pattern , { dot : true } ) ;
395
411
}
396
412
@@ -433,7 +449,7 @@ function isExcludedFromLinterOptions(
433
449
if ( config === undefined || config . linterOptions === undefined || config . linterOptions . exclude === undefined ) {
434
450
return false ;
435
451
}
436
- return config . linterOptions . exclude . some ( pattern => testForExclusionPattern ( fileName , pattern ) ) ;
452
+ return config . linterOptions . exclude . some ( pattern => testForExclusionPattern ( fileName , pattern , undefined ) ) ;
437
453
}
438
454
439
455
function getConfigurationFailureMessage ( err : any ) : string {
0 commit comments