@haltcase/style 1.0.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/LICENSE.md +382 -0
  2. package/eslint/_base.js +52 -0
  3. package/eslint/browser.js +8 -0
  4. package/eslint/cjs.js +21 -0
  5. package/eslint/constants.js +13 -0
  6. package/eslint/jest.js +28 -0
  7. package/eslint/next.js +27 -0
  8. package/eslint/node.js +8 -0
  9. package/eslint/playwright-test.js +8 -0
  10. package/eslint/react.js +18 -0
  11. package/eslint/rules/best-practice.js +239 -0
  12. package/eslint/rules/es2015.js +71 -0
  13. package/eslint/rules/import.js +85 -0
  14. package/eslint/rules/jest.js +28 -0
  15. package/eslint/rules/jsx-a11y.js +20 -0
  16. package/eslint/rules/playwright-test.js +28 -0
  17. package/eslint/rules/possible-errors.js +47 -0
  18. package/eslint/rules/react.js +117 -0
  19. package/eslint/rules/simple-import-sort.js +23 -0
  20. package/eslint/rules/stylistic.js +86 -0
  21. package/eslint/rules/tsdoc.js +17 -0
  22. package/eslint/rules/typescript/extension.js +45 -0
  23. package/eslint/rules/typescript/import.js +22 -0
  24. package/eslint/rules/typescript/index.js +89 -0
  25. package/eslint/rules/typescript/strict.js +18 -0
  26. package/eslint/rules/unicorn.js +677 -0
  27. package/eslint/rules/variables.js +36 -0
  28. package/eslint/typescript.js +26 -0
  29. package/eslint/utils/require-package.js +44 -0
  30. package/package.json +111 -0
  31. package/prettier/index.js +21 -0
  32. package/readme.md +228 -0
  33. package/typescript/tsconfig.base.json +13 -0
  34. package/typescript/tsconfig.bundler.json +13 -0
  35. package/typescript/tsconfig.next.json +26 -0
  36. package/typescript/tsconfig.node.json +4 -0
  37. package/typescript/tsconfig.node18.json +13 -0
  38. package/typescript/tsconfig.node20.json +13 -0
  39. package/typescript/tsconfig.web.json +8 -0
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ const {
4
+ levels: { warn, error }
5
+ } = require("../constants");
6
+
7
+ module.exports = {
8
+ rules: {
9
+ /**
10
+ * Disallow labels that share a name with a variable.
11
+ *
12
+ * 🚫 Not fixable - https://eslint.org/docs/rules/no-label-var
13
+ */
14
+ "no-label-var": error,
15
+ /**
16
+ * Disallow initializing variables to `undefined`.
17
+ *
18
+ * 🔧 Fixable - https://eslint.org/docs/rules/no-undef-init
19
+ */
20
+ "no-undef-init": warn,
21
+ /**
22
+ * Disallow unused variables.
23
+ *
24
+ * 🚫 Not fixable - https://eslint.org/docs/rules/no-unused-vars
25
+ */ "no-unused-vars": [
26
+ error,
27
+ {
28
+ args: "after-used",
29
+ argsIgnorePattern: "^_",
30
+ ignoreRestSiblings: true,
31
+ vars: "all",
32
+ varsIgnorePattern: "^_"
33
+ }
34
+ ]
35
+ }
36
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+
3
+ const { typescriptFiles } = require("./constants");
4
+ const checkForPackage = require("./utils/require-package");
5
+
6
+ checkForPackage("typescript", "typescript");
7
+
8
+ module.exports = {
9
+ overrides: [
10
+ {
11
+ files: typescriptFiles,
12
+ extends: [
13
+ "plugin:@typescript-eslint/recommended",
14
+ "plugin:@typescript-eslint/recommended-requiring-type-checking",
15
+ "plugin:@typescript-eslint/strict",
16
+ "plugin:import/typescript",
17
+ "prettier",
18
+ require.resolve("./rules/typescript"),
19
+ require.resolve("./rules/typescript/extension"),
20
+ require.resolve("./rules/typescript/import"),
21
+ require.resolve("./rules/typescript/strict"),
22
+ require.resolve("./rules/tsdoc")
23
+ ]
24
+ }
25
+ ]
26
+ };
@@ -0,0 +1,44 @@
1
+ // @ts-check
2
+ "use strict";
3
+
4
+ const packageJson = require("../../package.json");
5
+
6
+ /**
7
+ * @returns {string} The name of the package manager.
8
+ */
9
+ const readPackageManager = () => {
10
+ const match = process.env.npm_config_user_agent?.match(/^(?<pm>\w+)\//);
11
+ return match?.groups ? match.groups?.pm : "npm";
12
+ };
13
+
14
+ /**
15
+ * @param {string} configName
16
+ * @param {string} packageName
17
+ */
18
+ module.exports = (configName, packageName) => {
19
+ try {
20
+ require.resolve(packageName);
21
+ } catch {
22
+ const packageManager = readPackageManager();
23
+ const command =
24
+ packageManager === "yarn" || packageManager === "pnpm"
25
+ ? "add"
26
+ : "install";
27
+
28
+ // eslint-disable-next-line unicorn/prevent-abbreviations
29
+ const dev = packageManager === "yarn" ? "--dev" : "--save-dev";
30
+
31
+ /* eslint-disable no-console */
32
+ console.error(
33
+ `The \`${configName}\` config requires an optional peer dependency, which has not been installed.`
34
+ );
35
+ console.error();
36
+ console.error("To install it, run:");
37
+ console.error(
38
+ `- ${packageManager} ${command} ${dev} ${packageName}@${packageJson.peerDependencies[packageName]}`
39
+ );
40
+ /* eslint-enable no-console */
41
+
42
+ throw new Error(`Missing dependency encountered: ${packageName}`);
43
+ }
44
+ };
package/package.json ADDED
@@ -0,0 +1,111 @@
1
+ {
2
+ "name": "@haltcase/style",
3
+ "version": "1.0.0-canary.1",
4
+ "description": "haltcase's web ecosystem style guide & configurations",
5
+ "keywords": [
6
+ "eslint",
7
+ "prettier",
8
+ "typescript",
9
+ "style",
10
+ "eslint-config"
11
+ ],
12
+ "homepage": "https://github.com/haltcase/style#readme",
13
+ "bugs": "https://github.com/haltcase/style/issues",
14
+ "repository": "https://github.com/haltcase/style.git",
15
+ "license": "MPL-2.0",
16
+ "author": "Bo Lingen <bo@lingen.life> (https://bolingen.me)",
17
+ "exports": {
18
+ "./eslint/rules/*": "./eslint/rules/*.js",
19
+ "./eslint/rules/*.js": "./eslint/rules/*.js",
20
+ "./eslint/browser": "./eslint/browser.js",
21
+ "./eslint/cjs": "./eslint/cjs.js",
22
+ "./eslint/jest": "./eslint/jest.js",
23
+ "./eslint/next": "./eslint/next.js",
24
+ "./eslint/node": "./eslint/node.js",
25
+ "./eslint/playwright-test": "./eslint/playwright-test.js",
26
+ "./eslint/react": "./eslint/react.js",
27
+ "./eslint/typescript": "./eslint/typescript.js",
28
+ "./prettier": "./prettier/index.js",
29
+ "./typescript/base": "./typescript/tsconfig.base.json",
30
+ "./typescript/bundler": "./typescript/tsconfig.bundler.json",
31
+ "./typescript/next": "./typescript/tsconfig.next.json",
32
+ "./typescript/node": "./typescript/tsconfig.node.json",
33
+ "./typescript/node18": "./typescript/tsconfig.node18.json",
34
+ "./typescript/node20": "./typescript/tsconfig.node20.json",
35
+ "./typescript/web": "./typescript/tsconfig.web.json"
36
+ },
37
+ "files": [
38
+ "eslint",
39
+ "prettier",
40
+ "typescript"
41
+ ],
42
+ "scripts": {
43
+ "eslint-check": "eslint --max-warnings=0 .",
44
+ "format": "eslint --fix . && prettier --write .",
45
+ "prepare": "husky install",
46
+ "prettier-check": "prettier --check .",
47
+ "semantic-release": "semantic-release",
48
+ "test": "echo \"Error: no test specified\" && exit 1"
49
+ },
50
+ "prettier": "./prettier",
51
+ "dependencies": {
52
+ "@babel/core": "^7.22.1",
53
+ "@babel/eslint-parser": "^7.21.8",
54
+ "@rushstack/eslint-patch": "^1.3.0",
55
+ "@typescript-eslint/eslint-plugin": "^5.59.9",
56
+ "@typescript-eslint/parser": "^5.59.9",
57
+ "eslint-config-prettier": "^8.8.0",
58
+ "eslint-import-resolver-alias": "^1.1.2",
59
+ "eslint-import-resolver-typescript": "^3.5.5",
60
+ "eslint-plugin-eslint-comments": "^3.2.0",
61
+ "eslint-plugin-import": "^2.27.5",
62
+ "eslint-plugin-jest": "^27.2.1",
63
+ "eslint-plugin-jsx-a11y": "^6.7.1",
64
+ "eslint-plugin-playwright": "^0.12.0",
65
+ "eslint-plugin-react": "^7.32.2",
66
+ "eslint-plugin-react-hooks": "^4.6.0",
67
+ "eslint-plugin-simple-import-sort": "^10.0.0",
68
+ "eslint-plugin-testing-library": "^5.11.0",
69
+ "eslint-plugin-tsdoc": "^0.2.17",
70
+ "eslint-plugin-unicorn": "^47.0.0",
71
+ "prettier-plugin-packagejson": "^2.4.3"
72
+ },
73
+ "devDependencies": {
74
+ "@commitlint/cli": "^17.6.5",
75
+ "@commitlint/config-conventional": "^17.6.5",
76
+ "@semantic-release/git": "^10.0.1",
77
+ "eslint": "^8.42.0",
78
+ "husky": "^8.0.3",
79
+ "prettier": "^2.8.8",
80
+ "pretty-quick": "^3.1.3",
81
+ "semantic-release": "^19.0.5",
82
+ "typescript": "^5.1.3"
83
+ },
84
+ "peerDependencies": {
85
+ "@next/eslint-plugin-next": "^13.4.4",
86
+ "eslint": "^8.42.0",
87
+ "prettier": "^2.8.8",
88
+ "typescript": "^5.1.3"
89
+ },
90
+ "peerDependenciesMeta": {
91
+ "@next/eslint-plugin-next": {
92
+ "optional": true
93
+ },
94
+ "eslint": {
95
+ "optional": true
96
+ },
97
+ "prettier": {
98
+ "optional": true
99
+ },
100
+ "typescript": {
101
+ "optional": true
102
+ }
103
+ },
104
+ "packageManager": "pnpm@8.6.1",
105
+ "engines": {
106
+ "node": "20"
107
+ },
108
+ "publishConfig": {
109
+ "access": "public"
110
+ }
111
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Some of Prettier's defaults can be overridden by an EditorConfig file. We
5
+ * define those here to ensure that doesn't happen.
6
+ *
7
+ * See: https://github.com/prettier/prettier/blob/main/docs/configuration.md#editorconfig
8
+ */
9
+ const overridableDefaults = {
10
+ endOfLine: "lf",
11
+ printWidth: 80,
12
+ tabWidth: 2,
13
+ useTabs: true
14
+ };
15
+
16
+ module.exports = {
17
+ ...overridableDefaults,
18
+ singleQuote: false,
19
+ trailingComma: "none",
20
+ plugins: ["prettier-plugin-packagejson"]
21
+ };
package/readme.md ADDED
@@ -0,0 +1,228 @@
1
+ # `@haltcase/style` &middot; [![Version](https://flat.badgen.net/npm/v/@haltcase/style)](https://www.npmjs.com/package/@haltcase/style) [![License](https://flat.badgen.net/npm/license/@haltcase/style)](https://www.npmjs.com/package/@haltcase/style) [![GitHub Actions](https://flat.badgen.net/github/checks/haltcase/style)](https://github.com/haltcase/style/actions)
2
+
3
+ > Style guide and configurations for tools in the web ecosystem.
4
+
5
+ - [Prettier](#prettier)
6
+ - [ESLint](#eslint)
7
+ - [TypeScript](#typescript)
8
+
9
+ > This package originated from [Vercel's style guide](https://github.com/vercel/style-guide).
10
+
11
+ ## installation
12
+
13
+ Install `@haltcase/style` with your preferred package manager:
14
+
15
+ ```sh
16
+ # npm
17
+ npm i --save-dev @haltcase/style
18
+
19
+ # pnpm
20
+ pnpm i --save-dev @haltcase/style
21
+
22
+ # yarn
23
+ yarn add --dev @haltcase/style
24
+ ```
25
+
26
+ > Some ESLint scenarios require additional peer dependencies.
27
+ > See the [ESLint](#eslint) section.
28
+
29
+ ## usage
30
+
31
+ ### Prettier
32
+
33
+ > **Note**
34
+ > Prettier is a peer dependency you'll need to install
35
+ > at the root of your project.
36
+ >
37
+ > See: https://prettier.io/docs/en/install.html
38
+
39
+ Add the following in `package.json`:
40
+
41
+ ```json
42
+ {
43
+ "prettier": "@haltcase/style/prettier"
44
+ }
45
+ ```
46
+
47
+ ### ESLint
48
+
49
+ > **Note**
50
+ > ESLint is a peer dependency you'll need to install
51
+ > at the root of your project.
52
+ >
53
+ > See: https://eslint.org/docs/user-guide/getting-started#installation-and-usage
54
+
55
+ Several ESLint configs are available which can be combined.
56
+
57
+ Start with at least one of these base configs, which should always be first in `extends`:
58
+
59
+ - `@haltcase/style/eslint/browser`
60
+ - `@haltcase/style/eslint/node`
61
+
62
+ > **Note**
63
+ > You can scope configs so they only target specific files.
64
+ >
65
+ > See: [Scoped configuration with `overrides`](#scoped-configuration-with-overrides).
66
+
67
+ The following additional configs are available:
68
+
69
+ - `@haltcase/style/eslint/jest`
70
+ - `@haltcase/style/eslint/next` (requires `@next/eslint-plugin-next` to be installed at the same version as `next`)
71
+ - `@haltcase/style/eslint/playwright-test`
72
+ - `@haltcase/style/eslint/react`
73
+ - `@haltcase/style/eslint/typescript` (requires `typescript` to be installed and [additional configuration](#configuring-eslint-for-typescript))
74
+
75
+ Be sure to use `require.resolve` to reference these configs &mdash; ESLint requires absolute paths ([eslint/eslint#9188](https://github.com/eslint/eslint/issues/9188)).
76
+
77
+ For example, to use the shared ESLint config(s) in a Next.js project, set the
78
+ following in `.eslintrc.js`.
79
+
80
+ ```js
81
+ module.exports = {
82
+ extends: [
83
+ require.resolve("@haltcase/style/eslint/browser"),
84
+ require.resolve("@haltcase/style/eslint/react"),
85
+ require.resolve("@haltcase/style/eslint/next")
86
+ ]
87
+ };
88
+ ```
89
+
90
+ #### Configuring ESLint for TypeScript
91
+
92
+ Some of the rules enabled in the TypeScript config require additional type
93
+ information, you'll need to provide the path to your `tsconfig.json`.
94
+
95
+ For more information, see: https://typescript-eslint.io/docs/linting/type-linting
96
+
97
+ ```js
98
+ const { resolve } = require("path");
99
+
100
+ const project = resolve(__dirname, "tsconfig.json");
101
+
102
+ module.exports = {
103
+ root: true,
104
+ extends: [
105
+ require.resolve("@haltcase/style/eslint/node"),
106
+ require.resolve("@haltcase/style/eslint/typescript")
107
+ ],
108
+ parserOptions: {
109
+ project
110
+ },
111
+ settings: {
112
+ "import/resolver": {
113
+ typescript: {
114
+ project
115
+ }
116
+ }
117
+ }
118
+ };
119
+ ```
120
+
121
+ #### Configuring custom components for `jsx-a11y`
122
+
123
+ It's common practice for React apps to have shared components like `Button`
124
+ that wrap native elements. You can pass this information along to `jsx-a11y`
125
+ via the `components` setting.
126
+
127
+ The below list is not exhaustive.
128
+
129
+ ```js
130
+ module.exports = {
131
+ root: true,
132
+ extends: [require.resolve("@haltcase/style/eslint/react")],
133
+ settings: {
134
+ "jsx-a11y": {
135
+ components: {
136
+ Article: "article",
137
+ Button: "button",
138
+ Image: "img",
139
+ Input: "input",
140
+ Link: "a",
141
+ Video: "video"
142
+ }
143
+ }
144
+ }
145
+ };
146
+ ```
147
+
148
+ #### Scoped configuration with `overrides`
149
+
150
+ ESLint configs can be scoped to include/exclude specific paths. This ensures
151
+ that rules don't "leak" into places where those rules don't apply.
152
+
153
+ In this example, Jest rules are only being applied to files matching Jest's
154
+ default test match pattern.
155
+
156
+ ```js
157
+ module.exports = {
158
+ extends: [require.resolve("@haltcase/style/eslint/node")],
159
+ overrides: [
160
+ {
161
+ files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
162
+ extends: [require.resolve("@haltcase/style/eslint/jest")]
163
+ }
164
+ ]
165
+ };
166
+ ```
167
+
168
+ ##### A note on file extensions
169
+
170
+ By default, all TypeScript rules are scoped to files ending with `.ts` and
171
+ `.tsx`.
172
+
173
+ However, when using overrides, file extensions must be included or ESLint will
174
+ only include `.js` files.
175
+
176
+ ```js
177
+ module.exports = {
178
+ overrides: [
179
+ {
180
+ files: ["directory/**/*.[jt]s?(x)"],
181
+ rules: {
182
+ "my-rule": "off"
183
+ }
184
+ }
185
+ ]
186
+ };
187
+ ```
188
+
189
+ ### TypeScript
190
+
191
+ Several Typescript configs are available to cover various scenarios:
192
+
193
+ | Name | Description |
194
+ | ------------------------------------ | ---------------------------------------------------------------------------------------------------- |
195
+ | `@haltcase/style/typescript/base` | Baseline config, intended to be extended from. |
196
+ | `@haltcase/style/typescript/bundler` | For use in bundled projects, most commonly [Vite](https://vitejs.dev/) + [React](https://react.dev). |
197
+ | `@haltcase/style/typescript/next` | For use in [Next.js](https://nextjs.org/) projects. |
198
+ | `@haltcase/style/typescript/node` | Default Node config, currently targeting Node 20. |
199
+ | `@haltcase/style/typescript/node18` | For projects targeting Node 18. |
200
+ | `@haltcase/style/typescript/node20` | For projects targeting Node 20. |
201
+ | `@haltcase/style/typescript/web` | For use in web projects. |
202
+
203
+ Typically, you'll only need to extend from one of these:
204
+
205
+ ```json
206
+ {
207
+ "extends": "@haltcase/style/typescript/next"
208
+ }
209
+ ```
210
+
211
+ You could also combine them with an `extends` array in [Typescript 5+](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-rc/#supporting-multiple-configuration-files-in-extends):
212
+
213
+ ```json
214
+ {
215
+ "extends": [
216
+ "@haltcase/style/typescript/node",
217
+ "@haltcase/style/typescript/web"
218
+ ]
219
+ }
220
+ ```
221
+
222
+ > **Note**
223
+ > Run `tsc --showConfig` to see the result of the combined configs.
224
+
225
+ ## contributing
226
+
227
+ Please read our [contributing](https://github.com/haltcase/style/blob/main/CONTRIBUTING.md)
228
+ guide before creating a pull request.
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "esModuleInterop": true,
5
+ "forceConsistentCasingInFileNames": true,
6
+ "incremental": true,
7
+ "isolatedModules": true,
8
+ "noFallthroughCasesInSwitch": true,
9
+ "noUncheckedIndexedAccess": true,
10
+ "skipLibCheck": true,
11
+ "strict": true
12
+ }
13
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
6
+ "jsx": "react-jsx",
7
+ "module": "ESNext",
8
+ "moduleResolution": "bundler",
9
+ "noEmit": true,
10
+ "resolveJsonModule": true,
11
+ "target": "ES2015"
12
+ }
13
+ }
@@ -0,0 +1,26 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "allowJs": true,
5
+ "esModuleInterop": true,
6
+ "forceConsistentCasingInFileNames": true,
7
+ "incremental": true,
8
+ "isolatedModules": true,
9
+ "jsx": "preserve",
10
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
11
+ "module": "ESNext",
12
+ "moduleResolution": "node",
13
+ "noEmit": true,
14
+ "resolveJsonModule": true,
15
+ "skipLibCheck": true,
16
+ "strict": true,
17
+ "target": "ES5",
18
+ "plugins": [
19
+ {
20
+ "name": "next"
21
+ }
22
+ ]
23
+ },
24
+ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
25
+ "exclude": ["node_modules"]
26
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.node20.json"
4
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "declaration": true,
6
+ "lib": ["ES2023"],
7
+ "module": "Node16",
8
+ "moduleResolution": "nodenext",
9
+ "resolveJsonModule": true,
10
+ "sourceMap": true,
11
+ "target": "ES2022"
12
+ }
13
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "declaration": true,
6
+ "lib": ["ES2023"],
7
+ "module": "Node16",
8
+ "moduleResolution": "nodenext",
9
+ "resolveJsonModule": true,
10
+ "sourceMap": true,
11
+ "target": "ES2023"
12
+ }
13
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "./tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "module": "commonjs",
6
+ "target": "ES2015"
7
+ }
8
+ }