Skip to content

Commit

Permalink
fix: resolve paths and normalize (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Apr 19, 2021
1 parent c12e7be commit 818b825
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 52 deletions.
3 changes: 2 additions & 1 deletion declarations/linter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{lint: Linter, report: Reporter}}
* @returns {{lint: Linter, report: Reporter, threads: number}}
*/
export default function linter(
key: string | undefined,
Expand All @@ -11,6 +11,7 @@ export default function linter(
): {
lint: Linter;
report: Reporter;
threads: number;
};
export type ESLint = import('eslint').ESLint;
export type Formatter = import('eslint').ESLint.Formatter;
Expand Down
5 changes: 0 additions & 5 deletions declarations/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
* @returns {string[]}
*/
export function parseFiles(files: string | string[], context: string): string[];
/**
* @param {string} str
* @returns {string}
*/
export function replaceBackslashes(str: string): string;
/**
* @param {string|string[]} patterns
* @param {string|string[]} extensions
Expand Down
44 changes: 28 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"arrify": "^2.0.1",
"jest-worker": "^26.6.2",
"micromatch": "^4.0.2",
"normalize-path": "^3.0.0",
"schema-utils": "^3.0.0"
},
"devDependencies": {
Expand All @@ -60,6 +61,7 @@
"@commitlint/config-conventional": "^11.0.0",
"@types/fs-extra": "^9.0.6",
"@types/micromatch": "^4.0.1",
"@types/normalize-path": "^3.0.0",
"@types/webpack": "^4.41.26",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-eslint": "^10.1.0",
Expand Down
50 changes: 21 additions & 29 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join } from 'path';
import { resolve } from 'path';
import { statSync } from 'fs';

// @ts-ignore
import normalizePath from 'normalize-path';
// @ts-ignore
import arrify from 'arrify';

Expand All @@ -11,18 +13,10 @@ import arrify from 'arrify';
*/
export function parseFiles(files, context) {
return arrify(files).map((/** @type {string} */ file) =>
replaceBackslashes(join(context, file))
normalizePath(resolve(context, file))
);
}

/**
* @param {string} str
* @returns {string}
*/
export function replaceBackslashes(str) {
return str.replace(/\\/g, '/');
}

/**
* @param {string|string[]} patterns
* @param {string|string[]} extensions
Expand All @@ -35,26 +29,24 @@ export function parseFoldersToGlobs(patterns, extensions = []) {
.map((/** @type {string} */ extension) => extension.replace(/^\./u, ''))
.join(',');

return arrify(patterns)
.map((/** @type {string} */ pattern) => replaceBackslashes(pattern))
.map((/** @type {string} */ pattern) => {
try {
// The patterns are absolute because they are prepended with the context.
const stats = statSync(pattern);
/* istanbul ignore else */
if (stats.isDirectory()) {
return pattern.replace(
/[/\\]*?$/u,
`/**${
extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''
}`
);
}
} catch (_) {
// Return the pattern as is on error.
return arrify(patterns).map((/** @type {string} */ pattern) => {
try {
// The patterns are absolute because they are prepended with the context.
const stats = statSync(pattern);
/* istanbul ignore else */
if (stats.isDirectory()) {
return pattern.replace(
/[/\\]*?$/u,
`/**${
extensionsGlob ? `/*.${prefix + extensionsGlob + postfix}` : ''
}`
);
}
return pattern;
});
} catch (_) {
// Return the pattern as is on error.
}
return pattern;
});
}

/**
Expand Down
17 changes: 16 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseFoldersToGlobs } from '../src/utils';
import { parseFoldersToGlobs, parseFiles } from '../src/utils';

jest.mock('fs', () => {
return {
Expand All @@ -12,6 +12,21 @@ jest.mock('fs', () => {
};
});

test('parseFiles should return relative files from context', () => {
expect(
parseFiles(
['**/*', '../package-a/src/**/', '../package-b/src/**/'],
'main/src'
)
).toEqual(
expect.arrayContaining([
expect.stringContaining('main/src/**/*'),
expect.stringContaining('main/package-a/src/**'),
expect.stringContaining('main/package-b/src/**'),
])
);
});

test('parseFoldersToGlobs should return globs for folders', () => {
const withoutSlash = '/path/to/code';
const withSlash = `${withoutSlash}/`;
Expand Down

0 comments on commit 818b825

Please sign in to comment.