Skip to content

Commit 805b54c

Browse files
fix: path ignored (#312)
* fix: path ignored * style: formatting * chore: update cspell * style: formatting
1 parent f73c4ba commit 805b54c

File tree

19 files changed

+77
-38
lines changed

19 files changed

+77
-38
lines changed

.cspell.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
"ignorePaths": [
2020
"CHANGELOG.md",
2121
"package.json",
22+
"coverage/**",
2223
"dist/**",
2324
"**/__snapshots__/**",
24-
"package-lock.json"
25+
"package-lock.json",
26+
"/test/output"
2527
]
2628
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "Dependency Review"
1+
name: 'Dependency Review'
22
on: [pull_request]
33

44
permissions:
@@ -8,7 +8,7 @@ jobs:
88
dependency-review:
99
runs-on: ubuntu-latest
1010
steps:
11-
- name: "Checkout Repository"
11+
- name: 'Checkout Repository'
1212
uses: actions/checkout@v3
13-
- name: "Dependency Review"
13+
- name: 'Dependency Review'
1414
uses: actions/dependency-review-action@v3

src/getStylelint.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const cache = {};
1313
/** @typedef {import('stylelint')} Stylelint */
1414
/** @typedef {import('stylelint').LintResult} LintResult */
1515
/** @typedef {import('./options').Options} Options */
16+
/** @typedef {(stylelint: Stylelint, filePath: string) => Promise<boolean>} isPathIgnored */
1617
/** @typedef {() => Promise<void>} AsyncTask */
1718
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
18-
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
19+
/** @typedef {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lintFiles: LintTask, cleanup: AsyncTask, threads: number }} Linter */
1920
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
2021

2122
/**
@@ -26,9 +27,23 @@ function loadStylelint(options) {
2627
const stylelintOptions = getStylelintOptions(options);
2728
const stylelint = setup(options, stylelintOptions);
2829

30+
/** @type {isPathIgnored} */
31+
let isPathIgnored;
32+
33+
try {
34+
isPathIgnored = require(`${options.stylelintPath}/lib/isPathIgnored`);
35+
} catch (e) {
36+
try {
37+
// @ts-ignore
38+
isPathIgnored = require('stylelint/lib/isPathIgnored');
39+
} catch (_) {
40+
isPathIgnored = () => Promise.resolve(false);
41+
}
42+
}
43+
2944
return {
3045
stylelint,
31-
api: stylelint.createLinter(stylelintOptions),
46+
isPathIgnored,
3247
lintFiles,
3348
cleanup: async () => {},
3449
threads: 1,

src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ class StylelintWebpackPlugin {
9090
}
9191

9292
compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
93+
/** @type {import('stylelint')} */
94+
let stylelint;
95+
9396
/** @type {import('./linter').Linter} */
9497
let lint;
9598

96-
/** @type {import('stylelint').InternalApi} */
97-
let api;
99+
/** @type {import('./linter').isPathIgnored} */
100+
let isPathIgnored;
98101

99102
/** @type {import('./linter').Reporter} */
100103
let report;
@@ -103,7 +106,7 @@ class StylelintWebpackPlugin {
103106
let threads;
104107

105108
try {
106-
({ lint, api, report, threads } = linter(
109+
({ stylelint, lint, isPathIgnored, report, threads } = linter(
107110
this.key,
108111
options,
109112
compilation
@@ -127,7 +130,7 @@ class StylelintWebpackPlugin {
127130
: globby.sync(wanted, { dot: true, ignore: exclude })
128131
).map(async (file) => {
129132
try {
130-
return (await api.isPathIgnored(file)) ? false : file;
133+
return (await isPathIgnored(stylelint, file)) ? false : file;
131134
} catch (e) {
132135
return file;
133136
}

src/linter.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const { arrify } = require('./utils');
77
/** @typedef {import('stylelint')} Stylelint */
88
/** @typedef {import('stylelint').LintResult} LintResult */
99
/** @typedef {import('stylelint').LinterResult} LinterResult */
10-
/** @typedef {import('stylelint').InternalApi} InternalApi */
1110
/** @typedef {import('stylelint').Formatter} Formatter */
1211
/** @typedef {import('stylelint').FormatterType} FormatterType */
1312
/** @typedef {import('webpack').Compiler} Compiler */
1413
/** @typedef {import('webpack').Compilation} Compilation */
1514
/** @typedef {import('./options').Options} Options */
15+
/** @typedef {import('./getStylelint').isPathIgnored} isPathIgnored */
1616
/** @typedef {(compilation: Compilation) => Promise<void>} GenerateReport */
1717
/** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport}} Report */
1818
/** @typedef {() => Promise<Report>} Reporter */
@@ -26,14 +26,14 @@ const resultStorage = new WeakMap();
2626
* @param {string|undefined} key
2727
* @param {Options} options
2828
* @param {Compilation} compilation
29-
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
29+
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
3030
*/
3131
function linter(key, options, compilation) {
3232
/** @type {Stylelint} */
3333
let stylelint;
3434

35-
/** @type {InternalApi} */
36-
let api;
35+
/** @type {isPathIgnored} */
36+
let isPathIgnored;
3737

3838
/** @type {(files: string|string[]) => Promise<LintResult[]>} */
3939
let lintFiles;
@@ -50,7 +50,7 @@ function linter(key, options, compilation) {
5050
const crossRunResultStorage = getResultStorage(compilation);
5151

5252
try {
53-
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
53+
({ stylelint, isPathIgnored, lintFiles, cleanup, threads } = getStylelint(
5454
key,
5555
options
5656
));
@@ -59,8 +59,9 @@ function linter(key, options, compilation) {
5959
}
6060

6161
return {
62+
stylelint,
6263
lint,
63-
api,
64+
isPathIgnored,
6465
report,
6566
threads,
6667
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
display: block;
3+
}

test/fixtures/stylelint-path/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('file-loader!./test.scss');
2+
require('file-loader!./ignore.scss');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
display: block;
3+
}

test/mock/stylelint/index.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,4 @@ module.exports = {
2424
],
2525
};
2626
},
27-
28-
createLinter() {
29-
return {
30-
isPathIgnored() {
31-
return false;
32-
},
33-
};
34-
},
3527
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {import('stylelint')} stylelint
3+
* @param {string} filePath
4+
* @returns {Promise<boolean>}
5+
*/
6+
function isPathIgnored(stylelint, filePath) {
7+
return new Promise((resolve) => {
8+
resolve(filePath.endsWith('ignore.scss'));
9+
});
10+
}
11+
12+
module.exports = isPathIgnored;

test/stylelint-ignore.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import pack from './utils/pack';
22

33
describe('stylelint ignore', () => {
44
it('should ignore file', (done) => {
5-
const compiler = pack('stylelintignore');
5+
const compiler = pack('stylelint-ignore');
66

77
compiler.run((err, stats) => {
88
expect(stats.hasWarnings()).toBe(false);

test/stylelint-lint.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ describe('stylelint lint', () => {
99
jest.mock('stylelint', () => {
1010
return {
1111
lint: mockLintFiles,
12-
createLinter: () => {
13-
return {
14-
isPathIgnored: () => false,
15-
};
16-
},
1712
};
1813
});
14+
15+
jest.mock('stylelint/lib/isPathIgnored', () => {
16+
throw new Error();
17+
});
1918
});
2019

2120
beforeEach(() => {

test/stylelint-path.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import pack from './utils/pack';
55
describe('stylelint path', () => {
66
it('should use another instance of stylelint via stylelintPath config', (done) => {
77
const stylelintPath = join(__dirname, 'mock/stylelint');
8-
const compiler = pack('good', { stylelintPath });
8+
const compiler = pack('stylelint-path', { stylelintPath });
99

1010
compiler.run((err, stats) => {
1111
expect(err).toBeNull();

types/getStylelint.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ declare namespace getStylelint {
1414
Stylelint,
1515
LintResult,
1616
Options,
17+
isPathIgnored,
1718
AsyncTask,
1819
LintTask,
1920
Linter,
@@ -22,8 +23,8 @@ declare namespace getStylelint {
2223
}
2324
type Options = import('./options').Options;
2425
type Linter = {
25-
api: import('stylelint').InternalApi;
2626
stylelint: Stylelint;
27+
isPathIgnored: isPathIgnored;
2728
lintFiles: LintTask;
2829
cleanup: AsyncTask;
2930
threads: number;
@@ -87,6 +88,10 @@ type Stylelint = import('postcss').PluginCreator<
8788
};
8889
};
8990
type LintResult = import('stylelint').LintResult;
91+
type isPathIgnored = (
92+
stylelint: Stylelint,
93+
filePath: string
94+
) => Promise<boolean>;
9095
type AsyncTask = () => Promise<void>;
9196
type LintTask = (files: string | string[]) => Promise<LintResult[]>;
9297
type Worker = JestWorker & {

types/linter.d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ export = linter;
44
* @param {string|undefined} key
55
* @param {Options} options
66
* @param {Compilation} compilation
7-
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
7+
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
88
*/
99
declare function linter(
1010
key: string | undefined,
1111
options: Options,
1212
compilation: Compilation
1313
): {
14-
api: InternalApi;
14+
stylelint: Stylelint;
15+
isPathIgnored: getStylelint.isPathIgnored;
1516
lint: Linter;
1617
report: Reporter;
1718
threads: number;
@@ -21,12 +22,12 @@ declare namespace linter {
2122
Stylelint,
2223
LintResult,
2324
LinterResult,
24-
InternalApi,
2525
Formatter,
2626
FormatterType,
2727
Compiler,
2828
Compilation,
2929
Options,
30+
isPathIgnored,
3031
GenerateReport,
3132
Report,
3233
Reporter,
@@ -36,9 +37,6 @@ declare namespace linter {
3637
}
3738
type Options = import('./options').Options;
3839
type Compilation = import('webpack').Compilation;
39-
type InternalApi = import('stylelint').InternalApi;
40-
type Linter = (files: string | string[]) => void;
41-
type Reporter = () => Promise<Report>;
4240
type Stylelint = import('postcss').PluginCreator<
4341
import('stylelint').PostcssPluginOptions
4442
> & {
@@ -97,11 +95,15 @@ type Stylelint = import('postcss').PluginCreator<
9795
longhandSubPropertiesOfShorthandProperties: import('stylelint').LonghandSubPropertiesOfShorthandProperties;
9896
};
9997
};
98+
type Linter = (files: string | string[]) => void;
99+
type Reporter = () => Promise<Report>;
100+
import getStylelint = require('./getStylelint');
100101
type LintResult = import('stylelint').LintResult;
101102
type LinterResult = import('stylelint').LinterResult;
102103
type Formatter = import('stylelint').Formatter;
103104
type FormatterType = import('stylelint').FormatterType;
104105
type Compiler = import('webpack').Compiler;
106+
type isPathIgnored = import('./getStylelint').isPathIgnored;
105107
type GenerateReport = (compilation: Compilation) => Promise<void>;
106108
type Report = {
107109
errors?: StylelintError;

0 commit comments

Comments
 (0)