Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 5663f68

Browse files
committed
handle relative patterns for exclude
1 parent 44c2bdd commit 5663f68

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

.vscode/launch.json

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
{
8-
"type": "node",
9-
"request": "launch",
10-
"name": "Mocha Tests",
11-
"program": "${workspaceFolder}/tslint-runner/node_modules/mocha/bin/_mocha",
12-
"args": [
13-
"-u",
14-
"tdd",
15-
"--timeout",
16-
"999999",
17-
"--colors",
18-
"${workspaceFolder}/tslint-runner/out/test",
19-
"-g", "getNonOverlappingReplacements"
20-
],
21-
"internalConsoleOptions": "openOnSessionStart"
22-
}
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Mocha Tests",
11+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12+
"args": [
13+
"-u",
14+
"tdd",
15+
"--timeout",
16+
"999999",
17+
"--colors",
18+
"${workspaceFolder}/out/runner/test",
19+
// "-g",
20+
// "exclude"
21+
],
22+
"internalConsoleOptions": "openOnSessionStart"
23+
}
2324
]
2425
}

src/runner/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cp from 'child_process';
22
import * as fs from 'fs';
33
import * as minimatch from 'minimatch';
4-
import { dirname, delimiter } from 'path';
4+
import { dirname, delimiter, relative } from 'path';
55
import * as tslint from 'tslint'; // this is a dev dependency only
66
import * as typescript from 'typescript'; // this is a dev dependency only
77
import * as util from 'util';
@@ -208,14 +208,19 @@ export class TsLintRunner {
208208
this.trace('start doValidate ' + filePath);
209209
const uri = filePath;
210210

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)) {
212217
this.trace(`No linting: file ${filePath} is excluded`);
213218
return emptyResult;
214219
}
215220

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);
219224
}
220225

221226
const configFile = configuration.configFile || null;
@@ -338,7 +343,7 @@ export class TsLintRunner {
338343
return this.configCache.configuration;
339344
}
340345

341-
private fileIsExcluded(settings: RunConfiguration, filePath: string): boolean {
346+
private fileIsExcluded(settings: RunConfiguration, filePath: string, cwd: string | undefined): boolean {
342347
if (settings.ignoreDefinitionFiles) {
343348
if (filePath.endsWith('.d.ts')) {
344349
return true;
@@ -348,11 +353,11 @@ export class TsLintRunner {
348353
if (settings.exclude) {
349354
if (Array.isArray(settings.exclude)) {
350355
for (const pattern of settings.exclude) {
351-
if (testForExclusionPattern(filePath, pattern)) {
356+
if (testForExclusionPattern(filePath, pattern, cwd)) {
352357
return true;
353358
}
354359
}
355-
} else if (testForExclusionPattern(filePath, settings.exclude)) {
360+
} else if (testForExclusionPattern(filePath, settings.exclude, cwd)) {
356361
return true;
357362
}
358363
}
@@ -390,7 +395,18 @@ export class TsLintRunner {
390395
}
391396
}
392397

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+
394410
return minimatch(filePath, pattern, { dot: true });
395411
}
396412

@@ -433,7 +449,7 @@ function isExcludedFromLinterOptions(
433449
if (config === undefined || config.linterOptions === undefined || config.linterOptions.exclude === undefined) {
434450
return false;
435451
}
436-
return config.linterOptions.exclude.some(pattern => testForExclusionPattern(fileName, pattern));
452+
return config.linterOptions.exclude.some(pattern => testForExclusionPattern(fileName, pattern, undefined));
437453
}
438454

439455
function getConfigurationFailureMessage(err: any): string {

src/runner/test/runner.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('TSLintRunner', () => {
6767
expect(errorResult.lintResult.warningCount).to.equal(0);
6868
});
6969

70-
it('should not return any errors for excluded file', () => {
70+
it('should not return any errors for excluded file (absolute path)', () => {
7171
const filePath = path.join(testDataRoot, 'with-tslint', 'test.ts');
7272
const result = createTsLintRunner().runTsLint(filePath, fs.readFileSync(filePath).toString(), {
7373
exclude: [filePath],
@@ -76,6 +76,17 @@ describe('TSLintRunner', () => {
7676
expect(result.lintResult.errorCount).to.equal(0);
7777
});
7878

79+
it('should not return any errors for excluded file (relative path)', () => {
80+
const root = path.join(testDataRoot, 'with-tslint');
81+
const filePath = path.join(root, 'test.ts');
82+
const result = createTsLintRunner().runTsLint(filePath, fs.readFileSync(filePath).toString(), {
83+
workspaceFolderPath: root,
84+
exclude: ['test.ts'],
85+
} as RunConfiguration);
86+
87+
expect(result.lintResult.errorCount).to.equal(0);
88+
});
89+
7990
it('should set working directory to workspace path', () => {
8091
const workspacePath = path.join(testDataRoot, 'with-tslint');
8192
const filePath = path.join(workspacePath, 'test.ts');

0 commit comments

Comments
 (0)