Skip to content

Commit 5f66f04

Browse files
voxpelliLinusU
andauthored
Port to new ESLint API (#275)
* Validate engine versions + dependency list * Probably make all pass * Fix failing tests * Fix Node 12 support * Remove leftover todo comment * Improve naming of "results" post refactoring * Extract `hasErrors` and `hasWarnings` variables * Add coverage report * Exclude tests from type checks As else it checks `tmp/standard/`, which doesn't pass * Tweak type format * Extract utility methods * Remove non-needed @ts-ignore * Add unit tests for utils * We need to ensure we run the clone test first * Rename `build` to `lint` * Rename `Linter` to `StandardEngine` * Update README + CHANGELOG * Sync `dependabot.yml` with standard/standard repo See standard/standard#1752 * Update CHANGELOG.md Co-authored-by: Linus Unnebäck <[email protected]>
1 parent 606f80a commit 5f66f04

18 files changed

+417
-155
lines changed

.github/dependabot.yml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
# For more information see: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates
2-
31
version: 2
42
updates:
5-
- package-ecosystem: 'github-actions'
6-
directory: '/'
3+
- package-ecosystem: "npm"
4+
directory: "/"
75
schedule:
8-
interval: 'daily'
6+
interval: "daily"
7+
labels:
8+
- "dependency"
9+
# Always increase the version requirement to match the new version.
10+
versioning-strategy: increase
911

10-
- package-ecosystem: 'npm'
11-
directory: '/'
12+
- package-ecosystem: "github-actions"
13+
directory: "/"
14+
ignore:
15+
- dependency-name: "actions/*"
16+
update-types:
17+
["version-update:semver-minor", "version-update:semver-patch"]
1218
schedule:
13-
interval: 'daily'
19+
interval: "daily"
20+
labels:
21+
- "github-actions"

.github/workflows/lint.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: 'linting'
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
lint:
11+
runs-on: 'ubuntu-latest'
12+
13+
strategy:
14+
matrix:
15+
node-version: ['lts/*']
16+
fail-fast: false
17+
18+
steps:
19+
- name: 'Checkout Project'
20+
uses: 'actions/checkout@v2'
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: 'actions/setup-node@v2'
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
29+
- name: 'Cache Node dependencies'
30+
uses: 'actions/cache@v2'
31+
with:
32+
path: '~/.npm'
33+
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
34+
restore-keys: |
35+
${{ runner.os }}-node-
36+
37+
- name: 'Install Dependencies'
38+
run: 'npm install'
39+
40+
- name: 'Run Tests'
41+
run: 'npm run check'

.github/workflows/nodejs.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ jobs:
1717

1818
steps:
1919
- name: 'Checkout Project'
20-
uses: 'actions/checkout@v2.4.0'
20+
uses: 'actions/checkout@v2'
2121
with:
2222
fetch-depth: 0
2323

2424
- name: Use Node.js ${{ matrix.node-version }}
25-
uses: 'actions/setup-node@v2.4.1'
25+
uses: 'actions/setup-node@v2'
2626
with:
2727
node-version: ${{ matrix.node-version }}
2828

2929
- name: 'Cache Node dependencies'
30-
uses: 'actions/cache@v2.1.7'
30+
uses: 'actions/cache@v2'
3131
with:
3232
path: '~/.npm'
3333
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
@@ -38,4 +38,4 @@ jobs:
3838
run: 'npm install'
3939

4040
- name: 'Run Tests'
41-
run: 'npm test'
41+
run: 'npm run test-ci'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
tmp/
3+
coverage/

CHANGELOG.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,67 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 15.0.0 YYYY-MM-DD
6+
7+
- **BREAKING CHANGE:** To avoid confusion with ESLint exports and types, our `Linter` class has been renamed to `StandardEngine` and `cli()` now takes a `standardEngine` key instead of a `linter` key if a custom engine wants to be provided. #275
8+
- **BREAKING CHANGE:** Removed use of ESLint's deprecated `CLIEngine` API. This affects the `eslintConfig` option to our `StandardEngine` (formerly called `Linter`) constructor. #275
9+
- **BREAKING CHANGE:** Print additional label on warnings (to separate them from errors) b7c1e17
10+
- **BREAKING CHANGE:** Drop support for Node 10.x. Now require ESM-compatible Node.js versions: `^12.20.0 || ^14.13.1 || >=16.0.0` #252
11+
- **BREAKING CHANGE:** the `parseOpts` option to the `StandardEngine` (formerly called `Linter`) constructor has been replaced with a new `resolveEslintConfig` one
12+
- Change: make `--verbose` the default #232
13+
14+
15+
## 14.0.1 2020-08-31
16+
17+
- _Missing release notes_
18+
19+
## 14.0.0 2020-08-29
20+
21+
- _Missing release notes_
22+
23+
## 13.0.0 2020-08-27
24+
25+
- _Missing release notes_
26+
27+
## 12.1.1 2020-05-21
28+
29+
- Enhancement: Allow passing in a custom linter to `cli`
30+
31+
## 12.0.1 2020-04-30
32+
33+
- Enhancements: Add ts-standard to README linters list
34+
- Fixes: Bump deglob & minimist dependencies
35+
36+
## 12.0.0 2019-08-19
37+
38+
- **BREAKING CHANGE:** Remove `bundle.js` from the list of default ignored files
39+
- **BREAKING CHANGE:** Ignore patterns from `.git/info/exclude` in addition to `.gitignore`
40+
- Enhancement: Update deglob to 4.x
41+
42+
## 11.0.1 2019-07-12
43+
44+
- _Missing release notes_
45+
46+
## 11.0.0 2019-07-11
47+
48+
- _Missing release notes_
49+
50+
## 10.0.0 2018-08-30
51+
52+
- _Missing release notes_
53+
54+
## 9.0.0 2018-05-15
55+
56+
- _Missing release notes_
57+
58+
## 8.0.1 2018-03-02
59+
60+
- _Missing release notes_
61+
62+
## 8.0.0 2018-02-18
63+
64+
- _Missing release notes_
65+
566
## 7.2.0 2017-11-07
667

768
- New Feature: `noDefaultIgnore` option to can now be used to turn off default ignores.

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Create the files below and fill in your own values for `options.js`.
4242

4343
```js
4444
// programmatic usage
45-
const { Linter } = require('standard-engine')
45+
const { StandardEngine } = require('standard-engine')
4646
const opts = require('./options.js')
47-
module.exports = new Linter(opts)
47+
module.exports = new StandardEngine(opts)
4848
```
4949

5050
### `cli.js`
@@ -64,6 +64,7 @@ const eslint = require('eslint')
6464
const path = require('path')
6565
const pkg = require('./package.json')
6666

67+
/** @type {import('standard-engine').StandardEngineOptions} **/
6768
module.exports = {
6869
// homepage, version and bugs pulled from package.json
6970
version: pkg.version,
@@ -73,7 +74,7 @@ module.exports = {
7374
cmd: 'pocketlint', // should match the "bin" key in your package.json
7475
tagline: 'Live by your own standards!', // displayed in output --help
7576
eslintConfig: {
76-
configFile: path.join(__dirname, 'eslintrc.json')
77+
overrideConfigFile: path.join(__dirname, 'eslintrc.json')
7778
},
7879
cwd: '' // current working directory, passed to eslint
7980
}
@@ -293,13 +294,13 @@ module.exports = {
293294
}
294295
```
295296

296-
This function is called with the current ESLint config (the options passed to [ESLint's `CLIEngine`](http://eslint.org/docs/developer-guide/nodejs-api#cliengine)), the options object (`opts`), any options extracted from the project's `package.json` (`packageOpts`), and the directory that contained that `package.json` file (`rootDir`, equivalent to `opts.cwd` if no file was found).
297+
This function is called with the current ESLint config (the options passed to the [`ESLint`](https://eslint.org/docs/developer-guide/nodejs-api#-new-eslintoptions) constructor), the options object (`opts`), any options extracted from the project's `package.json` (`packageOpts`), and the directory that contained that `package.json` file (`rootDir`, equivalent to `opts.cwd` if no file was found).
297298

298299
Modify and return `eslintConfig`, or return a new object with the eslint config to be used.
299300

300301
## API Usage
301302

302-
### `engine.lintText(text, [opts])`
303+
### `async engine.lintText(text, [opts])`
303304

304305
Lint the provided source `text` to enforce your defined style. An `opts` object may
305306
be provided:
@@ -348,12 +349,7 @@ const results = {
348349
}
349350
```
350351

351-
### `results = engine.lintTextSync(text, [opts])`
352-
353-
Synchronous version of `engine.lintText()`. If an error occurs, an exception is
354-
thrown. Otherwise, a `results` object is returned.
355-
356-
### `engine.lintFiles(files, [opts], callback)`
352+
### `async engine.lintFiles(files, [opts])`
357353

358354
Lint the provided `files` globs. An `opts` object may be provided:
359355

bin/cmd.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const getStdin = require('get-stdin')
55

66
/**
77
* @typedef StandardCliOptions
8-
* @property {import('../').Linter} [linter]
8+
* @property {import('../').StandardEngine} [standardEngine]
99
* @property {string} [cmd]
1010
* @property {string} [tagline]
1111
* @property {string} [homepage]
1212
* @property {string} [bugs]
1313
*/
1414

1515
/**
16-
* @param {Omit<import('../').LinterOptions, 'cmd'> & StandardCliOptions} rawOpts
16+
* @param {Omit<import('../').StandardEngineOptions, 'cmd'> & StandardCliOptions} rawOpts
1717
* @returns {void}
1818
*/
1919
function cli (rawOpts) {
@@ -24,7 +24,7 @@ function cli (rawOpts) {
2424
...rawOpts
2525
}
2626

27-
const standard = rawOpts.linter || new (require('../').Linter)(opts)
27+
const standard = rawOpts.standardEngine || new (require('../').StandardEngine)(opts)
2828

2929
const argv = minimist(process.argv.slice(2), {
3030
alias: {
@@ -118,11 +118,11 @@ Flags (advanced):
118118
}
119119

120120
Promise.resolve(argv.stdin ? getStdin() : '').then(async stdinText => {
121-
/** @type {import('eslint').CLIEngine.LintReport} */
122-
let result
121+
/** @type {import('eslint').ESLint.LintResult[]} */
122+
let results
123123

124124
try {
125-
result = argv.stdin
125+
results = argv.stdin
126126
? await standard.lintText(stdinText, lintOpts)
127127
: await standard.lintFiles(argv._, lintOpts)
128128
} catch (err) {
@@ -141,29 +141,29 @@ Flags (advanced):
141141
return
142142
}
143143

144-
if (!result) throw new Error('expected a result')
144+
if (!results) throw new Error('expected a results')
145145

146146
if (outputFixed) {
147-
if (result.results[0] && result.results[0].output) {
147+
if (results[0] && results[0].output) {
148148
// Code contained fixable errors, so print the fixed code
149-
process.stdout.write(result.results[0].output)
149+
process.stdout.write(results[0].output)
150150
} else {
151151
// Code did not contain fixable errors, so print original code
152152
process.stdout.write(stdinText)
153153
}
154154
}
155155

156-
if (!result.errorCount && !result.warningCount) {
156+
const hasErrors = results.some(item => item.errorCount !== 0)
157+
const hasWarnings = results.some(item => item.warningCount !== 0)
158+
159+
if (!hasErrors && !hasWarnings) {
157160
process.exitCode = 0
158161
return
159162
}
160163

161164
console.error('%s: %s (%s)', opts.cmd, opts.tagline, opts.homepage)
162165

163-
// Are any warnings present?
164-
const isSomeWarnings = result.results.some(item => item.messages.some(message => message.severity === 1))
165-
166-
if (isSomeWarnings) {
166+
if (hasWarnings) {
167167
const homepage = opts.homepage != null ? ` (${opts.homepage})` : ''
168168
console.error(
169169
'%s: %s',
@@ -173,17 +173,17 @@ Flags (advanced):
173173
}
174174

175175
// Are any fixable rules present?
176-
const isSomeFixable = result.results.some(item => item.messages.some(message => !!message.fix))
176+
const hasFixable = results.some(item => item.messages.some(message => !!message.fix))
177177

178-
if (isSomeFixable) {
178+
if (hasFixable) {
179179
console.error(
180180
'%s: %s',
181181
opts.cmd,
182182
'Run `' + opts.cmd + ' --fix` to automatically fix some problems.'
183183
)
184184
}
185185

186-
for (const item of result.results) {
186+
for (const item of results) {
187187
for (const message of item.messages) {
188188
log(
189189
' %s:%d:%d: %s%s%s',
@@ -197,7 +197,7 @@ Flags (advanced):
197197
}
198198
}
199199

200-
process.exitCode = result.errorCount ? 1 : 0
200+
process.exitCode = hasErrors ? 1 : 0
201201
})
202202
.catch(err => process.nextTick(() => { throw err }))
203203
}

0 commit comments

Comments
 (0)