Skip to content

Commit 800142a

Browse files
committed
Overhaul linting + formatter approach
The standard linter didn't play nice with Prettier, so that we had standard undo certain formatting applied by Prettier. Thus we couldn't rely on the Prettier extension in say VS Code to apply formatting right away upon saving: an extra call in the CLI is necessary. This change simplifies the approach: we're using Prettier for formatting with an externalized config so that editors can pick it up, and eslint for linting, and the two won't interfere. This also allows us to add explicit steps for linting and formatting checks in the CI pipeline. We're also taking linting out of the "test" Grunt task. Unit tests and linting should be two distinct things.
1 parent 9fd191c commit 800142a

17 files changed

+57
-61
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
examples/**/node_modules
1+
dist

.eslintrc.json

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
{
2-
"extends": "standard",
3-
"rules": {
4-
"no-undef": "off",
5-
"no-unused-vars": "off",
6-
"no-var": "off",
7-
"space-before-function-paren": "error"
2+
"env": {
3+
"browser": true,
4+
"es2021": true
85
},
9-
"plugins": ["html", "markdown"],
10-
"overrides": [
11-
{
12-
"files": ["**/*.md"],
13-
"processor": "markdown/markdown"
14-
},
15-
{
16-
"files": ["**/*.md/*.javascript"],
17-
"rules": {
18-
"comma-dangle": ["error", "never"]
19-
}
20-
}
21-
]
6+
"extends": ["eslint:recommended", "prettier"],
7+
"overrides": [],
8+
"parserOptions": {
9+
"ecmaVersion": "latest",
10+
"sourceType": "module"
11+
},
12+
"rules": {}
2213
}

.github/workflows/ci.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ on:
99
- 'examples/**'
1010
- '**.md'
1111
- .gitignore
12-
- .prettierignore
1312
- .release-it.json
1413
pull_request:
1514
branches: [main]
@@ -19,13 +18,12 @@ on:
1918
- 'examples/**'
2019
- '**.md'
2120
- .gitignore
22-
- .prettierignore
2321
- .release-it.json
2422
schedule:
2523
- cron: '0 0 1 * *' # Every month
2624

2725
jobs:
28-
test:
26+
build:
2927
runs-on: ubuntu-latest
3028
strategy:
3129
matrix:
@@ -38,7 +36,11 @@ jobs:
3836
node-version: ${{ matrix.node-version }}
3937
- name: Install dependencies
4038
run: npm i
41-
- name: Unit tests
39+
- name: Check formatting
40+
run: npm run format:check
41+
- name: Lint
42+
run: npm run lint:check
43+
- name: Run unit tests
4244
run: npm test
4345

4446
e2e-test:

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"singleQuote": true,
4+
"trailingComma": "none"
5+
}

Gruntfile.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
function encodingMiddleware (request, response, next) {
1+
/* eslint-env node */
2+
function encodingMiddleware(request, response, next) {
23
const URL = require('url').URL
34
const url = new URL(request.url, 'http://localhost')
45

@@ -90,10 +91,9 @@ const config = {
9091
}
9192
},
9293
exec: {
94+
format: 'npm run format',
95+
lint: 'npm run lint',
9396
rollup: 'npx rollup -c',
94-
lint: 'npx standard',
95-
format:
96-
'npx prettier -l --write --single-quote --no-semi "**/*.{html,js,json,md,mjs,yml}" && npx eslint "**/*.{html,md}" --fix && npx standard --fix',
9797
'browserstack-runner': 'node_modules/.bin/browserstack-runner --verbose'
9898
}
9999
}
@@ -107,7 +107,6 @@ module.exports = function (grunt) {
107107
.forEach(grunt.loadNpmTasks)
108108

109109
grunt.registerTask('test', [
110-
'exec:lint',
111110
'exec:rollup',
112111
'connect:build-qunit',
113112
'qunit',
@@ -117,6 +116,11 @@ module.exports = function (grunt) {
117116
'exec:rollup',
118117
'exec:browserstack-runner'
119118
])
120-
grunt.registerTask('dev', ['exec:format', 'test', 'compare_size'])
119+
grunt.registerTask('dev', [
120+
'exec:format',
121+
'exec:lint',
122+
'test',
123+
'compare_size'
124+
])
121125
grunt.registerTask('default', 'dev')
122126
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="https://cloud.githubusercontent.com/assets/835857/14581711/ba623018-0436-11e6-8fce-d2ccd4d379c9.gif">
33
</p>
44

5-
# JavaScript Cookie [![CI](https://github.com/js-cookie/js-cookie/actions/workflows/ci.yml/badge.svg)](https://github.com/js-cookie/js-cookie/actions/workflows/ci.yml) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Code Climate](https://codeclimate.com/github/js-cookie/js-cookie.svg)](https://codeclimate.com/github/js-cookie/js-cookie) [![npm](https://img.shields.io/github/package-json/v/js-cookie/js-cookie)](https://www.npmjs.com/package/js-cookie) [![size](https://img.shields.io/bundlephobia/minzip/js-cookie/3)](https://www.npmjs.com/package/js-cookie) [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/js-cookie/badge?style=rounded)](https://www.jsdelivr.com/package/npm/js-cookie)
5+
# JavaScript Cookie [![CI](https://github.com/js-cookie/js-cookie/actions/workflows/ci.yml/badge.svg)](https://github.com/js-cookie/js-cookie/actions/workflows/ci.yml) [![Code Climate](https://codeclimate.com/github/js-cookie/js-cookie.svg)](https://codeclimate.com/github/js-cookie/js-cookie) [![npm](https://img.shields.io/github/package-json/v/js-cookie/js-cookie)](https://www.npmjs.com/package/js-cookie) [![size](https://img.shields.io/bundlephobia/minzip/js-cookie/3)](https://www.npmjs.com/package/js-cookie) [![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/js-cookie/badge?style=rounded)](https://www.jsdelivr.com/package/npm/js-cookie)
66

77
A simple, lightweight JavaScript API for handling cookies
88

examples/webpack/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env node */
12
const nodeStatic = require('node-static')
23
const file = new nodeStatic.Server('./dist')
34
const port = 8080

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/* eslint-env node */
12
module.exports = require('./dist/js.cookie')

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
],
2929
"scripts": {
3030
"test": "grunt test",
31-
"format": "grunt exec:format",
31+
"format": "prettier --list-different --write .",
32+
"format:check": "prettier --list-different .",
33+
"lint": "eslint --ext .js,.mjs --fix .",
34+
"lint:check": "eslint --ext .js,.mjs .",
3235
"dist": "rm -rf dist/* && rollup -c",
3336
"release": "release-it"
3437
},
@@ -46,7 +49,7 @@
4649
"@rollup/plugin-terser": "^0.4.0",
4750
"browserstack-runner": "github:browserstack/browserstack-runner#1e85e559951bdf97ffe2a7c744ee67ca83589fde",
4851
"eslint": "^8.43.0",
49-
"eslint-config-standard": "^17.1.0",
52+
"eslint-config-prettier": "^8.8.0",
5053
"eslint-plugin-html": "^7.0.0",
5154
"eslint-plugin-markdown": "^3.0.0",
5255
"grunt": "^1.0.4",
@@ -62,8 +65,7 @@
6265
"release-it": "^15.0.0",
6366
"rollup": "^3.17.2",
6467
"rollup-plugin-filesize": "^10.0.0",
65-
"rollup-plugin-license": "^3.0.0",
66-
"standard": "^17.0.0"
68+
"rollup-plugin-license": "^3.0.0"
6769
},
6870
"engines": {
6971
"node": ">=16"

src/api.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/* eslint-disable no-var */
21
import assign from './assign.mjs'
32
import defaultConverter from './converter.mjs'
43

5-
function init (converter, defaultAttributes) {
6-
function set (name, value, attributes) {
4+
function init(converter, defaultAttributes) {
5+
function set(name, value, attributes) {
76
if (typeof document === 'undefined') {
87
return
98
}
@@ -47,7 +46,7 @@ function init (converter, defaultAttributes) {
4746
name + '=' + converter.write(value, name) + stringifiedAttributes)
4847
}
4948

50-
function get (name) {
49+
function get(name) {
5150
if (typeof document === 'undefined' || (arguments.length && !name)) {
5251
return
5352
}
@@ -67,7 +66,9 @@ function init (converter, defaultAttributes) {
6766
if (name === found) {
6867
break
6968
}
70-
} catch (e) {}
69+
} catch (e) {
70+
// Do nothing...
71+
}
7172
}
7273

7374
return name ? jar[name] : jar
@@ -101,4 +102,3 @@ function init (converter, defaultAttributes) {
101102
}
102103

103104
export default init(defaultConverter, { path: '/' })
104-
/* eslint-enable no-var */

src/assign.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-var */
21
export default function (target) {
32
for (var i = 1; i < arguments.length; i++) {
43
var source = arguments[i]
@@ -8,4 +7,3 @@ export default function (target) {
87
}
98
return target
109
}
11-
/* eslint-enable no-var */

src/converter.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-var */
21
export default {
32
read: function (value) {
43
if (value[0] === '"') {
@@ -13,4 +12,3 @@ export default {
1312
)
1413
}
1514
}
16-
/* eslint-enable no-var */

test/encoding.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* global QUnit, lifecycle, using */
2-
/* eslint-disable no-var */
32

43
QUnit.module('cookie-value', lifecycle)
54

@@ -1169,5 +1168,3 @@ QUnit.test('cookie-name - 4 bytes characters', function (assert) {
11691168
)
11701169
})
11711170
})
1172-
1173-
/* eslint-enable no-var */

test/missing_semicolon.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<script>
77
window.__ok = false
88
;(function () {
9-
function loadFileSync (path) {
9+
function loadFileSync(path) {
1010
var xhr = new window.XMLHttpRequest()
1111
xhr.open('GET', path, false)
1212
xhr.send(null)

test/node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-env node */
12
exports.node = {
23
shouldLoadApi: function (test) {
34
test.expect(1)

test/tests.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* global Cookies, QUnit, lifecycle, quoted */
2-
/* eslint-disable no-var */
32

43
QUnit.module('setup', lifecycle)
54

@@ -18,7 +17,7 @@ QUnit.test('api instance creation', function (assert) {
1817
)
1918

2019
api = Cookies.withConverter({
21-
write: function (value, name) {
20+
write: function (value) {
2221
return value.toUpperCase()
2322
}
2423
}).withAttributes({ path: '/foo' })
@@ -28,7 +27,7 @@ QUnit.test('api instance creation', function (assert) {
2827
)
2928

3029
api = Cookies.withAttributes({ path: '/foo' }).withConverter({
31-
write: function (value, name) {
30+
write: function (value) {
3231
return value.toUpperCase()
3332
}
3433
})
@@ -252,7 +251,7 @@ QUnit.test('String primitive', function (assert) {
252251
})
253252

254253
QUnit.test('String object', function (assert) {
255-
/* eslint-disable no-new-wrappers */
254+
// eslint-disable-next-line no-new-wrappers
256255
Cookies.set('c', new String('v'))
257256
assert.strictEqual(Cookies.get('c'), 'v', 'should write value')
258257
})
@@ -605,5 +604,3 @@ QUnit.test('do not conflict with existent globals', function (assert) {
605604
)
606605
window.Cookies = Cookies
607606
})
608-
609-
/* eslint-enable no-var */

test/utils.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* global Cookies */
2-
/* eslint-disable no-var */
32

43
;(function () {
54
window.lifecycle = {
@@ -18,7 +17,7 @@
1817
}
1918

2019
window.using = function (assert) {
21-
function getQuery (key) {
20+
function getQuery(key) {
2221
var queries = window.location.href.split('?')[1]
2322
if (!queries) {
2423
return
@@ -30,7 +29,7 @@
3029
return decodeURIComponent(result)
3130
}
3231
}
33-
function setCookie (name, value) {
32+
function setCookie(name, value) {
3433
return {
3534
then: function (callback) {
3635
var iframe = document.getElementById('request_target')
@@ -54,7 +53,9 @@
5453
)
5554
callback(result.value, iframeDocument.cookie)
5655
done()
57-
} catch (e) {}
56+
} catch (e) {
57+
// Do nothing...
58+
}
5859
})
5960
iframe.src = requestURL
6061
}
@@ -70,5 +71,3 @@
7071
return '"' + input + '"'
7172
}
7273
})()
73-
74-
/* eslint-enable no-var */

0 commit comments

Comments
 (0)