Skip to content

Commit

Permalink
fix: handle absolute paths for the eslint.files option (#419)
Browse files Browse the repository at this point in the history
Closes: #414
  • Loading branch information
piotr-oles authored May 25, 2020
1 parent 19dba48 commit 1ff3ce4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
10 changes: 7 additions & 3 deletions src/eslint-reporter/EsLintReporterConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import webpack from 'webpack';
import { isAbsolute, join } from 'path';
import { EsLintReporterOptions } from './EsLintReporterOptions';
import { CLIEngineOptions } from './types/eslint';

Expand All @@ -7,7 +8,6 @@ interface EsLintReporterConfiguration {
memoryLimit: number;
options: CLIEngineOptions;
files: string[];
cwd: string;
}

function castToArray<T>(value: T | T[] | undefined): T[] {
Expand All @@ -28,8 +28,12 @@ function createEsLintReporterConfiguration(
enabled: !!(options && options.enabled === true),
memoryLimit: 2048,
...(typeof options === 'object' ? options : {}),
files: typeof options === 'object' ? castToArray(options.files) : [],
cwd: compiler.options.context || process.cwd(),
files: (typeof options === 'object' ? castToArray(options.files) : []).map((filesPattern) =>
// ensure that `filesPattern` is an absolute path
isAbsolute(filesPattern)
? filesPattern
: join(compiler.options.context || process.cwd(), filesPattern)
),
options: {
cwd: compiler.options.context || process.cwd(),
extensions: ['.js', '.ts', '.tsx'],
Expand Down
3 changes: 1 addition & 2 deletions src/eslint-reporter/reporter/EsLintReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { createIssuesFromEsLintResults } from '../issue/EsLintIssueFactory';
import { EsLintReporterConfiguration } from '../EsLintReporterConfiguration';
import { Reporter } from '../../reporter';
import minimatch from 'minimatch';
import { join } from 'path';

function createEsLintReporter(configuration: EsLintReporterConfiguration): Reporter {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -34,7 +33,7 @@ function createEsLintReporter(configuration: EsLintReporterConfiguration): Repor
} else {
const changedAndIncludedFiles = changedFiles.filter((changedFile) =>
includedFilesPatterns.some((includedFilesPattern) =>
minimatch(changedFile, join(configuration.cwd, includedFilesPattern))
minimatch(changedFile, includedFilesPattern)
)
);

Expand Down
53 changes: 31 additions & 22 deletions test/e2e/EsLint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('EsLint', () => {
});

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

if (absolute) {
// test case for providing absolute path to files
await sandbox.patch(
'webpack.config.js',
"files: './src/**/*.ts'",
"files: path.resolve(__dirname, './src/**/*.ts')"
);
}

const driver = createWebpackDevServerDriver(sandbox.spawn('npm run webpack-dev-server'), async);
let errors: string[];

Expand Down Expand Up @@ -92,34 +101,34 @@ describe('EsLint', () => {

// add a new error
await sandbox.patch(
'src/index.ts',
"let password = '';",
"let password = ''\nlet temporary: any;"
'src/model/User.ts',
[' lastName?: string;', '}'].join('\n'),
[' lastName?: string;', '}', '', 'let temporary: any;', ''].join('\n')
);

errors = await driver.waitForErrors();
expect(errors).toEqual([
[
'WARNING in src/index.ts 20:5-19',
'WARNING in src/model/User.ts 11:5-19',
"@typescript-eslint/no-unused-vars: 'temporary' is defined but never used.",
" 18 | let email = '';",
" 19 | let password = ''",
' > 20 | let temporary: any;',
' 9 | }',
' 10 | ',
' > 11 | let temporary: any;',
' | ^^^^^^^^^^^^^^',
' 21 | ',
" 22 | emailInput.addEventListener('change', event => {",
' 23 | if (event.target instanceof HTMLInputElement) {',
' 12 | ',
' 13 | ',
' 14 | function getUserName(user: User): string {',
].join('\n'),
[
'WARNING in src/index.ts 20:16-19',
'WARNING in src/model/User.ts 11:16-19',
'@typescript-eslint/no-explicit-any: Unexpected any. Specify a different type.',
" 18 | let email = '';",
" 19 | let password = ''",
' > 20 | let temporary: any;',
' 9 | }',
' 10 | ',
' > 11 | let temporary: any;',
' | ^^^',
' 21 | ',
" 22 | emailInput.addEventListener('change', event => {",
' 23 | if (event.target instanceof HTMLInputElement) {',
' 12 | ',
' 13 | ',
' 14 | function getUserName(user: User): string {',
].join('\n'),
]);
});
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/sandbox/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ async function createSandbox(): Promise<Sandbox> {
process.stdout.write(`Killing child process ${childProcess.pid}...\n`);
await retry(
() =>
new Promise((resolve, reject) =>
new Promise((resolve) =>
kill(childProcess.pid, 'SIGKILL', (error) => {
if (error) {
reject(error);
} else {
resolve();
// we don't want to reject as it's probably some OS issue
// or already killed process
console.error(error);
}
resolve();
})
)
);
Expand Down

0 comments on commit 1ff3ce4

Please sign in to comment.