Skip to content

Commit 1ff3ce4

Browse files
authored
fix: handle absolute paths for the eslint.files option (#419)
Closes: #414
1 parent 19dba48 commit 1ff3ce4

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

src/eslint-reporter/EsLintReporterConfiguration.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import webpack from 'webpack';
2+
import { isAbsolute, join } from 'path';
23
import { EsLintReporterOptions } from './EsLintReporterOptions';
34
import { CLIEngineOptions } from './types/eslint';
45

@@ -7,7 +8,6 @@ interface EsLintReporterConfiguration {
78
memoryLimit: number;
89
options: CLIEngineOptions;
910
files: string[];
10-
cwd: string;
1111
}
1212

1313
function castToArray<T>(value: T | T[] | undefined): T[] {
@@ -28,8 +28,12 @@ function createEsLintReporterConfiguration(
2828
enabled: !!(options && options.enabled === true),
2929
memoryLimit: 2048,
3030
...(typeof options === 'object' ? options : {}),
31-
files: typeof options === 'object' ? castToArray(options.files) : [],
32-
cwd: compiler.options.context || process.cwd(),
31+
files: (typeof options === 'object' ? castToArray(options.files) : []).map((filesPattern) =>
32+
// ensure that `filesPattern` is an absolute path
33+
isAbsolute(filesPattern)
34+
? filesPattern
35+
: join(compiler.options.context || process.cwd(), filesPattern)
36+
),
3337
options: {
3438
cwd: compiler.options.context || process.cwd(),
3539
extensions: ['.js', '.ts', '.tsx'],

src/eslint-reporter/reporter/EsLintReporter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { createIssuesFromEsLintResults } from '../issue/EsLintIssueFactory';
44
import { EsLintReporterConfiguration } from '../EsLintReporterConfiguration';
55
import { Reporter } from '../../reporter';
66
import minimatch from 'minimatch';
7-
import { join } from 'path';
87

98
function createEsLintReporter(configuration: EsLintReporterConfiguration): Reporter {
109
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -34,7 +33,7 @@ function createEsLintReporter(configuration: EsLintReporterConfiguration): Repor
3433
} else {
3534
const changedAndIncludedFiles = changedFiles.filter((changedFile) =>
3635
includedFilesPatterns.some((includedFilesPattern) =>
37-
minimatch(changedFile, join(configuration.cwd, includedFilesPattern))
36+
minimatch(changedFile, includedFilesPattern)
3837
)
3938
);
4039

test/e2e/EsLint.spec.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ describe('EsLint', () => {
2323
});
2424

2525
it.each([
26-
{ async: false, webpack: '4.0.0' },
27-
{ async: true, webpack: '^4.0.0' },
28-
{ async: false, webpack: '^5.0.0-beta.16' },
29-
{ async: true, webpack: '^5.0.0-beta.16' },
30-
])('reports lint error for %p', async ({ async, webpack }) => {
26+
{ async: false, webpack: '4.0.0', absolute: false },
27+
{ async: true, webpack: '^4.0.0', absolute: true },
28+
{ async: false, webpack: '^5.0.0-beta.16', absolute: true },
29+
{ async: true, webpack: '^5.0.0-beta.16', absolute: false },
30+
])('reports lint error for %p', async ({ async, webpack, absolute }) => {
3131
await sandbox.load([
3232
await readFixture(join(__dirname, 'fixtures/environment/eslint-basic.fixture'), {
3333
FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify(
@@ -43,6 +43,15 @@ describe('EsLint', () => {
4343
await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')),
4444
]);
4545

46+
if (absolute) {
47+
// test case for providing absolute path to files
48+
await sandbox.patch(
49+
'webpack.config.js',
50+
"files: './src/**/*.ts'",
51+
"files: path.resolve(__dirname, './src/**/*.ts')"
52+
);
53+
}
54+
4655
const driver = createWebpackDevServerDriver(sandbox.spawn('npm run webpack-dev-server'), async);
4756
let errors: string[];
4857

@@ -92,34 +101,34 @@ describe('EsLint', () => {
92101

93102
// add a new error
94103
await sandbox.patch(
95-
'src/index.ts',
96-
"let password = '';",
97-
"let password = ''\nlet temporary: any;"
104+
'src/model/User.ts',
105+
[' lastName?: string;', '}'].join('\n'),
106+
[' lastName?: string;', '}', '', 'let temporary: any;', ''].join('\n')
98107
);
99108

100109
errors = await driver.waitForErrors();
101110
expect(errors).toEqual([
102111
[
103-
'WARNING in src/index.ts 20:5-19',
112+
'WARNING in src/model/User.ts 11:5-19',
104113
"@typescript-eslint/no-unused-vars: 'temporary' is defined but never used.",
105-
" 18 | let email = '';",
106-
" 19 | let password = ''",
107-
' > 20 | let temporary: any;',
114+
' 9 | }',
115+
' 10 | ',
116+
' > 11 | let temporary: any;',
108117
' | ^^^^^^^^^^^^^^',
109-
' 21 | ',
110-
" 22 | emailInput.addEventListener('change', event => {",
111-
' 23 | if (event.target instanceof HTMLInputElement) {',
118+
' 12 | ',
119+
' 13 | ',
120+
' 14 | function getUserName(user: User): string {',
112121
].join('\n'),
113122
[
114-
'WARNING in src/index.ts 20:16-19',
123+
'WARNING in src/model/User.ts 11:16-19',
115124
'@typescript-eslint/no-explicit-any: Unexpected any. Specify a different type.',
116-
" 18 | let email = '';",
117-
" 19 | let password = ''",
118-
' > 20 | let temporary: any;',
125+
' 9 | }',
126+
' 10 | ',
127+
' > 11 | let temporary: any;',
119128
' | ^^^',
120-
' 21 | ',
121-
" 22 | emailInput.addEventListener('change', event => {",
122-
' 23 | if (event.target instanceof HTMLInputElement) {',
129+
' 12 | ',
130+
' 13 | ',
131+
' 14 | function getUserName(user: User): string {',
123132
].join('\n'),
124133
]);
125134
});

test/e2e/sandbox/Sandbox.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ async function createSandbox(): Promise<Sandbox> {
9494
process.stdout.write(`Killing child process ${childProcess.pid}...\n`);
9595
await retry(
9696
() =>
97-
new Promise((resolve, reject) =>
97+
new Promise((resolve) =>
9898
kill(childProcess.pid, 'SIGKILL', (error) => {
9999
if (error) {
100-
reject(error);
101-
} else {
102-
resolve();
100+
// we don't want to reject as it's probably some OS issue
101+
// or already killed process
102+
console.error(error);
103103
}
104+
resolve();
104105
})
105106
)
106107
);

0 commit comments

Comments
 (0)