From 800142a4ff43dc3e61f566cdcb63aad7d89a0407 Mon Sep 17 00:00:00 2001 From: Klaus Hartl Date: Thu, 22 Jun 2023 18:00:50 +0200 Subject: [PATCH] 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. --- .eslintignore | 2 +- .eslintrc.json | 29 ++++++++++------------------- .github/workflows/ci.yml | 10 ++++++---- .prettierrc.json | 5 +++++ Gruntfile.js | 16 ++++++++++------ README.md | 2 +- examples/webpack/server.js | 1 + index.js | 1 + package.json | 10 ++++++---- src/api.mjs | 12 ++++++------ src/assign.mjs | 2 -- src/converter.mjs | 2 -- test/encoding.js | 3 --- test/missing_semicolon.html | 2 +- test/node.js | 1 + test/tests.js | 9 +++------ test/utils.js | 11 +++++------ 17 files changed, 57 insertions(+), 61 deletions(-) create mode 100644 .prettierrc.json diff --git a/.eslintignore b/.eslintignore index b241fdda..1521c8b7 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1 @@ -examples/**/node_modules \ No newline at end of file +dist diff --git a/.eslintrc.json b/.eslintrc.json index 66d13503..b8d65b77 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,22 +1,13 @@ { - "extends": "standard", - "rules": { - "no-undef": "off", - "no-unused-vars": "off", - "no-var": "off", - "space-before-function-paren": "error" + "env": { + "browser": true, + "es2021": true }, - "plugins": ["html", "markdown"], - "overrides": [ - { - "files": ["**/*.md"], - "processor": "markdown/markdown" - }, - { - "files": ["**/*.md/*.javascript"], - "rules": { - "comma-dangle": ["error", "never"] - } - } - ] + "extends": ["eslint:recommended", "prettier"], + "overrides": [], + "parserOptions": { + "ecmaVersion": "latest", + "sourceType": "module" + }, + "rules": {} } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 360b7102..a2212a5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,6 @@ on: - 'examples/**' - '**.md' - .gitignore - - .prettierignore - .release-it.json pull_request: branches: [main] @@ -19,13 +18,12 @@ on: - 'examples/**' - '**.md' - .gitignore - - .prettierignore - .release-it.json schedule: - cron: '0 0 1 * *' # Every month jobs: - test: + build: runs-on: ubuntu-latest strategy: matrix: @@ -38,7 +36,11 @@ jobs: node-version: ${{ matrix.node-version }} - name: Install dependencies run: npm i - - name: Unit tests + - name: Check formatting + run: npm run format:check + - name: Lint + run: npm run lint:check + - name: Run unit tests run: npm test e2e-test: diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 00000000..49955e2e --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,5 @@ +{ + "semi": false, + "singleQuote": true, + "trailingComma": "none" +} diff --git a/Gruntfile.js b/Gruntfile.js index 35e108be..fbaeab38 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,4 +1,5 @@ -function encodingMiddleware (request, response, next) { +/* eslint-env node */ +function encodingMiddleware(request, response, next) { const URL = require('url').URL const url = new URL(request.url, 'http://localhost') @@ -90,10 +91,9 @@ const config = { } }, exec: { + format: 'npm run format', + lint: 'npm run lint', rollup: 'npx rollup -c', - lint: 'npx standard', - format: - 'npx prettier -l --write --single-quote --no-semi "**/*.{html,js,json,md,mjs,yml}" && npx eslint "**/*.{html,md}" --fix && npx standard --fix', 'browserstack-runner': 'node_modules/.bin/browserstack-runner --verbose' } } @@ -107,7 +107,6 @@ module.exports = function (grunt) { .forEach(grunt.loadNpmTasks) grunt.registerTask('test', [ - 'exec:lint', 'exec:rollup', 'connect:build-qunit', 'qunit', @@ -117,6 +116,11 @@ module.exports = function (grunt) { 'exec:rollup', 'exec:browserstack-runner' ]) - grunt.registerTask('dev', ['exec:format', 'test', 'compare_size']) + grunt.registerTask('dev', [ + 'exec:format', + 'exec:lint', + 'test', + 'compare_size' + ]) grunt.registerTask('default', 'dev') } diff --git a/README.md b/README.md index 118595c2..3914a09b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

-# 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) +# 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) A simple, lightweight JavaScript API for handling cookies diff --git a/examples/webpack/server.js b/examples/webpack/server.js index d03abd8a..dbb38140 100644 --- a/examples/webpack/server.js +++ b/examples/webpack/server.js @@ -1,3 +1,4 @@ +/* eslint-env node */ const nodeStatic = require('node-static') const file = new nodeStatic.Server('./dist') const port = 8080 diff --git a/index.js b/index.js index 992ca3ef..a37b7b53 100644 --- a/index.js +++ b/index.js @@ -1 +1,2 @@ +/* eslint-env node */ module.exports = require('./dist/js.cookie') diff --git a/package.json b/package.json index a43920d5..60c30d46 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,10 @@ ], "scripts": { "test": "grunt test", - "format": "grunt exec:format", + "format": "prettier --list-different --write .", + "format:check": "prettier --list-different .", + "lint": "eslint --ext .js,.mjs --fix .", + "lint:check": "eslint --ext .js,.mjs .", "dist": "rm -rf dist/* && rollup -c", "release": "release-it" }, @@ -46,7 +49,7 @@ "@rollup/plugin-terser": "^0.4.0", "browserstack-runner": "github:browserstack/browserstack-runner#1e85e559951bdf97ffe2a7c744ee67ca83589fde", "eslint": "^8.43.0", - "eslint-config-standard": "^17.1.0", + "eslint-config-prettier": "^8.8.0", "eslint-plugin-html": "^7.0.0", "eslint-plugin-markdown": "^3.0.0", "grunt": "^1.0.4", @@ -62,8 +65,7 @@ "release-it": "^15.0.0", "rollup": "^3.17.2", "rollup-plugin-filesize": "^10.0.0", - "rollup-plugin-license": "^3.0.0", - "standard": "^17.0.0" + "rollup-plugin-license": "^3.0.0" }, "engines": { "node": ">=16" diff --git a/src/api.mjs b/src/api.mjs index 0c4a5e08..6a33fbde 100644 --- a/src/api.mjs +++ b/src/api.mjs @@ -1,9 +1,8 @@ -/* eslint-disable no-var */ import assign from './assign.mjs' import defaultConverter from './converter.mjs' -function init (converter, defaultAttributes) { - function set (name, value, attributes) { +function init(converter, defaultAttributes) { + function set(name, value, attributes) { if (typeof document === 'undefined') { return } @@ -47,7 +46,7 @@ function init (converter, defaultAttributes) { name + '=' + converter.write(value, name) + stringifiedAttributes) } - function get (name) { + function get(name) { if (typeof document === 'undefined' || (arguments.length && !name)) { return } @@ -67,7 +66,9 @@ function init (converter, defaultAttributes) { if (name === found) { break } - } catch (e) {} + } catch (e) { + // Do nothing... + } } return name ? jar[name] : jar @@ -101,4 +102,3 @@ function init (converter, defaultAttributes) { } export default init(defaultConverter, { path: '/' }) -/* eslint-enable no-var */ diff --git a/src/assign.mjs b/src/assign.mjs index 2934ff37..fed18c12 100644 --- a/src/assign.mjs +++ b/src/assign.mjs @@ -1,4 +1,3 @@ -/* eslint-disable no-var */ export default function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] @@ -8,4 +7,3 @@ export default function (target) { } return target } -/* eslint-enable no-var */ diff --git a/src/converter.mjs b/src/converter.mjs index 7cd10cf2..6ad68a01 100644 --- a/src/converter.mjs +++ b/src/converter.mjs @@ -1,4 +1,3 @@ -/* eslint-disable no-var */ export default { read: function (value) { if (value[0] === '"') { @@ -13,4 +12,3 @@ export default { ) } } -/* eslint-enable no-var */ diff --git a/test/encoding.js b/test/encoding.js index 0a2e8297..ef32136e 100644 --- a/test/encoding.js +++ b/test/encoding.js @@ -1,5 +1,4 @@ /* global QUnit, lifecycle, using */ -/* eslint-disable no-var */ QUnit.module('cookie-value', lifecycle) @@ -1169,5 +1168,3 @@ QUnit.test('cookie-name - 4 bytes characters', function (assert) { ) }) }) - -/* eslint-enable no-var */ diff --git a/test/missing_semicolon.html b/test/missing_semicolon.html index 9e24861c..3de3a9d6 100644 --- a/test/missing_semicolon.html +++ b/test/missing_semicolon.html @@ -6,7 +6,7 @@