Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Code style/formatting: Prettier + Airbnb ESLint preset (#146)
Browse files Browse the repository at this point in the history
The purpose of this change is to move toward a more idiomatic code style, and automate as much of it as possible. What's in the change:

- Adds automatic formatting provided by [Prettier](https://prettier.io/)
- Consolidates all ESLint configs into one file via `overrides`
- Extends [Airbnb's ESLint config](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb), a commonly used, opinionated set of rules
  - Overrides some of those rules to be warnings where they're not automatically fixable; there are _a lot of warnings_, the goal is to fix them wherever we touch code with warnings
  - Manually fixes a very small portion of those warnings where it was trivial and obviously safe
- Extends Prettier's ESLint config to enable checking Prettier rules (e.g. while editing in VSCode and in CI)
- Applies automatic Prettier and ESLint formatting on save in VSCode

I've validated that transforms behave the same before and after this change, with all of the forms under test/forms as well as those from Enketo Core.
  • Loading branch information
eyelidlessness authored Apr 21, 2022
1 parent 9b2e08d commit b3c8d07
Show file tree
Hide file tree
Showing 27 changed files with 5,099 additions and 2,819 deletions.
83 changes: 41 additions & 42 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
"env": {
"es6": true,
"browser": false,
"node": true,
"commonjs": false
"node": true
},
"globals": {
"Promise": true
},
"extends": "eslint:recommended",
"plugins": [
"jsdoc"
],
"extends": ["airbnb", "prettier"],
"plugins": ["chai-friendly", "jsdoc", "prettier", "unicorn"],
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2019
Expand All @@ -24,40 +21,42 @@
}
},
"rules": {
"indent": ["warn", 4, { "SwitchCase": 1 }],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"no-console": ["warn", { "allow": ["error", "deprecate", "warn"] }],
"eol-last": ["error", "always"],
"padding-line-between-statements": ["warn", { "blankLine": "always", "prev": "*", "next": "return" }],
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}],
"space-in-parens": ["error", "always"],
"space-infix-ops": "error",
"array-bracket-spacing": ["error", "always"],
"no-implied-eval": "error",
"object-curly-spacing": ["error", "always"],
"jsdoc/check-examples": 1,
"jsdoc/check-param-names": 1,
"jsdoc/check-tag-names": 1,
"jsdoc/check-types": 1,
"jsdoc/newline-after-description": ["warn", "always"],
"jsdoc/no-undefined-types": 1,
"jsdoc/require-description": 0,
"jsdoc/require-description-complete-sentence": 0,
"jsdoc/require-example": 0,
"jsdoc/require-hyphen-before-param-description": 1,
"jsdoc/require-param": 1,
"jsdoc/require-param-description": 1,
"jsdoc/require-param-name": 1,
"jsdoc/require-param-type": 1,
"jsdoc/require-returns-check": 1,
"jsdoc/require-returns-description": 1,
"jsdoc/require-returns-type": 1,
"jsdoc/valid-types": 1
}
"prettier/prettier": "error",

"guard-for-in": "warn",
"import/extensions": "warn",
"import/order": "warn",
"no-param-reassign": "warn",
"no-restricted-syntax": [
"warn",
"ForInStatement",
"LabeledStatement",
"WithStatement"
],
"no-shadow": "warn",
"no-underscore-dangle": "warn",
"no-use-before-define": [
"warn",
{
"functions": false
}
],
"prefer-destructuring": "warn"
},
"overrides": [
{
"files": ["./.github/**/*.md", "./*.md"],
"parser": "markdown-eslint-parser",
"rules": {
"prettier/prettier": ["error", { "parser": "markdown" }]
}
},

{
"files": ["./test/**/*.js"],
"env": {
"mocha": true
}
}
]
}
60 changes: 30 additions & 30 deletions .github/workflows/npmjs.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
name: npmjs

on:
push:
pull_request:
release:
types:
- published
push:
pull_request:
release:
types:
- published

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '14', '16' ]
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
id: cache
with:
path: node_modules
key: ${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
registry-url: https://registry.npmjs.org/
- run: npm install -g npm@^6
- if: steps.cache.outputs.cache-hit != 'true'
run: npm ci
- run: npm test
- run: npm run build-docs
- if: github.event_name == 'release' && github.event.action == 'published' && matrix.node == '16'
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['14', '16']
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
id: cache
with:
path: node_modules
key: ${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
registry-url: https://registry.npmjs.org/
- run: npm install -g npm@^6
- if: steps.cache.outputs.cache-hit != 'true'
run: npm ci
- run: npm test
- run: npm run build-docs
- if: github.event_name == 'release' && github.event.action == 'published' && matrix.node == '16'
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore artifacts:

.nyc*
*/node_modules/*
docs/*
test-coverage/*
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json.schemastore.org/prettierrc",
"singleQuote": true,
"trailingComma": "es5"
}
9 changes: 2 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
{
"name": "Test (watch + debug)",
"request": "launch",
"runtimeArgs": [
"run-script",
"test:watch"
],
"runtimeArgs": ["run-script", "test:watch"],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node",
"sourceMaps": true,
"console": "integratedTerminal"
Expand Down
26 changes: 23 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
{
// Formatting
"eslint.enable": true,
"eslint.options": {
"rules": {
"no-debugger": 1 // Warn rather than error in-editor
}
"extends": ["../.eslintrc.json"]
},
"eslint.nodePath": "../node_modules/.bin",
"eslint.validate": ["markdown", "md"],
"prettier.configPath": ".prettierrc.json",
"prettier.ignorePath": ".prettierignore",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": [
"source.formatDocument",
"source.fixAll.eslint"
],
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},

// Code navigation
"javascript.referencesCodeLens.enabled": true
}
Loading

0 comments on commit b3c8d07

Please sign in to comment.