stylelint-webpack-plugin 2.3.2 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/linter.js CHANGED
@@ -1,155 +1,149 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = linter;
7
-
8
- var _path = require("path");
9
-
10
- var _arrify = _interopRequireDefault(require("arrify"));
11
-
12
- var _StylelintError = _interopRequireDefault(require("./StylelintError"));
13
-
14
- var _getStylelint = _interopRequireDefault(require("./getStylelint"));
15
-
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
- // @ts-ignore
19
-
20
- /** @typedef {import('stylelint')} Stylelint */
21
-
22
- /** @typedef {import('stylelint').LintResult} LintResult */
3
+ const {
4
+ dirname,
5
+ isAbsolute,
6
+ join
7
+ } = require('path');
8
+ const arrify = require('arrify');
9
+ const StylelintError = require('./StylelintError');
10
+ const getStylelint = require('./getStylelint');
23
11
 
24
12
  /** @typedef {import('webpack').Compiler} Compiler */
25
-
26
13
  /** @typedef {import('webpack').Compilation} Compilation */
27
-
14
+ /** @typedef {import('./getStylelint').Stylelint} Stylelint */
15
+ /** @typedef {import('./getStylelint').LintResult} LintResult */
16
+ /** @typedef {import('./getStylelint').LinterResult} LinterResult */
17
+ /** @typedef {import('./getStylelint').Formatter} Formatter */
18
+ /** @typedef {import('./getStylelint').FormatterType} FormatterType */
19
+ /** @typedef {import('./getStylelint').isPathIgnored} isPathIgnored */
28
20
  /** @typedef {import('./options').Options} Options */
29
-
30
- /** @typedef {import('./options').FormatterType} FormatterType */
31
-
32
- /** @typedef {((results: LintResult[]) => string)} FormatterFunction */
33
-
34
21
  /** @typedef {(compilation: Compilation) => Promise<void>} GenerateReport */
35
-
36
22
  /** @typedef {{errors?: StylelintError, warnings?: StylelintError, generateReportAsset?: GenerateReport}} Report */
37
-
38
23
  /** @typedef {() => Promise<Report>} Reporter */
39
-
40
24
  /** @typedef {(files: string|string[]) => void} Linter */
41
-
42
25
  /** @typedef {{[files: string]: LintResult}} LintResultMap */
43
26
 
44
27
  /** @type {WeakMap<Compiler, LintResultMap>} */
45
28
  const resultStorage = new WeakMap();
46
- /**
47
- * @param {string|undefined} key
48
- * @param {Options} options
49
- * @param {Compilation} compilation
50
- * @returns {{lint: Linter, report: Reporter, threads: number}}
51
- */
52
29
 
30
+ /**
31
+ * @param {string|undefined} key
32
+ * @param {Options} options
33
+ * @param {Compilation} compilation
34
+ * @returns {{stylelint: Stylelint, isPathIgnored: isPathIgnored, lint: Linter, report: Reporter, threads: number}}
35
+ */
53
36
  function linter(key, options, compilation) {
54
37
  /** @type {Stylelint} */
55
38
  let stylelint;
56
- /** @type {(files: string|string[]) => Promise<LintResult[]>} */
57
39
 
40
+ /** @type {isPathIgnored} */
41
+ let isPathIgnored;
42
+
43
+ /** @type {(files: string|string[]) => Promise<LintResult[]>} */
58
44
  let lintFiles;
59
- /** @type {() => Promise<void>} */
60
45
 
46
+ /** @type {() => Promise<void>} */
61
47
  let cleanup;
62
- /** @type number */
63
48
 
49
+ /** @type number */
64
50
  let threads;
65
- /** @type {Promise<LintResult[]>[]} */
66
51
 
52
+ /** @type {Promise<LintResult[]>[]} */
67
53
  const rawResults = [];
68
54
  const crossRunResultStorage = getResultStorage(compilation);
69
-
70
55
  try {
71
56
  ({
72
57
  stylelint,
58
+ isPathIgnored,
73
59
  lintFiles,
74
60
  cleanup,
75
61
  threads
76
- } = (0, _getStylelint.default)(key, options));
62
+ } = getStylelint(key, options));
77
63
  } catch (e) {
78
- throw new _StylelintError.default(e.message);
64
+ throw new StylelintError(e.message);
79
65
  }
80
-
81
66
  return {
67
+ stylelint,
82
68
  lint,
69
+ isPathIgnored,
83
70
  report,
84
71
  threads
85
72
  };
86
- /**
87
- * @param {string | string[]} files
88
- */
89
73
 
74
+ /**
75
+ * @param {string | string[]} files
76
+ */
90
77
  function lint(files) {
91
- for (const file of (0, _arrify.default)(files)) {
78
+ for (const file of arrify(files)) {
92
79
  delete crossRunResultStorage[file];
93
80
  }
94
-
95
81
  rawResults.push(lintFiles(files).catch(e => {
96
- compilation.errors.push(e);
82
+ // @ts-ignore
83
+ compilation.errors.push(new StylelintError(e.message));
97
84
  return [];
98
85
  }));
99
86
  }
100
-
101
87
  async function report() {
102
88
  // Filter out ignored files.
103
- let results = removeIgnoredWarnings( // Get the current results, resetting the rawResults to empty
89
+ let results = removeIgnoredWarnings(
90
+ // Get the current results, resetting the rawResults to empty
104
91
  await flatten(rawResults.splice(0, rawResults.length)));
105
92
  await cleanup();
106
-
107
93
  for (const result of results) {
108
94
  crossRunResultStorage[String(result.source)] = result;
109
95
  }
96
+ results = Object.values(crossRunResultStorage);
110
97
 
111
- results = Object.values(crossRunResultStorage); // do not analyze if there are no results or stylelint config
112
-
98
+ // do not analyze if there are no results or stylelint config
113
99
  if (!results || results.length < 1) {
114
100
  return {};
115
101
  }
116
-
117
102
  const formatter = loadFormatter(stylelint, options.formatter);
103
+
104
+ /** @type {LinterResult} */
105
+ const returnValue = {
106
+ // @ts-ignore
107
+ cwd: options.cwd,
108
+ errored: false,
109
+ results: [],
110
+ output: '',
111
+ reportedDisables: [],
112
+ ruleMetadata: getRuleMetadata(results)
113
+ };
118
114
  const {
119
115
  errors,
120
116
  warnings
121
- } = formatResults(formatter, parseResults(options, results));
117
+ } = formatResults(formatter, parseResults(options, results), returnValue);
122
118
  return {
123
119
  errors,
124
120
  warnings,
125
121
  generateReportAsset
126
122
  };
127
- /**
128
- * @param {Compilation} compilation
129
- * @returns {Promise<void>}
130
- */
131
123
 
124
+ /**
125
+ * @param {Compilation} compilation
126
+ * @returns {Promise<void>}
127
+ */
132
128
  async function generateReportAsset({
133
129
  compiler
134
130
  }) {
135
131
  const {
136
132
  outputReport
137
133
  } = options;
138
- /**
139
- * @param {string} name
140
- * @param {string | Buffer} content
134
+ /**
135
+ * @param {string} name
136
+ * @param {string | Buffer} content
141
137
  */
142
-
143
- const save = (name, content) =>
144
- /** @type {Promise<void>} */
138
+ const save = (name, content) => /** @type {Promise<void>} */
145
139
  new Promise((finish, bail) => {
146
140
  const {
147
141
  mkdir,
148
142
  writeFile
149
- } = compiler.outputFileSystem; // ensure directory exists
143
+ } = compiler.outputFileSystem;
144
+ // ensure directory exists
150
145
  // @ts-ignore - the types for `outputFileSystem` are missing the 3 arg overload
151
-
152
- mkdir((0, _path.dirname)(name), {
146
+ mkdir(dirname(name), {
153
147
  recursive: true
154
148
  }, err => {
155
149
  /* istanbul ignore if */
@@ -159,74 +153,66 @@ function linter(key, options, compilation) {
159
153
  });
160
154
  });
161
155
  });
162
-
163
156
  if (!outputReport || !outputReport.filePath) {
164
157
  return;
165
158
  }
166
-
167
- const content = outputReport.formatter ? loadFormatter(stylelint, outputReport.formatter)(results) : formatter(results);
159
+ const content = outputReport.formatter;
160
+ loadFormatter(stylelint, outputReport.formatter)(results, returnValue);
161
+ formatter(results, returnValue);
168
162
  let {
169
163
  filePath
170
164
  } = outputReport;
171
-
172
- if (!(0, _path.isAbsolute)(filePath)) {
173
- filePath = (0, _path.join)(compiler.outputPath, filePath);
165
+ if (!isAbsolute(filePath)) {
166
+ filePath = join(compiler.outputPath, filePath);
174
167
  }
175
-
176
- await save(filePath, content);
168
+ await save(filePath, String(content));
177
169
  }
178
170
  }
179
171
  }
180
- /**
181
- * @param {FormatterFunction} formatter
182
- * @param {{ errors: LintResult[]; warnings: LintResult[]; }} results
183
- * @returns {{errors?: StylelintError, warnings?: StylelintError}}
184
- */
185
172
 
186
-
187
- function formatResults(formatter, results) {
173
+ /**
174
+ * @param {Formatter} formatter
175
+ * @param {{ errors: LintResult[]; warnings: LintResult[]; }} results
176
+ * @param {LinterResult} returnValue
177
+ * @returns {{errors?: StylelintError, warnings?: StylelintError}}
178
+ */
179
+ function formatResults(formatter, results, returnValue) {
188
180
  let errors;
189
181
  let warnings;
190
-
191
182
  if (results.warnings.length > 0) {
192
- warnings = new _StylelintError.default(formatter(results.warnings));
183
+ warnings = new StylelintError(formatter(results.warnings, returnValue));
193
184
  }
194
-
195
185
  if (results.errors.length > 0) {
196
- errors = new _StylelintError.default(formatter(results.errors));
186
+ errors = new StylelintError(formatter(results.errors, returnValue));
197
187
  }
198
-
199
188
  return {
200
189
  errors,
201
190
  warnings
202
191
  };
203
192
  }
204
- /**
205
- * @param {Options} options
206
- * @param {LintResult[]} results
207
- * @returns {{errors: LintResult[], warnings: LintResult[]}}
193
+ /**
194
+ * @param {Options} options
195
+ * @param {LintResult[]} results
196
+ * @returns {{errors: LintResult[], warnings: LintResult[]}}
208
197
  */
209
-
210
-
211
198
  function parseResults(options, results) {
212
199
  /** @type {LintResult[]} */
213
200
  const errors = [];
214
- /** @type {LintResult[]} */
215
201
 
202
+ /** @type {LintResult[]} */
216
203
  const warnings = [];
217
204
  results.forEach(file => {
218
205
  const fileErrors = file.warnings.filter(message => options.emitError && message.severity === 'error');
219
-
220
206
  if (fileErrors.length > 0) {
221
- errors.push({ ...file,
207
+ errors.push({
208
+ ...file,
222
209
  warnings: fileErrors
223
210
  });
224
211
  }
225
-
226
212
  const fileWarnings = file.warnings.filter(message => options.emitWarning && message.severity === 'warning');
227
-
228
213
  if (fileWarnings.length > 0) {
229
- warnings.push({ ...file,
214
+ warnings.push({
215
+ ...file,
230
216
  warnings: fileWarnings
231
217
  });
232
218
  }
@@ -236,65 +222,75 @@ function parseResults(options, results) {
236
222
  warnings
237
223
  };
238
224
  }
239
- /**
240
- * @param {Stylelint} stylelint
241
- * @param {FormatterType=} formatter
242
- * @returns {FormatterFunction}
243
- */
244
-
245
225
 
226
+ /**
227
+ * @param {Stylelint} stylelint
228
+ * @param {FormatterType=} formatter
229
+ * @returns {Formatter}
230
+ */
246
231
  function loadFormatter(stylelint, formatter) {
247
232
  if (typeof formatter === 'function') {
248
233
  return formatter;
249
234
  }
250
-
251
235
  if (typeof formatter === 'string') {
252
236
  try {
253
237
  return stylelint.formatters[formatter];
254
- } catch (_) {// Load the default formatter.
238
+ } catch (_) {
239
+ // Load the default formatter.
255
240
  }
256
241
  }
257
-
258
242
  return stylelint.formatters.string;
259
243
  }
260
- /**
261
- * @param {LintResult[]} results
262
- * @returns {LintResult[]}
263
- */
264
-
265
244
 
245
+ /**
246
+ * @param {LintResult[]} results
247
+ * @returns {LintResult[]}
248
+ */
266
249
  function removeIgnoredWarnings(results) {
267
250
  return results.filter(result => !result.ignored);
268
251
  }
269
- /**
270
- * @param {Promise<LintResult[]>[]} results
271
- * @returns {Promise<LintResult[]>}
272
- */
273
-
274
252
 
253
+ /**
254
+ * @param {Promise<LintResult[]>[]} results
255
+ * @returns {Promise<LintResult[]>}
256
+ */
275
257
  async function flatten(results) {
276
- /**
277
- * @param {LintResult[]} acc
278
- * @param {LintResult[]} list
258
+ /**
259
+ * @param {LintResult[]} acc
260
+ * @param {LintResult[]} list
279
261
  */
280
262
  const flat = (acc, list) => [...acc, ...list];
281
-
282
263
  return (await Promise.all(results)).reduce(flat, []);
283
264
  }
284
- /**
285
- * @param {Compilation} compilation
286
- * @returns {LintResultMap}
287
- */
288
-
289
265
 
266
+ /**
267
+ * @param {Compilation} compilation
268
+ * @returns {LintResultMap}
269
+ */
290
270
  function getResultStorage({
291
271
  compiler
292
272
  }) {
293
273
  let storage = resultStorage.get(compiler);
294
-
295
274
  if (!storage) {
296
275
  resultStorage.set(compiler, storage = {});
297
276
  }
298
-
299
277
  return storage;
300
- }
278
+ }
279
+
280
+ /**
281
+ * @param {LintResult[]} lintResults
282
+ */
283
+ /* istanbul ignore next */
284
+ function getRuleMetadata(lintResults) {
285
+ const [lintResult] = lintResults;
286
+
287
+ // eslint-disable-next-line no-undefined
288
+ if (lintResult === undefined) return {};
289
+
290
+ // eslint-disable-next-line no-underscore-dangle, no-undefined
291
+ if (lintResult._postcssResult === undefined) return {};
292
+
293
+ // eslint-disable-next-line no-underscore-dangle
294
+ return lintResult._postcssResult.stylelint.ruleMetadata;
295
+ }
296
+ module.exports = linter;
package/dist/options.js CHANGED
@@ -1,54 +1,44 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getOptions = getOptions;
7
- exports.getStylelintOptions = getStylelintOptions;
8
-
9
- var _schemaUtils = require("schema-utils");
10
-
11
- var _options = _interopRequireDefault(require("./options.json"));
12
-
13
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3
+ const {
4
+ validate
5
+ } = require('schema-utils');
14
6
 
15
7
  // @ts-ignore
8
+ const schema = require('./options.json');
16
9
 
17
- /** @typedef {import("stylelint")} stylelint */
10
+ /** @typedef {import('./getStylelint').LinterOptions} StylelintOptions */
11
+ /** @typedef {import('./getStylelint').FormatterType} FormatterType */
18
12
 
19
- /** @typedef {import("stylelint").LinterOptions} StylelintOptions */
20
-
21
- /** @typedef {import("stylelint").FormatterType} FormatterType */
22
-
23
- /**
24
- * @typedef {Object} OutputReport
25
- * @property {string=} filePath
26
- * @property {FormatterType=} formatter
13
+ /**
14
+ * @typedef {Object} OutputReport
15
+ * @property {string=} filePath
16
+ * @property {FormatterType=} formatter
27
17
  */
28
18
 
29
- /**
30
- * @typedef {Object} PluginOptions
31
- * @property {string} context
32
- * @property {boolean} emitError
33
- * @property {boolean} emitWarning
34
- * @property {string|string[]=} exclude
35
- * @property {string|string[]} extensions
36
- * @property {boolean} failOnError
37
- * @property {boolean} failOnWarning
38
- * @property {string|string[]} files
39
- * @property {FormatterType} formatter
40
- * @property {boolean} lintDirtyModulesOnly
41
- * @property {boolean} quiet
42
- * @property {string} stylelintPath
43
- * @property {OutputReport} outputReport
44
- * @property {number|boolean=} threads
19
+ /**
20
+ * @typedef {Object} PluginOptions
21
+ * @property {string} context
22
+ * @property {boolean} emitError
23
+ * @property {boolean} emitWarning
24
+ * @property {string|string[]=} exclude
25
+ * @property {string|string[]} extensions
26
+ * @property {boolean} failOnError
27
+ * @property {boolean} failOnWarning
28
+ * @property {string|string[]} files
29
+ * @property {FormatterType} formatter
30
+ * @property {boolean} lintDirtyModulesOnly
31
+ * @property {boolean} quiet
32
+ * @property {string} stylelintPath
33
+ * @property {OutputReport} outputReport
34
+ * @property {number|boolean=} threads
45
35
  */
46
36
 
47
37
  /** @typedef {Partial<PluginOptions & StylelintOptions>} Options */
48
38
 
49
- /**
50
- * @param {Options} pluginOptions
51
- * @returns {Partial<PluginOptions>}
39
+ /**
40
+ * @param {Options} pluginOptions
41
+ * @returns {Partial<PluginOptions>}
52
42
  */
53
43
  function getOptions(pluginOptions) {
54
44
  const options = {
@@ -61,35 +51,41 @@ function getOptions(pluginOptions) {
61
51
  emitError: true,
62
52
  emitWarning: false
63
53
  } : {})
64
- }; // @ts-ignore
54
+ };
65
55
 
66
- (0, _schemaUtils.validate)(_options.default, options, {
56
+ // @ts-ignore
57
+ validate(schema, options, {
67
58
  name: 'Stylelint Webpack Plugin',
68
59
  baseDataPath: 'options'
69
60
  });
70
61
  return options;
71
62
  }
72
- /**
73
- * @param {Options} pluginOptions
74
- * @returns {Partial<StylelintOptions>}
75
- */
76
-
77
63
 
64
+ /**
65
+ * @param {Options} pluginOptions
66
+ * @returns {Partial<StylelintOptions>}
67
+ */
78
68
  function getStylelintOptions(pluginOptions) {
79
- const stylelintOptions = { ...pluginOptions
80
- }; // Keep the files and formatter option because it is common to both the plugin and Stylelint.
69
+ const stylelintOptions = {
70
+ ...pluginOptions
71
+ };
81
72
 
73
+ // Keep the files and formatter option because it is common to both the plugin and Stylelint.
82
74
  const {
83
75
  files,
84
76
  formatter,
85
77
  ...stylelintOnlyOptions
86
- } = _options.default.properties; // No need to guard the for-in because schema.properties has hardcoded keys.
87
- // eslint-disable-next-line guard-for-in
78
+ } = schema.properties;
88
79
 
80
+ // No need to guard the for-in because schema.properties has hardcoded keys.
81
+ // eslint-disable-next-line guard-for-in
89
82
  for (const option in stylelintOnlyOptions) {
90
83
  // @ts-ignore
91
84
  delete stylelintOptions[option];
92
85
  }
93
-
94
86
  return stylelintOptions;
95
- }
87
+ }
88
+ module.exports = {
89
+ getOptions,
90
+ getStylelintOptions
91
+ };