eslint-formatter-gitlab 7.0.1 → 7.2.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
@@ -15,6 +15,8 @@ Show ESLint results directly in the
15
15
 
16
16
  - [Installation](#installation)
17
17
  - [Usage](#usage)
18
+ - [GitLab CI](#gitlab-ci)
19
+ - [CLI](#cli)
18
20
  - [Programmatic usage](#programmatic-usage)
19
21
  - [Example](#example)
20
22
  - [Configuration](#configuration)
@@ -31,6 +33,8 @@ npm install --save-dev eslint eslint-formatter-gitlab
31
33
 
32
34
  ## Usage
33
35
 
36
+ ### GitLab CI
37
+
34
38
  Define a GitLab job to run `eslint`.
35
39
 
36
40
  `.gitlab-ci.yml`:
@@ -50,9 +54,19 @@ The formatter automatically detects a GitLab CI environment. It detects where to
50
54
  quality report based on the GitLab configuration file. It also prints ESLint issues to the GitLab
51
55
  job console with links.
52
56
 
57
+ ### CLI
58
+
59
+ Specify the `gitlab` formatter when running `eslint` from the command line.
60
+
61
+ ```sh
62
+ npx eslint --format gitlab
63
+ ```
64
+
53
65
  ### Programmatic usage
54
66
 
55
- The formatter can be used programmatically using ESLint.
67
+ You probably want to use
68
+ [`eslint-formatter-codeclimate`](https://github.com/remcohaszing/eslint-formatter-codeclimate)
69
+ instead. But if you must, you can load the `gitlab` formatter like this using ESLint.
56
70
 
57
71
  ```js
58
72
  import { ESLint } from 'eslint'
@@ -1,4 +1,5 @@
1
1
  /**
2
+ * @import { InspectColorForeground } from 'node:util'
2
3
  * @import { ESLint } from 'eslint'
3
4
  */
4
5
 
@@ -35,18 +36,29 @@ async function getOutputPath(projectDir, jobName) {
35
36
  let configContents
36
37
  try {
37
38
  configContents = await readFile(configPath, 'utf8')
38
- } catch {
39
+ } catch (cause) {
39
40
  throw new Error(
40
41
  'Could not resolve .gitlab-ci.yml to automatically detect report artifact path.' +
41
- ' Please manually provide a path via the ESLINT_CODE_QUALITY_REPORT variable.'
42
+ ' Please manually provide a path via the ESLINT_CODE_QUALITY_REPORT variable.',
43
+ { cause }
42
44
  )
43
45
  }
44
- const doc = yaml.parseDocument(configContents, {
46
+ // A GitLab CI configuration file may start with a header document.
47
+ // https://docs.gitlab.com/ci/yaml/#header-keywords
48
+ const docs = yaml.parseAllDocuments(configContents, {
45
49
  version: '1.1',
46
50
  customTags: [reference]
47
51
  })
48
52
  const path = [jobName, 'artifacts', 'reports', 'codequality']
49
- const location = doc.getIn(path)
53
+
54
+ /** @type {unknown} */
55
+ let location
56
+ for (const doc of docs) {
57
+ location = doc.getIn(path)
58
+ if (location != null) {
59
+ break
60
+ }
61
+ }
50
62
  if (typeof location !== 'string' || !location) {
51
63
  throw new TypeError(
52
64
  `Expected ${path.join('.')} to be one exact path, got: ${JSON.stringify(location)}`
@@ -74,14 +86,16 @@ function plural(count, text) {
74
86
  * The ESLint report results.
75
87
  * @param {string} projectDir
76
88
  * The GitLab project directory.
89
+ * @param {(color: InspectColorForeground, text: string) => string} color
90
+ * A function to color text or not.
77
91
  * @returns {string}
78
92
  * The ESLint messages converted to a format suitable as output in GitLab CI job logs.
79
93
  */
80
- function gitlabConsoleFormatter(results, projectDir) {
94
+ function gitlabConsoleFormatter(results, projectDir, color) {
81
95
  // Severity labels manually padded to have equal lengths and end with spaces
82
- const labelFatal = `${styleText('magenta', 'fatal')} `
83
- const labelError = `${styleText('red', 'error')} `
84
- const labelWarn = `${styleText('yellow', 'warn')} `
96
+ const labelFatal = `${color('magenta', 'fatal')} `
97
+ const labelError = `${color('red', 'error')} `
98
+ const labelWarn = `${color('yellow', 'warn')} `
85
99
 
86
100
  const lines = ['']
87
101
 
@@ -126,7 +140,7 @@ function gitlabConsoleFormatter(results, projectDir) {
126
140
  if (message.endLine != null && message.endLine !== message.line) {
127
141
  anchor += `-${message.endLine}`
128
142
  }
129
- line += styleText('blue', `${gitLabBaseURL}${repoFilePath}${anchor}`)
143
+ line += color('blue', `${gitLabBaseURL}${repoFilePath}${anchor}`)
130
144
  } else {
131
145
  line += `${filePath}:${message.line}:${message.column}`
132
146
  }
@@ -138,9 +152,9 @@ function gitlabConsoleFormatter(results, projectDir) {
138
152
  const total = warnings + errors + fatal
139
153
  if (total > 0) {
140
154
  const details = `(${fatal} fatal, ${plural(errors, 'error')}, ${plural(warnings, 'warning')})`
141
- lines.push('', `${styleText('red', '✖')} ${plural(total, 'problem')} ${details}`)
155
+ lines.push('', `${color('red', '✖')} ${plural(total, 'problem')} ${details}`)
142
156
  } else {
143
- lines.push(`${styleText('green', '✔')} No problems found`)
157
+ lines.push(`${color('green', '✔')} No problems found`)
144
158
  }
145
159
 
146
160
  lines.push('')
@@ -167,7 +181,15 @@ async function eslintFormatterGitLab(results, data) {
167
181
  await writeFile(outputPath, `${JSON.stringify(issues, null, 2)}\n`)
168
182
  }
169
183
 
170
- return gitlabConsoleFormatter(results, projectDir)
184
+ return gitlabConsoleFormatter(
185
+ results,
186
+ projectDir,
187
+ data.color
188
+ ? (color, text) => styleText(color, text, { validateStream: false })
189
+ : data.color === false
190
+ ? (color, text) => text
191
+ : styleText
192
+ )
171
193
  }
172
194
 
173
195
  export default eslintFormatterGitLab
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-formatter-gitlab",
3
- "version": "7.0.1",
3
+ "version": "7.2.0",
4
4
  "description": "Show ESLint results directly in the GitLab code quality results",
5
5
  "type": "module",
6
6
  "author": "Remco Haszing <remcohaszing@gmail.com>",
@@ -38,13 +38,13 @@
38
38
  "eslint": ">=9"
39
39
  },
40
40
  "devDependencies": {
41
- "@remcohaszing/eslint": "^12.0.0",
42
- "@types/node": "^24.0.0",
43
- "c8": "^10.0.0",
41
+ "@remcohaszing/eslint": "^14.0.0",
42
+ "@types/node": "^25.0.0",
43
+ "c8": "^11.0.0",
44
44
  "codeclimate-types": "^0.3.0",
45
45
  "prettier": "^3.0.0",
46
46
  "remark-cli": "^12.0.0",
47
47
  "remark-preset-remcohaszing": "^3.0.0",
48
- "typescript": "^5.0.0"
48
+ "typescript": "^6.0.0"
49
49
  }
50
50
  }
@@ -1 +1 @@
1
- {"version":3,"file":"eslint-formatter-gitlab.d.ts","sourceRoot":"","sources":["../lib/eslint-formatter-gitlab.js"],"names":[],"mappings":";AAqJA;;;;;;;GAOG;AACH,gDAPW,iBAAiB,EAAE,QAEnB,qBAAqB,GAEnB,OAAO,CAAC,MAAM,CAAC,CAgB3B;4BAzK0B,QAAQ"}
1
+ {"version":3,"file":"eslint-formatter-gitlab.d.ts","sourceRoot":"","sources":["../lib/eslint-formatter-gitlab.js"],"names":[],"mappings":";AAmKA;;;;;;;GAOG;AACH,gDAPW,iBAAiB,EAAE,QAEnB,qBAAqB,GAEnB,OAAO,CAAC,MAAM,CAAC,CAwB3B;4BA9L0B,QAAQ"}