gulp-eslint-new 1.2.0 → 1.3.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/README.md CHANGED
@@ -208,11 +208,11 @@ gulp.src(['**/*.js', '!node_modules/**'])
208
208
  }));
209
209
  ```
210
210
 
211
- Type: `(result: Object, callback: Function) => void`
211
+ Param Type: `(result: Object, callback: Function) => void`
212
212
 
213
213
  Call an asynchronous, Node-style callback-based function for each ESLint file result. The callback must be called for the stream to finish. If an error is passed to the callback, it will be wrapped in a gulp `PluginError` and emitted from the stream.
214
214
 
215
- Type: `(result: Object) => Promise<void>`
215
+ Param Type: `(result: Object) => Promise<void>`
216
216
 
217
217
  Call an asynchronous, promise-based function for each ESLint file result. If the promise is rejected, the rejection reason will be wrapped in a gulp `PluginError` and emitted from the stream.
218
218
 
@@ -292,16 +292,18 @@ gulp.src(['**/*.js', '!node_modules/**'])
292
292
  .pipe(gulpESLintNew.failAfterError());
293
293
  ```
294
294
 
295
- ### `gulpESLintNew.format(formatter, output)`
295
+ ### `gulpESLintNew.format(formatter, writer)`
296
296
 
297
297
  Format all linted files once.
298
298
  This should be used in the stream after piping through `gulpESLintNew`; otherwise, this will find no ESLint results to format.
299
299
 
300
- The `formatter` argument may be a `string`, `Function`, or `undefined`.
301
- As a `string`, a formatter module by that name or path will be resolved.
302
- The resolved formatter will be either one of the [built-in ESLint formatters](https://eslint.org/docs/user-guide/formatters/#eslint-formatters), or a formatter exported by a module with the specied path (located relative to the ESLint working directory), or a formatter exported by a package installed as a dependency (the prefix "eslint-formatter-" in the package name can be omitted).
303
- If `undefined`, the ESLint "stylish" formatter will be resolved.
304
- A `Function` will be called with an `Array` of file linting results to format.
300
+ `formatter` param type: `string | Object | Function | undefined`
301
+
302
+ The `formatter` argument determines the [ESLint formatter](https://eslint.org/docs/user-guide/formatters/) used to format linting results.
303
+ If a `string` is provided, a formatter module by that name or path will be resolved.
304
+ The resolved formatter will be either one of the built-in ESLint formatters, or a formatter exported by a module with the specified path (located relative to the ESLint working directory), or a formatter exported by a package installed as a dependency (the prefix "eslint-formatter-" in the package name can be omitted).
305
+ Instead of providing a string, it is also possible to specify a formatter object as resolved by the ESLint method [`loadFormatter`](https://eslint.org/docs/developer-guide/nodejs-api#-eslintloadformatternameorpath), or a formatter function directly.
306
+ If `undefined` is specified, the ESLint "stylish" formatter will be used.
305
307
 
306
308
  ```javascript
307
309
  // Use the default "stylish" ESLint formatter.
@@ -315,9 +317,12 @@ gulpESLintNew.format('checkstyle')
315
317
  gulpESLintNew.format('pretty')
316
318
  ```
317
319
 
318
- The `output` argument may be a writable stream, `Function`, or `undefined`. As a writable stream, the formatter results will be written to the stream.
319
- If `undefined`, the formatter results will be written to [gulp's log](https://github.com/gulpjs/fancy-log#logmsg).
320
- A `Function` will be called with the formatter results as the only parameter.
320
+ `writer` param type: `NodeJS.WritableStream | Function | undefined`
321
+
322
+ The `writer` argument may be a writable stream, `Function`, or `undefined`.
323
+ As a writable stream, the formatter output will be written to the stream.
324
+ If `undefined`, the formatter output will be written to [gulp's log](https://github.com/gulpjs/fancy-log#logmsg).
325
+ A `Function` will be called with the formatter output as the only parameter.
321
326
 
322
327
  ```javascript
323
328
  // write to gulp's log (default)
@@ -327,7 +332,7 @@ gulpESLintNew.format()
327
332
  gulpESLintNew.format('junit', process.stdout)
328
333
  ```
329
334
 
330
- ### `gulpESLintNew.formatEach(formatter, output)`
335
+ ### `gulpESLintNew.formatEach(formatter, writer)`
331
336
 
332
337
  Format each linted file individually.
333
338
  This should be used in the stream after piping through `gulpESLintNew`; otherwise, this will find no ESLint results to format.
@@ -338,7 +343,7 @@ The arguments for `formatEach` are the same as the arguments for `format`.
338
343
 
339
344
  Overwrite files with the fixed content provided by ESLint.
340
345
  This should be used in conjunction with the option `fix` in [`gulpESLintNew(options)`](#gulpeslintnewoptions).
341
- Files without a fixed content will be ignored.
346
+ Files without a fix and files that were not processed by ESLint will be left untouched.
342
347
 
343
348
  ```javascript
344
349
  gulp.src(['**/*.js', '!node_modules/**'])
package/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import 'node';
3
3
  import { TransformCallback } from 'stream';
4
4
 
5
5
  export type GulpESLintAction<Type>
6
- = ((value: Type, callback: TransformCallback) => void) | ((value: Type) => Promise<void>);
6
+ = ((value: Type, callback: TransformCallback) => unknown) | ((value: Type) => Promise<unknown>);
7
7
 
8
8
  export type GulpESLintOptions =
9
9
  Omit<
@@ -60,7 +60,9 @@ export type GulpESLintResults =
60
60
  fixableWarningCount: number;
61
61
  };
62
62
 
63
- declare const gulpESLint: {
63
+ export type GulpESLintWriter = (str: string) => unknown | Promise<unknown>;
64
+
65
+ declare const gulpESLintNew: {
64
66
  /**
65
67
  * Append ESLint result to each file.
66
68
  *
@@ -111,32 +113,32 @@ declare const gulpESLint: {
111
113
  * Format the results of each file individually.
112
114
  *
113
115
  * @param formatter
114
- * The name or function for an ESLint result formatter.
116
+ * A name or path of a formatter, a formatter object or a formatter function.
115
117
  * Defaults to the [stylish](https://eslint.org/docs/user-guide/formatters/#stylish) formatter.
116
- * @param writable
118
+ * @param writer
117
119
  * A funtion or stream to write the formatted ESLint results.
118
120
  * Defaults to gulp's [fancy-log](https://github.com/gulpjs/fancy-log#readme).
119
121
  * @returns gulp file stream.
120
122
  */
121
123
  formatEach(
122
- formatter?: string | ESLint.Formatter['format'],
123
- writable?: ((str: string) => void) | NodeJS.WritableStream
124
+ formatter?: string | ESLint.Formatter | ESLint.Formatter['format'],
125
+ writer?: GulpESLintWriter | NodeJS.WritableStream
124
126
  ): NodeJS.ReadWriteStream;
125
127
 
126
128
  /**
127
129
  * Wait until all files have been linted and format all results at once.
128
130
  *
129
131
  * @param formatter
130
- * The name or function for an ESLint result formatter.
132
+ * A name or path of a formatter, a formatter object or a formatter function.
131
133
  * Defaults to the [stylish](https://eslint.org/docs/user-guide/formatters/#stylish) formatter.
132
- * @param writable
134
+ * @param writer
133
135
  * A funtion or stream to write the formatted ESLint results.
134
136
  * Defaults to gulp's [fancy-log](https://github.com/gulpjs/fancy-log#readme).
135
137
  * @returns gulp file stream.
136
138
  */
137
139
  format(
138
- formatter?: string | ESLint.Formatter['format'],
139
- writable?: ((str: string) => void) | NodeJS.WritableStream
140
+ formatter?: string | ESLint.Formatter | ESLint.Formatter['format'],
141
+ writer?: GulpESLintWriter | NodeJS.WritableStream
140
142
  ): NodeJS.ReadWriteStream;
141
143
  };
142
- export default gulpESLint;
144
+ export default gulpESLintNew;
package/index.js CHANGED
@@ -9,7 +9,7 @@ const {
9
9
  hasOwn,
10
10
  isErrorMessage,
11
11
  migrateOptions,
12
- resolveWritable,
12
+ resolveWriter,
13
13
  writeResults
14
14
  } = require('./util');
15
15
  const { ESLint } = require('eslint');
@@ -145,21 +145,21 @@ exports.failAfterError = () => exports.results(({ errorCount }) => {
145
145
  }
146
146
  });
147
147
 
148
- exports.formatEach = (formatter, writable) => {
149
- writable = resolveWritable(writable);
148
+ exports.formatEach = (formatter, writer) => {
149
+ writer = resolveWriter(writer);
150
150
  return createTransform(
151
151
  async file => {
152
152
  const { eslint } = file;
153
153
  if (eslint) {
154
154
  const eslintInfo = getESLintInfo(file);
155
- await writeResults([eslint], eslintInfo, formatter, writable);
155
+ await writeResults([eslint], eslintInfo, formatter, writer);
156
156
  }
157
157
  }
158
158
  );
159
159
  };
160
160
 
161
- exports.format = (formatter, writable) => {
162
- writable = resolveWritable(writable);
161
+ exports.format = (formatter, writer) => {
162
+ writer = resolveWriter(writer);
163
163
  const results = [];
164
164
  let commonInfo;
165
165
  return createTransform(
@@ -183,7 +183,7 @@ exports.format = (formatter, writable) => {
183
183
  },
184
184
  async () => {
185
185
  if (results.length) {
186
- await writeResults(results, commonInfo, formatter, writable);
186
+ await writeResults(results, commonInfo, formatter, writer);
187
187
  }
188
188
  }
189
189
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gulp-eslint-new",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "A gulp plugin to lint code with ESLint 8",
5
5
  "keywords": [
6
6
  "gulpplugin",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@types/eslint": "^8.4.1",
42
- "@types/node": "^17.0.13",
42
+ "@types/node": "^17.0.15",
43
43
  "eslint": "^8.8.0",
44
44
  "fancy-log": "^2.0.0",
45
45
  "plugin-error": "^1.0.1",
@@ -47,8 +47,8 @@
47
47
  "vinyl-fs": "^3.0.3"
48
48
  },
49
49
  "devDependencies": {
50
- "@typescript-eslint/eslint-plugin": "^5.10.0",
51
- "@typescript-eslint/parser": "^5.10.0",
50
+ "@typescript-eslint/eslint-plugin": "^5.10.2",
51
+ "@typescript-eslint/parser": "^5.10.2",
52
52
  "c8": "^7.11.0",
53
53
  "eslint-plugin-eslint-comments": "^3.2.0",
54
54
  "gulp": "^4.0.2",
package/util.js CHANGED
@@ -1,5 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ /**
4
+ * @typedef {import('eslint').ESLint} ESLint
5
+ * @typedef {import('eslint').ESLint.Formatter} ESLint.Formatter
6
+ * @typedef {import('eslint').ESLint.LintResult} ESLint.LintResult
7
+ * @typedef {ESLint.Formatter['format']} FormatterFunction
8
+ * @typedef {import('.').GulpESLintWriter} GulpESLintWriter
9
+ * @typedef {import('eslint').Linter} Linter
10
+ * @typedef {import('eslint').Linter.LintMessage} Linter.LintMessage
11
+ */
12
+
3
13
  const fancyLog = require('fancy-log');
4
14
  const { relative } = require('path');
5
15
  const PluginError = require('plugin-error');
@@ -41,6 +51,8 @@ function isErrorMessage({ severity }) {
41
51
  return severity > 1;
42
52
  }
43
53
 
54
+ const isObject = value => Object(value) === value;
55
+
44
56
  /**
45
57
  * Determine if a message is a warning.
46
58
  *
@@ -52,19 +64,22 @@ function isWarningMessage({ severity }) {
52
64
  }
53
65
 
54
66
  /**
55
- * Resolve formatter from string.
56
- * If a function is specified, it will be treated as an ESLint 6 style formatter function and
57
- * wrapped into an object appropriately.
67
+ * Resolve a formatter from a string.
68
+ * If a function is specified, it will be treated as a formatter function and wrapped in an object
69
+ * appropriately.
58
70
  *
59
71
  * @param {{ cwd: string, eslint: ESLint }} eslintInfo
60
72
  * Current directory and instance of ESLint used to load and configure the formatter.
61
73
  *
62
- * @param {string|Function} [formatter]
63
- * A name or path of a formatter, or an ESLint 6 style formatter function to resolve as a formatter.
74
+ * @param {string|ESLint.Formatter|FormatterFunction} [formatter]
75
+ * A name or path of a formatter, a formatter object or a formatter function.
64
76
  *
65
77
  * @returns {Promise<ESLint.Formatter>} An ESLint formatter.
66
78
  */
67
79
  function resolveFormatter({ cwd, eslint }, formatter) {
80
+ if (isObject(formatter) && typeof formatter.format === 'function') {
81
+ return formatter;
82
+ }
68
83
  if (typeof formatter === 'function') {
69
84
  return {
70
85
  format: results => {
@@ -142,7 +157,7 @@ async function awaitHandler(handler, data, done) {
142
157
  /**
143
158
  * Create a transform stream in object mode from synchronous or asynchronous handler functions.
144
159
  * All files are passed through the stream.
145
- * Errors thrown by the handlers will be wrapped inside a `PluginError` and emitted from the stream.
160
+ * Errors thrown by the handlers will be wrapped in a `PluginError` and emitted from the stream.
146
161
  *
147
162
  * @param {Function} handleFile
148
163
  * A function that is called for each file, with the file object as the only parameter.
@@ -153,7 +168,7 @@ async function awaitHandler(handler, data, done) {
153
168
  * A function that is called with no parameters before closing the stream.
154
169
  * If the function returns a promise, the stream will be closed after the promise is resolved.
155
170
  *
156
- * @returns {Stream} A transform stream.
171
+ * @returns {Transform} A transform stream.
157
172
  */
158
173
  exports.createTransform = (handleFile, handleFinal) => {
159
174
  const transform = (file, enc, done) => void awaitHandler(() => handleFile(file), file, done);
@@ -358,22 +373,24 @@ exports.migrateOptions = (options = { }) => {
358
373
  exports.resolveFormatter = resolveFormatter;
359
374
 
360
375
  /**
361
- * Resolve writable.
376
+ * Resolve a writer function used to write formatted ESLint messages.
362
377
  *
363
- * @param {Function|Stream} [writable=fancyLog]
378
+ * @param {GulpESLintWriter|NodeJS.WritableStream} [writer=fancyLog]
364
379
  * A stream or function to resolve as a format writer.
365
- * @returns {Function} A function that writes formatted messages.
380
+ * @returns {GulpESLintWriter} A function that writes formatted messages.
366
381
  */
367
- exports.resolveWritable = (writable = fancyLog) => {
368
- const { write } = writable;
369
- if (typeof write === 'function') {
370
- writable = write.bind(writable);
382
+ exports.resolveWriter = (writer = fancyLog) => {
383
+ if (isObject(writer)) {
384
+ const { write } = writer;
385
+ if (typeof write === 'function') {
386
+ writer = write.bind(writer);
387
+ }
371
388
  }
372
- return writable;
389
+ return writer;
373
390
  };
374
391
 
375
392
  /**
376
- * Write formatter results to writable/output.
393
+ * Write formatted ESLint messages.
377
394
  *
378
395
  * @param {ESLint.LintResult[]} results
379
396
  * A list of ESLint results.
@@ -381,16 +398,16 @@ exports.resolveWritable = (writable = fancyLog) => {
381
398
  * @param {{ cwd: string, eslint: ESLint }} eslintInfo
382
399
  * Current directory and instance of ESLint used to load and configure the formatter.
383
400
  *
384
- * @param {string|Function} [formatter]
385
- * A name or path of a formatter, or an ESLint 6 style formatter function to resolve as a formatter.
401
+ * @param {string|ESLint.Formatter|FormatterFunction} [formatter]
402
+ * A name or path of a formatter, a formatter object or a formatter function.
386
403
  *
387
- * @param {Function} [writable]
388
- * A function used to write formatted ESLint results.
404
+ * @param {GulpESLintWriter} [writer]
405
+ * A function used to write formatted ESLint messages.
389
406
  */
390
- exports.writeResults = async (results, eslintInfo, formatter, writable) => {
407
+ exports.writeResults = async (results, eslintInfo, formatter, writer) => {
391
408
  const formatterObj = await resolveFormatter(eslintInfo, formatter);
392
409
  const message = await formatterObj.format(results);
393
- if (writable && message != null && message !== '') {
394
- writable(message);
410
+ if (writer && message != null && message !== '') {
411
+ await writer(message);
395
412
  }
396
413
  };