Skip to content

Commit

Permalink
fix: path ignored (#312)
Browse files Browse the repository at this point in the history
* fix: path ignored

* style: formatting

* chore: update cspell

* style: formatting
  • Loading branch information
ricardogobbosouza committed Jan 19, 2023
1 parent f73c4ba commit 805b54c
Show file tree
Hide file tree
Showing 19 changed files with 77 additions and 38 deletions.
4 changes: 3 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
"ignorePaths": [
"CHANGELOG.md",
"package.json",
"coverage/**",
"dist/**",
"**/__snapshots__/**",
"package-lock.json"
"package-lock.json",
"/test/output"
]
}
6 changes: 3 additions & 3 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Dependency Review"
name: 'Dependency Review'
on: [pull_request]

permissions:
Expand All @@ -8,7 +8,7 @@ jobs:
dependency-review:
runs-on: ubuntu-latest
steps:
- name: "Checkout Repository"
- name: 'Checkout Repository'
uses: actions/checkout@v3
- name: "Dependency Review"
- name: 'Dependency Review'
uses: actions/dependency-review-action@v3
19 changes: 17 additions & 2 deletions src/getStylelint.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const cache = {};
/** @typedef {import('stylelint')} Stylelint */
/** @typedef {import('stylelint').LintResult} LintResult */
/** @typedef {import('./options').Options} Options */
/** @typedef {(stylelint: Stylelint, filePath: string) => Promise<boolean>} isPathIgnored */
/** @typedef {() => Promise<void>} AsyncTask */
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
/** @typedef {{api: import('stylelint').InternalApi, stylelint: Stylelint, lintFiles: LintTask, cleanup: AsyncTask, threads: number, }} Linter */
/** @typedef {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lintFiles: LintTask, cleanup: AsyncTask, threads: number }} Linter */
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */

/**
Expand All @@ -26,9 +27,23 @@ function loadStylelint(options) {
const stylelintOptions = getStylelintOptions(options);
const stylelint = setup(options, stylelintOptions);

/** @type {isPathIgnored} */
let isPathIgnored;

try {
isPathIgnored = require(`${options.stylelintPath}/lib/isPathIgnored`);
} catch (e) {
try {
// @ts-ignore
isPathIgnored = require('stylelint/lib/isPathIgnored');
} catch (_) {
isPathIgnored = () => Promise.resolve(false);
}
}

return {
stylelint,
api: stylelint.createLinter(stylelintOptions),
isPathIgnored,
lintFiles,
cleanup: async () => {},
threads: 1,
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ class StylelintWebpackPlugin {
}

compiler.hooks.thisCompilation.tap(this.key, (compilation) => {
/** @type {import('stylelint')} */
let stylelint;

/** @type {import('./linter').Linter} */
let lint;

/** @type {import('stylelint').InternalApi} */
let api;
/** @type {import('./linter').isPathIgnored} */
let isPathIgnored;

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

try {
({ lint, api, report, threads } = linter(
({ stylelint, lint, isPathIgnored, report, threads } = linter(
this.key,
options,
compilation
Expand All @@ -127,7 +130,7 @@ class StylelintWebpackPlugin {
: globby.sync(wanted, { dot: true, ignore: exclude })
).map(async (file) => {
try {
return (await api.isPathIgnored(file)) ? false : file;
return (await isPathIgnored(stylelint, file)) ? false : file;
} catch (e) {
return file;
}
Expand Down
13 changes: 7 additions & 6 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const { arrify } = require('./utils');
/** @typedef {import('stylelint')} Stylelint */
/** @typedef {import('stylelint').LintResult} LintResult */
/** @typedef {import('stylelint').LinterResult} LinterResult */
/** @typedef {import('stylelint').InternalApi} InternalApi */
/** @typedef {import('stylelint').Formatter} Formatter */
/** @typedef {import('stylelint').FormatterType} FormatterType */
/** @typedef {import('webpack').Compiler} Compiler */
/** @typedef {import('webpack').Compilation} Compilation */
/** @typedef {import('./options').Options} Options */
/** @typedef {import('./getStylelint').isPathIgnored} isPathIgnored */
/** @typedef {(compilation: Compilation) => Promise<void>} GenerateReport */
/** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport}} Report */
/** @typedef {() => Promise<Report>} Reporter */
Expand All @@ -26,14 +26,14 @@ const resultStorage = new WeakMap();
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
*/
function linter(key, options, compilation) {
/** @type {Stylelint} */
let stylelint;

/** @type {InternalApi} */
let api;
/** @type {isPathIgnored} */
let isPathIgnored;

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

try {
({ stylelint, api, lintFiles, cleanup, threads } = getStylelint(
({ stylelint, isPathIgnored, lintFiles, cleanup, threads } = getStylelint(
key,
options
));
Expand All @@ -59,8 +59,9 @@ function linter(key, options, compilation) {
}

return {
stylelint,
lint,
api,
isPathIgnored,
report,
threads,
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/stylelint-path/ignore.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
display: block;
}
2 changes: 2 additions & 0 deletions test/fixtures/stylelint-path/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('file-loader!./test.scss');
require('file-loader!./ignore.scss');
3 changes: 3 additions & 0 deletions test/fixtures/stylelint-path/test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
display: block;
}
8 changes: 0 additions & 8 deletions test/mock/stylelint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,4 @@ module.exports = {
],
};
},

createLinter() {
return {
isPathIgnored() {
return false;
},
};
},
};
12 changes: 12 additions & 0 deletions test/mock/stylelint/lib/isPathIgnored.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {import('stylelint')} stylelint
* @param {string} filePath
* @returns {Promise<boolean>}
*/
function isPathIgnored(stylelint, filePath) {
return new Promise((resolve) => {
resolve(filePath.endsWith('ignore.scss'));
});
}

module.exports = isPathIgnored;
2 changes: 1 addition & 1 deletion test/stylelint-ignore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pack from './utils/pack';

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

compiler.run((err, stats) => {
expect(stats.hasWarnings()).toBe(false);
Expand Down
9 changes: 4 additions & 5 deletions test/stylelint-lint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ describe('stylelint lint', () => {
jest.mock('stylelint', () => {
return {
lint: mockLintFiles,
createLinter: () => {
return {
isPathIgnored: () => false,
};
},
};
});

jest.mock('stylelint/lib/isPathIgnored', () => {
throw new Error();
});
});

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/stylelint-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pack from './utils/pack';
describe('stylelint path', () => {
it('should use another instance of stylelint via stylelintPath config', (done) => {
const stylelintPath = join(__dirname, 'mock/stylelint');
const compiler = pack('good', { stylelintPath });
const compiler = pack('stylelint-path', { stylelintPath });

compiler.run((err, stats) => {
expect(err).toBeNull();
Expand Down
7 changes: 6 additions & 1 deletion types/getStylelint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare namespace getStylelint {
Stylelint,
LintResult,
Options,
isPathIgnored,
AsyncTask,
LintTask,
Linter,
Expand All @@ -22,8 +23,8 @@ declare namespace getStylelint {
}
type Options = import('./options').Options;
type Linter = {
api: import('stylelint').InternalApi;
stylelint: Stylelint;
isPathIgnored: isPathIgnored;
lintFiles: LintTask;
cleanup: AsyncTask;
threads: number;
Expand Down Expand Up @@ -87,6 +88,10 @@ type Stylelint = import('postcss').PluginCreator<
};
};
type LintResult = import('stylelint').LintResult;
type isPathIgnored = (
stylelint: Stylelint,
filePath: string
) => Promise<boolean>;
type AsyncTask = () => Promise<void>;
type LintTask = (files: string | string[]) => Promise<LintResult[]>;
type Worker = JestWorker & {
Expand Down
14 changes: 8 additions & 6 deletions types/linter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ export = linter;
* @param {string|undefined} key
* @param {Options} options
* @param {Compilation} compilation
* @returns {{api: InternalApi, lint: Linter, report: Reporter, threads: number}}
* @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
*/
declare function linter(
key: string | undefined,
options: Options,
compilation: Compilation
): {
api: InternalApi;
stylelint: Stylelint;
isPathIgnored: getStylelint.isPathIgnored;
lint: Linter;
report: Reporter;
threads: number;
Expand All @@ -21,12 +22,12 @@ declare namespace linter {
Stylelint,
LintResult,
LinterResult,
InternalApi,
Formatter,
FormatterType,
Compiler,
Compilation,
Options,
isPathIgnored,
GenerateReport,
Report,
Reporter,
Expand All @@ -36,9 +37,6 @@ declare namespace linter {
}
type Options = import('./options').Options;
type Compilation = import('webpack').Compilation;
type InternalApi = import('stylelint').InternalApi;
type Linter = (files: string | string[]) => void;
type Reporter = () => Promise<Report>;
type Stylelint = import('postcss').PluginCreator<
import('stylelint').PostcssPluginOptions
> & {
Expand Down Expand Up @@ -97,11 +95,15 @@ type Stylelint = import('postcss').PluginCreator<
longhandSubPropertiesOfShorthandProperties: import('stylelint').LonghandSubPropertiesOfShorthandProperties;
};
};
type Linter = (files: string | string[]) => void;
type Reporter = () => Promise<Report>;
import getStylelint = require('./getStylelint');
type LintResult = import('stylelint').LintResult;
type LinterResult = import('stylelint').LinterResult;
type Formatter = import('stylelint').Formatter;
type FormatterType = import('stylelint').FormatterType;
type Compiler = import('webpack').Compiler;
type isPathIgnored = import('./getStylelint').isPathIgnored;
type GenerateReport = (compilation: Compilation) => Promise<void>;
type Report = {
errors?: StylelintError;
Expand Down

0 comments on commit 805b54c

Please sign in to comment.