Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eslint-config package #168

Merged
merged 6 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/eslint-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: "Verify eslint-config build"
on:
pull_request:
paths:
- "packages/eslint-config/**.ts"
- "packages/eslint-config/**.json"
push:
branches:
- master

jobs:
verify:
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./packages/eslint-config

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
cache: "yarn"
- name: Install dependencies
run: yarn install --immutable
- name: Check format
run: yarn format:check
- name: Check lint
run: yarn lint --max-warnings=0
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
2,
"always",
[
"eslint-config",
"pool-deployment",
"pool-math",
"pool-playground",
Expand Down
11 changes: 11 additions & 0 deletions packages/eslint-config/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["@swim-io/eslint-config/js-only"],
"env": {
"node": true
},
"rules": {
"functional/immutable-data": "off",
"import/no-commonjs": "off",
"import/unambiguous": "off"
}
}
2 changes: 2 additions & 0 deletions packages/eslint-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
types/
41 changes: 41 additions & 0 deletions packages/eslint-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ESLint config

Shared default ESLint configuration for Swim TS projects.

## Installation

Install the package:

```sh
npm install --save-dev @swim-io/eslint-config
```

Install the required peer dependencies:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we go for typescript only projects can't we include these as dependencies (and the typescript specific ones). Wouldn't that be more convenient?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not (I went down several tunnels in this rabbit hole). First of all plugins and configs are treated very differently and get resolved differently. ESLint says this:

If your shareable config depends on a plugin, you should also specify it as a peerDependency (plugins will be loaded relative to the end user’s project, so the end user is required to install the plugins they need). However, if your shareable config depends on a third-party parser or another shareable config, you can specify these packages as dependencies.

However, we can't actually do that with the prettier config because it's an optional dependency of eslint-plugin-prettier: prettier/eslint-plugin-prettier#451. Which means we have to include it as a dependency of whatever loads the plugin if I understand correctly (also didn't get it to work any other way).

I also couldn't get the TS parser to work as a dependency, and I didn't work out why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they're working on changing all of this but this issue has been open since 2015: eslint/eslint#3458

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an example of a similar package we used in my previous job, see https://github.com/denis-sokolov/eslint-plugin/blob/master/package.json but I didn't set it up myself so I'm missing the little details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was somewhat helpful: https://zaicevas.com/blog/how-eslint-resolves-plugins-and-shareable-configs
It might well be that it's yarn that won't let us break the rules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we'll add postinstall script that will automatically install peer dependencies?

Copy link
Collaborator Author

@wormat wormat Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of Yarn v2, Yarn won't run this automatically: https://yarnpkg.com/getting-started/migration#explicitly-call-the-pre-and-post-scripts
But that could be a good thing! I think it's also OK as it is, since we only need to add them once per package.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually maybe it does do that for dependencies otherwise everything would probably break.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is even possible on reflection. The postinstall script will run in the scope of the dependency, but we actually need to add them to the consuming package.


```sh
npm install --save-dev eslint eslint-config-prettier eslint-plugin-functional eslint-plugin-import eslint-plugin-jest eslint-plugin-prettier
```

For TS projects also install these optional dependencies:
nicomiicro marked this conversation as resolved.
Show resolved Hide resolved

```sh
npm install --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-deprecation
```

## Usage

In TS projects extend from the default config in your ESLint configuration file:

```json
{
"extends": ["@swim-io"]
}
```

In JS projects extend from the JS-only config:

```json
{
"extends": ["@swim-io/eslint-config/js-only"]
}
```
45 changes: 45 additions & 0 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = {
plugins: ["@typescript-eslint", "deprecation"],
extends: [
"@swim-io/eslint-config/js-only",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/typescript",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
},
rules: {
// @typescript-eslint
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/prefer-readonly-parameter-types": "off",

// deprecation
"deprecation/deprecation": "warn",
},
overrides: [
{
files: ["**/*.test.ts"],
env: {
node: true,
},
rules: {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
},
},
],
};
47 changes: 47 additions & 0 deletions packages/eslint-config/js-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
root: true,
plugins: ["functional", "import", "jest", "prettier"],
extends: [
"eslint:recommended",
"prettier",
"plugin:prettier/recommended",
"plugin:import/recommended",
"plugin:functional/external-recommended",
"plugin:functional/no-mutations",
"plugin:jest/recommended",
],
env: {
"jest/globals": true,
},
rules: {
// Vanilla ESLint
"no-console": ["warn", { allow: ["error", "info", "table", "warn"] }],
"no-undef": "error",
"sort-imports": ["error", { ignoreDeclarationSort: true }],

// functional
"functional/immutable-data": ["error", { ignorePattern: "this" }],
"functional/no-let": "off",
"functional/prefer-readonly-type": ["error", { ignoreClass: "fieldsOnly" }],

// import
"import/extensions": ["error", "always", { ts: "never", tsx: "never" }],
"import/first": "error",
"import/newline-after-import": "error",
"import/no-absolute-path": "error",
"import/no-amd": "error",
"import/no-commonjs": "error",
"import/no-cycle": "error",
"import/no-deprecated": "error",
"import/no-dynamic-require": "error",
"import/no-mutable-exports": "error",
"import/no-self-import": "error",
"import/no-unused-modules": "error",
"import/no-useless-path-segments": "error",
"import/order": [
"error",
{ alphabetize: { order: "asc" }, "newlines-between": "always" },
],
"import/unambiguous": "error",
},
};
51 changes: 51 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@swim-io/eslint-config",
"version": "0.2.0",
"description": "Shared default ESLint configuration for Swim TS projects.",
"main": "index.js",
"files": [
"*.js",
"*.md"
],
"repository": {
"type": "git",
"url": "https://github.com/swim-io/swim/tree/master/packages/eslint-config"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"scripts": {
"format": "prettier --write \"*.js\"",
"format:check": "prettier --check index.js",
"lint": "eslint \"./*.js\"",
"lint:fix": "eslint --fix \"./*.js\"",
"test": "echo \"Error: no test specified\" && exit 1",
"verify": "yarn format:check && yarn lint",
"prepare": "yarn verify"
},
"peerDependencies": {
"eslint": ">= 8.19",
"eslint-config-prettier": "^8.5",
"eslint-plugin-functional": "^4.2",
"eslint-plugin-import": "^2.26",
"eslint-plugin-jest": "^26.5",
"eslint-plugin-prettier": "^4.2"
},
"optionalDependencies": {
"@typescript-eslint/eslint-plugin": "^5.30",
"@typescript-eslint/parser": "^5.30",
"eslint-plugin-deprecation": "^1.3"
},
"devDependencies": {
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5",
"eslint-plugin-deprecation": "^1.3.2",
"eslint-plugin-functional": "^4.2.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.2",
"prettier": "^2.7.1"
}
}
80 changes: 1 addition & 79 deletions packages/pool-math/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,81 +1,3 @@
{
"root": true,
"plugins": [
"@typescript-eslint",
"deprecation",
"functional",
"import",
"jest",
"prettier"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier",
"plugin:prettier/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:functional/external-recommended",
"plugin:functional/no-mutations",
"plugin:jest/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"env": {
"jest/globals": true
},
"rules": {
"no-console": ["warn", { "allow": ["error", "info", "table", "warn"] }],
"no-undef": "error",
"sort-imports": ["error", { "ignoreDeclarationSort": true }],
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
// "deprecation/deprecation": "warn",
"functional/immutable-data": ["error", { "ignorePattern": "this" }],
"functional/no-let": "off",
"functional/prefer-readonly-type": [
"error",
{ "ignoreClass": "fieldsOnly" }
],
"import/extensions": ["error", "always", { "ts": "never", "tsx": "never" }],
"import/first": "error",
"import/newline-after-import": "error",
"import/no-absolute-path": "error",
"import/no-amd": "error",
"import/no-commonjs": "error",
"import/no-cycle": "error",
"import/no-deprecated": "error",
"import/no-dynamic-require": "error",
"import/no-mutable-exports": "error",
"import/no-self-import": "error",
"import/no-unused-modules": "error",
"import/no-useless-path-segments": "error",
"import/order": [
"error",
{ "alphabetize": { "order": "asc" }, "newlines-between": "always" }
],
"import/unambiguous": "error"
},
"overrides": [
{
"files": ["**/*.test.ts"],
"rules": {
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off"
}
}
]
"extends": ["@swim-io"]
}
9 changes: 5 additions & 4 deletions packages/pool-math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
"decimal.js": "^10.3.1"
},
"devDependencies": {
"@swim-io/eslint-config": "workspace:^",
"@types/jest": "^28.1.3",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"eslint": "^8.18.0",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-deprecation": "^1.3.2",
"eslint-plugin-functional": "^4.2.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-prettier": "^4.1.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^28.1.1",
"prettier": "^2.7.1",
"ts-jest": "^28.0.5",
Expand Down