Skip to content

Commit a11665f

Browse files
authored
feat: usage of ESM imports instead of CommonJS (#271)
1 parent 9f616cc commit a11665f

File tree

8 files changed

+98
-37
lines changed

8 files changed

+98
-37
lines changed

.github/workflows/old-test.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Special test for the oldest version of Node.js that we "support"
2+
# even though the linter won't actually run. Test that the command
3+
# line program exits cleanly.
4+
5+
name: Old test
6+
7+
on:
8+
push:
9+
branches: [master]
10+
pull_request:
11+
branches: [master]
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [0.10.48]
20+
21+
steps:
22+
- name: Checkout project
23+
uses: actions/checkout@v2.3.4
24+
25+
- name: Use Node.js ${{ matrix.node-version }}
26+
uses: actions/setup-node@v2.4.0
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
30+
- name: Cache Node dependencies
31+
uses: actions/cache@v2.1.6
32+
with:
33+
path: ~/.npm
34+
key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }}
35+
restore-keys: |
36+
${{ runner.os }}-node-
37+
38+
- name: Install dependencies
39+
run: npm install
40+
41+
- name: Test that the command line program exits cleanly.
42+
run: ./bin/cmd.js
43+
shell: bash

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [10.x, 12.x, 14.x, 16.x]
15+
node-version: [12.20.0, 14.13.1, 16.0.0]
1616
fail-fast: false
1717

1818
steps:

bin/cmd.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
#!/usr/bin/env node
2+
/* eslint-disable no-var, no-eval */
23

3-
const opts = require('../options.js')
4-
require('standard-engine').cli(opts)
4+
var match = process.version.match(/v(\d+)\.(\d+)/)
5+
var major = parseInt(match[1], 10)
6+
var minor = parseInt(match[2], 10)
7+
8+
if (major >= 12 || (major === 12 && minor >= 20)) {
9+
eval('import("standard-engine")').then(function (standardEngine) {
10+
eval('import("../options.js")').then(function (options) {
11+
standardEngine.cli(options.default)
12+
})
13+
})
14+
} else {
15+
console.error('semistandard: Node 12.20.0 or greater is required. `semistandard` did not run.')
16+
}

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*! semistandard. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
22
// programmatic usage
3-
const Linter = require('standard-engine').linter
3+
import engine from 'standard-engine'
4+
import opts from './options.js'
45

5-
const opts = require('./options.js')
6+
const Linter = engine.linter
67

7-
module.exports = new Linter(opts)
8+
export default new Linter(opts)

options.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
const path = require('path')
2-
const pkg = require('./package.json')
1+
import { fileURLToPath } from 'node:url'
2+
import { readFileSync } from 'node:fs'
3+
import eslint from 'eslint'
34

4-
module.exports = {
5+
const pkgUrl = new URL('./package.json', import.meta.url)
6+
const pkg = JSON.parse(readFileSync(pkgUrl, 'utf-8'))
7+
8+
export default {
59
// cmd, homepage, bugs all pulled from package.json
610
cmd: 'semistandard',
711
version: pkg.version,
812
homepage: pkg.homepage,
913
bugs: pkg.bugs.url,
1014
tagline: 'Semicolons For All!',
11-
eslint: require('eslint'),
15+
eslint,
1216
eslintConfig: {
13-
configFile: path.join(__dirname, 'eslintrc.json')
17+
configFile: fileURLToPath(new URL('eslintrc.json', import.meta.url))
1418
}
1519
}

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,26 @@
1414
"url": "https://github.com/standard/semistandard/issues"
1515
},
1616
"dependencies": {
17-
"eslint": "^7.27.0",
17+
"eslint": "^7.32.0",
1818
"eslint-config-semistandard": "16.0.0",
1919
"eslint-config-standard": "16.0.3",
2020
"eslint-config-standard-jsx": "10.0.0",
21-
"eslint-plugin-import": "^2.22.1",
21+
"eslint-plugin-import": "^2.24.2",
2222
"eslint-plugin-node": "^11.1.0",
2323
"eslint-plugin-promise": "^5.1.0",
24-
"eslint-plugin-react": "~7.21.5",
25-
"standard-engine": "^14.0.0"
24+
"eslint-plugin-react": "~7.25.1",
25+
"standard-engine": "^14.0.1"
2626
},
2727
"devDependencies": {
28-
"merge": "^1.2.1",
28+
"merge": "^2.1.1",
2929
"mkdirp": "^1.0.4",
3030
"rimraf": "^3.0.2",
3131
"run-series": "^1.1.9",
3232
"standard": "*",
33-
"tape": "^5.0.1",
34-
"xtend": "^4.0.2"
33+
"tape": "^5.3.1"
3534
},
3635
"engines": {
37-
"node": ">=10.12.0"
36+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
3837
},
3938
"homepage": "https://github.com/standard/semistandard",
4039
"keywords": [
@@ -66,6 +65,7 @@
6665
],
6766
"license": "MIT",
6867
"main": "index.js",
68+
"type": "module",
6969
"repository": {
7070
"type": "git",
7171
"url": "https://github.com/standard/semistandard.git"

test/api.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const path = require('path')
2-
const semistandard = require('../')
3-
const test = require('tape')
4-
const filePath = path.resolve('./bin/cmd.js')
1+
import { resolve } from 'node:path'
2+
import test from 'tape'
3+
import semistandard from '../index.js'
4+
5+
const filePath = resolve('./bin/cmd.js')
56

67
test('api usage', function (t) {
78
t.plan(6)
89
semistandard.lintFiles(['bin/cmd.js'], {}, function (err, result) {
910
t.error(err, 'no error while linting')
1011
t.equal(typeof result, 'object', 'result is an object')
11-
t.equal(result.errorCount, 2, 'error count 2')
12+
t.equal(result.errorCount, 7, 'error count 7')
1213

13-
t.equal(path.resolve(result.results[0].filePath), filePath, 'error filepath correct')
14-
t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'first mising semicolon message')
15-
t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'second mising semicolon message')
14+
t.equal(resolve(result.results[0].filePath), filePath, 'error filepath correct')
15+
t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'first missing semicolon message')
16+
t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'second missing semicolon message')
1617
})
1718
})

test/clone.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* VERSION BUMP.)
99
*/
1010

11-
const cp = require('child_process')
12-
const extend = require('xtend')
13-
const mkdirp = require('mkdirp')
14-
const path = require('path')
15-
const rimraf = require('rimraf')
16-
const series = require('run-series')
17-
const test = require('tape')
11+
import cp from 'node:child_process'
12+
import path from 'node:path'
13+
import { fileURLToPath } from 'node:url'
14+
import mkdirp from 'mkdirp'
15+
import rimraf from 'rimraf'
16+
import series from 'run-series'
17+
import test from 'tape'
1818

19-
const TMP = path.join(__dirname, '..', 'tmp')
20-
const SEMISTANDARD = path.join(__dirname, '..', 'bin', 'cmd.js')
19+
const TMP = fileURLToPath(new URL('../tmp', import.meta.url))
20+
const SEMISTANDARD = fileURLToPath(new URL('../bin/cmd.js', import.meta.url))
2121

2222
// const URLS = require('./semistandard-repos.json')
2323
const URLS = [
@@ -65,7 +65,7 @@ test('lint repos', function (t) {
6565
})
6666

6767
function spawn (command, args, opts, cb) {
68-
const child = cp.spawn(command, args, extend({ stdio: 'inherit' }, opts))
68+
const child = cp.spawn(command, args, { stdio: 'inherit', ...opts })
6969
child.on('error', cb)
7070
child.on('close', function (code) {
7171
if (code !== 0) cb(new Error('non-zero exit code: ' + code))

0 commit comments

Comments
 (0)