@@ -5,14 +5,15 @@ const arrify = require('arrify');
55const StylelintError = require ( './StylelintError' ) ;
66const getStylelint = require ( './getStylelint' ) ;
77
8- /** @typedef {import('stylelint') } Stylelint */
9- /** @typedef {import('stylelint').LintResult } LintResult */
10- /** @typedef {import('stylelint').InternalApi } InternalApi */
118/** @typedef {import('webpack').Compiler } Compiler */
129/** @typedef {import('webpack').Compilation } Compilation */
10+ /** @typedef {import('./getStylelint').Stylelint } Stylelint */
11+ /** @typedef {import('./getStylelint').LintResult } LintResult */
12+ /** @typedef {import('./getStylelint').LinterResult } LinterResult */
13+ /** @typedef {import('./getStylelint').Formatter } Formatter */
14+ /** @typedef {import('./getStylelint').FormatterType } FormatterType */
15+ /** @typedef {import('./getStylelint').isPathIgnored } isPathIgnored */
1316/** @typedef {import('./options').Options } Options */
14- /** @typedef {import('./options').FormatterType } FormatterType */
15- /** @typedef {((results: LintResult[]) => string) } FormatterFunction */
1617/** @typedef {(compilation: Compilation) => Promise<void> } GenerateReport */
1718/** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport} } Report */
1819/** @typedef {() => Promise<Report> } Reporter */
@@ -26,14 +27,14 @@ const resultStorage = new WeakMap();
2627 * @param {string|undefined } key
2728 * @param {Options } options
2829 * @param {Compilation } compilation
29- * @returns {{api: InternalApi , lint: Linter, report: Reporter, threads: number} }
30+ * @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored , lint: Linter, report: Reporter, threads: number} }
3031 */
3132function linter ( key , options , compilation ) {
3233 /** @type {Stylelint } */
3334 let stylelint ;
3435
35- /** @type {InternalApi } */
36- let api ;
36+ /** @type {isPathIgnored } */
37+ let isPathIgnored ;
3738
3839 /** @type {(files: string|string[]) => Promise<LintResult[]> } */
3940 let lintFiles ;
@@ -50,7 +51,7 @@ function linter(key, options, compilation) {
5051 const crossRunResultStorage = getResultStorage ( compilation ) ;
5152
5253 try {
53- ( { stylelint, api , lintFiles, cleanup, threads } = getStylelint (
54+ ( { stylelint, isPathIgnored , lintFiles, cleanup, threads } = getStylelint (
5455 key ,
5556 options
5657 ) ) ;
@@ -59,8 +60,9 @@ function linter(key, options, compilation) {
5960 }
6061
6162 return {
63+ stylelint,
6264 lint,
63- api ,
65+ isPathIgnored ,
6466 report,
6567 threads,
6668 } ;
@@ -102,9 +104,22 @@ function linter(key, options, compilation) {
102104 }
103105
104106 const formatter = loadFormatter ( stylelint , options . formatter ) ;
107+
108+ /** @type {LinterResult } */
109+ const returnValue = {
110+ // @ts -ignore
111+ cwd : options . cwd ,
112+ errored : false ,
113+ results : [ ] ,
114+ output : '' ,
115+ reportedDisables : [ ] ,
116+ ruleMetadata : getRuleMetadata ( results ) ,
117+ } ;
118+
105119 const { errors, warnings } = formatResults (
106120 formatter ,
107- parseResults ( options , results )
121+ parseResults ( options , results ) ,
122+ returnValue
108123 ) ;
109124
110125 return {
@@ -146,42 +161,42 @@ function linter(key, options, compilation) {
146161 return ;
147162 }
148163
149- const content = outputReport . formatter
150- ? loadFormatter ( stylelint , outputReport . formatter ) ( results )
151- : formatter ( results ) ;
164+ const content = outputReport . formatter ;
165+ loadFormatter ( stylelint , outputReport . formatter ) ( results , returnValue ) ;
166+ formatter ( results , returnValue ) ;
152167
153168 let { filePath } = outputReport ;
154169 if ( ! isAbsolute ( filePath ) ) {
155170 filePath = join ( compiler . outputPath , filePath ) ;
156171 }
157172
158- await save ( filePath , content ) ;
173+ await save ( filePath , String ( content ) ) ;
159174 }
160175 }
161176}
162177
163178/**
164- * @param {FormatterFunction } formatter
179+ * @param {Formatter } formatter
165180 * @param {{ errors: LintResult[]; warnings: LintResult[]; } } results
181+ * @param {LinterResult } returnValue
166182 * @returns {{errors?: StylelintError, warnings?: StylelintError} }
167183 */
168- function formatResults ( formatter , results ) {
184+ function formatResults ( formatter , results , returnValue ) {
169185 let errors ;
170186 let warnings ;
171187 if ( results . warnings . length > 0 ) {
172- warnings = new StylelintError ( formatter ( results . warnings ) ) ;
188+ warnings = new StylelintError ( formatter ( results . warnings , returnValue ) ) ;
173189 }
174190
175191 if ( results . errors . length > 0 ) {
176- errors = new StylelintError ( formatter ( results . errors ) ) ;
192+ errors = new StylelintError ( formatter ( results . errors , returnValue ) ) ;
177193 }
178194
179195 return {
180196 errors,
181197 warnings,
182198 } ;
183199}
184-
185200/**
186201 * @param {Options } options
187202 * @param {LintResult[] } results
@@ -227,7 +242,7 @@ function parseResults(options, results) {
227242/**
228243 * @param {Stylelint } stylelint
229244 * @param {FormatterType= } formatter
230- * @returns {FormatterFunction }
245+ * @returns {Formatter }
231246 */
232247function loadFormatter ( stylelint , formatter ) {
233248 if ( typeof formatter === 'function' ) {
@@ -278,4 +293,21 @@ function getResultStorage({ compiler }) {
278293 return storage ;
279294}
280295
296+ /**
297+ * @param {LintResult[] } lintResults
298+ */
299+ /* istanbul ignore next */
300+ function getRuleMetadata ( lintResults ) {
301+ const [ lintResult ] = lintResults ;
302+
303+ // eslint-disable-next-line no-undefined
304+ if ( lintResult === undefined ) return { } ;
305+
306+ // eslint-disable-next-line no-underscore-dangle, no-undefined
307+ if ( lintResult . _postcssResult === undefined ) return { } ;
308+
309+ // eslint-disable-next-line no-underscore-dangle
310+ return lintResult . _postcssResult . stylelint . ruleMetadata ;
311+ }
312+
281313module . exports = linter ;
0 commit comments