Skip to content

Commit 818b825

Browse files
fix: resolve paths and normalize (#97)
1 parent c12e7be commit 818b825

File tree

6 files changed

+69
-52
lines changed

6 files changed

+69
-52
lines changed

declarations/linter.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @param {string|undefined} key
33
* @param {Options} options
44
* @param {Compilation} compilation
5-
* @returns {{lint: Linter, report: Reporter}}
5+
* @returns {{lint: Linter, report: Reporter, threads: number}}
66
*/
77
export default function linter(
88
key: string | undefined,
@@ -11,6 +11,7 @@ export default function linter(
1111
): {
1212
lint: Linter;
1313
report: Reporter;
14+
threads: number;
1415
};
1516
export type ESLint = import('eslint').ESLint;
1617
export type Formatter = import('eslint').ESLint.Formatter;

declarations/utils.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
* @returns {string[]}
55
*/
66
export function parseFiles(files: string | string[], context: string): string[];
7-
/**
8-
* @param {string} str
9-
* @returns {string}
10-
*/
11-
export function replaceBackslashes(str: string): string;
127
/**
138
* @param {string|string[]} patterns
149
* @param {string|string[]} extensions

package-lock.json

Lines changed: 28 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"arrify": "^2.0.1",
5151
"jest-worker": "^26.6.2",
5252
"micromatch": "^4.0.2",
53+
"normalize-path": "^3.0.0",
5354
"schema-utils": "^3.0.0"
5455
},
5556
"devDependencies": {
@@ -60,6 +61,7 @@
6061
"@commitlint/config-conventional": "^11.0.0",
6162
"@types/fs-extra": "^9.0.6",
6263
"@types/micromatch": "^4.0.1",
64+
"@types/normalize-path": "^3.0.0",
6365
"@types/webpack": "^4.41.26",
6466
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
6567
"babel-eslint": "^10.1.0",

src/utils.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { join } from 'path';
1+
import { resolve } from 'path';
22
import { statSync } from 'fs';
33

4+
// @ts-ignore
5+
import normalizePath from 'normalize-path';
46
// @ts-ignore
57
import arrify from 'arrify';
68

@@ -11,18 +13,10 @@ import arrify from 'arrify';
1113
*/
1214
export function parseFiles(files, context) {
1315
return arrify(files).map((/** @type {string} */ file) =>
14-
replaceBackslashes(join(context, file))
16+
normalizePath(resolve(context, file))
1517
);
1618
}
1719

18-
/**
19-
* @param {string} str
20-
* @returns {string}
21-
*/
22-
export function replaceBackslashes(str) {
23-
return str.replace(/\\/g, '/');
24-
}
25-
2620
/**
2721
* @param {string|string[]} patterns
2822
* @param {string|string[]} extensions
@@ -35,26 +29,24 @@ export function parseFoldersToGlobs(patterns, extensions = []) {
3529
.map((/** @type {string} */ extension) => extension.replace(/^\./u, ''))
3630
.join(',');
3731

38-
return arrify(patterns)
39-
.map((/** @type {string} */ pattern) => replaceBackslashes(pattern))
40-
.map((/** @type {string} */ pattern) => {
41-
try {
42-
// The patterns are absolute because they are prepended with the context.
43-
const stats = statSync(pattern);
44-
/* istanbul ignore else */
45-
if (stats.isDirectory()) {
46-
return pattern.replace(
47-
/[/\\]*?$/u,
48-
`/**${
49-
extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''
50-
}`
51-
);
52-
}
53-
} catch (_) {
54-
// Return the pattern as is on error.
32+
return arrify(patterns).map((/** @type {string} */ pattern) => {
33+
try {
34+
// The patterns are absolute because they are prepended with the context.
35+
const stats = statSync(pattern);
36+
/* istanbul ignore else */
37+
if (stats.isDirectory()) {
38+
return pattern.replace(
39+
/[/\\]*?$/u,
40+
`/**${
41+
extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''
42+
}`
43+
);
5544
}
56-
return pattern;
57-
});
45+
} catch (_) {
46+
// Return the pattern as is on error.
47+
}
48+
return pattern;
49+
});
5850
}
5951

6052
/**

test/utils.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseFoldersToGlobs } from '../src/utils';
1+
import { parseFoldersToGlobs, parseFiles } from '../src/utils';
22

33
jest.mock('fs', () => {
44
return {
@@ -12,6 +12,21 @@ jest.mock('fs', () => {
1212
};
1313
});
1414

15+
test('parseFiles should return relative files from context', () => {
16+
expect(
17+
parseFiles(
18+
['**/*', '../package-a/src/**/', '../package-b/src/**/'],
19+
'main/src'
20+
)
21+
).toEqual(
22+
expect.arrayContaining([
23+
expect.stringContaining('main/src/**/*'),
24+
expect.stringContaining('main/package-a/src/**'),
25+
expect.stringContaining('main/package-b/src/**'),
26+
])
27+
);
28+
});
29+
1530
test('parseFoldersToGlobs should return globs for folders', () => {
1631
const withoutSlash = '/path/to/code';
1732
const withSlash = `${withoutSlash}/`;

0 commit comments

Comments
 (0)