@dfinity/eslint-config-oisy-wallet 0.0.6 → 0.1.0

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.
package/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright 2021 DFINITY LLC.
189
+ Copyright 2024 DFINITY Stiftung
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
@@ -0,0 +1,13 @@
1
+ import js from "@eslint/js";
2
+ import ts from "typescript-eslint";
3
+ import prettier from "eslint-config-prettier";
4
+ import { eslintRules } from "./eslint.rules.mjs";
5
+ import { eslintIgnore } from "./eslint.ignore.mjs";
6
+
7
+ export const eslintCoreConfig = [
8
+ js.configs.recommended,
9
+ ...ts.configs.recommended,
10
+ prettier,
11
+ ...eslintRules,
12
+ ...eslintIgnore,
13
+ ];
@@ -0,0 +1,21 @@
1
+ import { join } from "node:path";
2
+ import { existsSync } from "node:fs";
3
+ import { fileURLToPath } from "node:url";
4
+ import { nonNullish } from "@dfinity/utils";
5
+ import { includeIgnoreFile } from "@eslint/compat";
6
+
7
+ const resolveGitIgnorePath = () => {
8
+ const gitIgnore = join(process.cwd(), ".gitignore");
9
+
10
+ if (existsSync(gitIgnore)) {
11
+ return fileURLToPath(new URL(gitIgnore, import.meta.url));
12
+ }
13
+
14
+ return undefined;
15
+ };
16
+
17
+ const gitignorePath = resolveGitIgnorePath();
18
+
19
+ export const eslintIgnore = [
20
+ ...(nonNullish(gitignorePath) ? [includeIgnoreFile(gitignorePath)] : []),
21
+ ];
@@ -0,0 +1,34 @@
1
+ import globals from "globals";
2
+ import ts from "typescript-eslint";
3
+ import { join } from "node:path";
4
+ import { existsSync } from "node:fs";
5
+ import { nonNullish } from "@dfinity/utils";
6
+
7
+ const resolveProject = () => {
8
+ const eslintJson = join(process.cwd(), "tsconfig.eslint.json");
9
+
10
+ if (existsSync(eslintJson)) {
11
+ return eslintJson;
12
+ }
13
+
14
+ return join(process.cwd(), "tsconfig.json");
15
+ };
16
+
17
+ export const languageOptions = (extraFileExtensions) => ({
18
+ languageOptions: {
19
+ globals: {
20
+ ...globals.browser,
21
+ ...globals.node,
22
+ NodeJS: true,
23
+ },
24
+
25
+ parser: ts.parser,
26
+ ecmaVersion: 2020,
27
+ sourceType: "module",
28
+
29
+ parserOptions: {
30
+ project: [resolveProject()],
31
+ ...(nonNullish(extraFileExtensions) && { extraFileExtensions }),
32
+ },
33
+ },
34
+ });
@@ -0,0 +1,100 @@
1
+ import localRules from "eslint-plugin-local-rules";
2
+ import preferArrow from "eslint-plugin-prefer-arrow";
3
+ import { join } from "node:path";
4
+ import { existsSync } from "node:fs";
5
+ import { fixupPluginRules, includeIgnoreFile } from "@eslint/compat";
6
+ import _import from "eslint-plugin-import";
7
+ import { fileURLToPath } from "node:url";
8
+ import { nonNullish } from "@dfinity/utils";
9
+
10
+ const resolveGitIgnorePath = () => {
11
+ const gitIgnore = join(process.cwd(), ".gitignore");
12
+
13
+ if (existsSync(gitIgnore)) {
14
+ return fileURLToPath(new URL(gitIgnore, import.meta.url));
15
+ }
16
+
17
+ return undefined;
18
+ };
19
+
20
+ const gitignorePath = resolveGitIgnorePath();
21
+
22
+ export const eslintRules = [
23
+ ...(nonNullish(gitignorePath) ? [includeIgnoreFile(gitignorePath)] : []),
24
+ {
25
+ plugins: {
26
+ "local-rules": localRules,
27
+ "prefer-arrow": preferArrow,
28
+ import: fixupPluginRules(_import),
29
+ },
30
+
31
+ rules: {
32
+ "@typescript-eslint/consistent-type-definitions": "error",
33
+
34
+ // TODO: Disabled for now to allow the migration of ESLint v9 in OISY, as the rule is not yet enforced.
35
+ // It should also be introduced as an optional rule.
36
+ // "@typescript-eslint/consistent-type-imports": "error",
37
+
38
+ "@typescript-eslint/no-inferrable-types": "error",
39
+ "@typescript-eslint/no-unnecessary-type-assertion": "error",
40
+
41
+ "@typescript-eslint/no-unused-vars": [
42
+ "warn",
43
+ {
44
+ argsIgnorePattern: "^_",
45
+ varsIgnorePattern: "^_",
46
+ caughtErrorsIgnorePattern: "^_",
47
+ },
48
+ ],
49
+
50
+ "@typescript-eslint/prefer-nullish-coalescing": "error",
51
+ "@typescript-eslint/prefer-reduce-type-parameter": "error",
52
+ "arrow-body-style": ["warn", "as-needed"],
53
+ curly: "error",
54
+
55
+ // TODO: Disabled for now to allow the migration of ESLint v9 in OISY, as the rule is not yet enforced.
56
+ // It should also be introduced as an optional rule.
57
+ // "func-style": "error",
58
+
59
+ "local-rules/prefer-object-params": "warn",
60
+
61
+ // TODO: Disabled for now to allow the migration of ESLint v9 in OISY, as the rule is not yet enforced.
62
+ // It should also be introduced as an optional rule.
63
+ // "local-rules/use-nullish-checks": "warn",
64
+
65
+ "local-rules/use-option-type-wrapper": "warn",
66
+ "import/no-duplicates": ["error", { "prefer-inline": true }],
67
+ "import/no-relative-parent-imports": "error",
68
+ "no-console": ["error", { allow: ["error", "warn"] }],
69
+ "no-continue": "warn",
70
+ "no-delete-var": "error",
71
+ "no-else-return": ["warn", { allowElseIf: false }],
72
+ "no-unused-vars": "off",
73
+
74
+ // TODO: Disabled for now to allow the migration of ESLint v9 in OISY, as the rule is not yet enforced.
75
+ // It should also be introduced as an optional rule.
76
+ // "prefer-arrow-callback": "error",
77
+
78
+ // TODO: Disabled for now to allow the migration of ESLint v9 in OISY, as the rule is not yet enforced.
79
+ // It should also be introduced as an optional rule.
80
+ // "prefer-arrow/prefer-arrow-functions": [
81
+ // "error",
82
+ // {
83
+ // disallowPrototype: true,
84
+ // singleReturnOnly: false,
85
+ // classPropertiesAllowed: false,
86
+ // },
87
+ // ],
88
+
89
+ "prefer-template": "error",
90
+ "require-await": "error",
91
+ },
92
+ },
93
+ {
94
+ files: ["**/*.ts"],
95
+
96
+ rules: {
97
+ "prefer-const": "error",
98
+ },
99
+ },
100
+ ];
@@ -4,4 +4,5 @@ module.exports = {
4
4
  "prefer-object-params": require("./rules/prefer-object-params.cjs"),
5
5
  "no-relative-imports": require("./rules/no-relative-imports.cjs"),
6
6
  "explicit-non-void-return-type": require("./rules/explicit-non-void-return-type.cjs"),
7
+ "use-nullish-checks": require("./rules/use-nullish-checks.cjs"),
7
8
  };
package/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import { languageOptions } from "./configs/eslint.language.mjs";
2
+ import { eslintCoreConfig } from "./configs/eslint.core.mjs";
3
+
4
+ export default [...eslintCoreConfig, languageOptions()];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dfinity/eslint-config-oisy-wallet",
3
- "version": "0.0.6",
3
+ "version": "0.1.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Shared ESLint configurations from the Oisy Wallet team",
6
6
  "repository": {
@@ -10,23 +10,24 @@
10
10
  "bugs": {
11
11
  "url": "https://github.com/dfinity/eslint-config-oisy-wallet/issues"
12
12
  },
13
- "main": "index.cjs",
13
+ "main": "index.mjs",
14
14
  "files": [
15
15
  "README.md",
16
16
  "LICENSE",
17
- "index.cjs",
18
- "svelte.cjs",
17
+ "index.mjs",
18
+ "svelte.mjs",
19
+ "configs",
19
20
  "eslint-local-rules.cjs",
20
21
  "rules/**/*.cjs"
21
22
  ],
22
23
  "exports": {
23
24
  ".": {
24
- "import": "./index.cjs",
25
- "require": "./index.cjs"
25
+ "import": "./index.mjs",
26
+ "require": "./index.mjs"
26
27
  },
27
28
  "./svelte": {
28
- "import": "./svelte.cjs",
29
- "require": "./svelte.cjs"
29
+ "import": "./svelte.mjs",
30
+ "require": "./svelte.mjs"
30
31
  },
31
32
  "./eslint-local-rules": {
32
33
  "import": "./eslint-local-rules.cjs",
@@ -36,7 +37,7 @@
36
37
  "scripts": {
37
38
  "format": "prettier . --write",
38
39
  "format:check": "prettier --check .",
39
- "lint": "eslint -c index.cjs . --ext .cjs",
40
+ "lint": "eslint .",
40
41
  "test": "vitest --run"
41
42
  },
42
43
  "keywords": [
@@ -56,23 +57,29 @@
56
57
  "best-practices"
57
58
  ],
58
59
  "peerDependencies": {
59
- "@typescript-eslint/eslint-plugin": "^6.20.0",
60
- "eslint": "^8.57.0",
61
- "eslint-config-love": "^84.1.0",
62
- "eslint-config-prettier": "^9.1.0",
63
- "eslint-plugin-import": "^2.29.1",
60
+ "@dfinity/utils": "^2.10.0",
61
+ "@eslint/compat": "^1.2.6",
62
+ "@eslint/eslintrc": "^3.2.0",
63
+ "@eslint/js": "^9.20.0",
64
+ "@typescript-eslint/eslint-plugin": "^8.24.1",
65
+ "eslint": "^9.20.1",
66
+ "eslint-config-love": "^118.0.0",
67
+ "eslint-config-prettier": "^10.0.1",
68
+ "eslint-plugin-import": "^2.31.0",
64
69
  "eslint-plugin-local-rules": "^3.0.2",
65
- "eslint-plugin-n": "^16.6.2",
66
- "eslint-plugin-prettier": "^5.2.1",
67
- "eslint-plugin-promise": "^6.1.1",
68
- "eslint-plugin-svelte": "^2.44.0",
69
- "prettier": "^3.3.3",
70
- "prettier-plugin-organize-imports": "^4.0.0",
70
+ "eslint-plugin-n": "^17.15.1",
71
+ "eslint-plugin-prefer-arrow": "^1.2.3",
72
+ "eslint-plugin-prettier": "^5.2.3",
73
+ "eslint-plugin-promise": "^7.2.1",
74
+ "eslint-plugin-svelte": "^2.46.1",
75
+ "globals": "^15.15.0",
76
+ "prettier": "^3.5.1",
77
+ "prettier-plugin-organize-imports": "^4.1.0",
71
78
  "typescript": "^5.3.3"
72
79
  },
73
80
  "devDependencies": {
74
81
  "@types/node": "^22.7.3",
75
- "@typescript-eslint/rule-tester": "^8.7.0",
76
- "vitest": "^2.1.1"
82
+ "@typescript-eslint/rule-tester": "^8.24.1",
83
+ "vitest": "^3.0.5"
77
84
  }
78
85
  }
@@ -68,17 +68,15 @@ module.exports = {
68
68
  schema: [],
69
69
  },
70
70
 
71
- create(context) {
72
- return {
73
- FunctionDeclaration(node) {
74
- checkReturnType({ node, context });
75
- },
76
- FunctionExpression(node) {
77
- checkReturnType({ node, context });
78
- },
79
- ArrowFunctionExpression(node) {
80
- checkReturnType({ node, context });
81
- },
82
- };
83
- },
71
+ create: (context) => ({
72
+ FunctionDeclaration: (node) => {
73
+ checkReturnType({ node, context });
74
+ },
75
+ FunctionExpression: (node) => {
76
+ checkReturnType({ node, context });
77
+ },
78
+ ArrowFunctionExpression: (node) => {
79
+ checkReturnType({ node, context });
80
+ },
81
+ }),
84
82
  };
@@ -12,18 +12,16 @@ module.exports = {
12
12
  },
13
13
  schema: [],
14
14
  },
15
- create(context) {
16
- return {
17
- ImportDeclaration(node) {
18
- const path = node.source.value;
15
+ create: (context) => ({
16
+ ImportDeclaration: (node) => {
17
+ const path = node.source.value;
19
18
 
20
- if (path.startsWith("./")) {
21
- context.report({
22
- node,
23
- messageId: "noRelativeImports",
24
- });
25
- }
26
- },
27
- };
28
- },
19
+ if (path.startsWith("./")) {
20
+ context.report({
21
+ node,
22
+ messageId: "noRelativeImports",
23
+ });
24
+ }
25
+ },
26
+ }),
29
27
  };
@@ -8,22 +8,20 @@ module.exports = {
8
8
  },
9
9
  schema: [],
10
10
  },
11
- create(context) {
12
- return {
13
- ImportDeclaration(node) {
14
- const filePath = context.getFilename();
11
+ create: (context) => ({
12
+ ImportDeclaration: (node) => {
13
+ const filePath = context.getFilename();
15
14
 
16
- const {
17
- source: { value },
18
- } = node;
15
+ const {
16
+ source: { value },
17
+ } = node;
19
18
 
20
- if (filePath.includes("/api/") && value === "svelte/store") {
21
- context.report({
22
- node,
23
- message: "Importing 'svelte/store' is not allowed in API modules.",
24
- });
25
- }
26
- },
27
- };
28
- },
19
+ if (filePath.includes("/api/") && value === "svelte/store") {
20
+ context.report({
21
+ node,
22
+ message: "Importing 'svelte/store' is not allowed in API modules.",
23
+ });
24
+ }
25
+ },
26
+ }),
29
27
  };
@@ -1,3 +1,5 @@
1
+ const { nonNullish } = require("@dfinity/utils");
2
+
1
3
  module.exports = {
2
4
  meta: {
3
5
  type: "suggestion",
@@ -9,12 +11,10 @@ module.exports = {
9
11
  },
10
12
  schema: [],
11
13
  },
12
- create(context) {
14
+ create: (context) => {
13
15
  const checkForMoreThanOneParameter = (node) => {
14
16
  const parent = node.parent;
15
17
 
16
- const nonNullish = (element) => element !== undefined && element !== null;
17
-
18
18
  // Check if it is a callback for looping methods
19
19
  if (
20
20
  nonNullish(parent) &&
@@ -103,13 +103,13 @@ module.exports = {
103
103
  };
104
104
 
105
105
  return {
106
- FunctionDeclaration(node) {
106
+ FunctionDeclaration: (node) => {
107
107
  checkForMoreThanOneParameter(node);
108
108
  },
109
- FunctionExpression(node) {
109
+ FunctionExpression: (node) => {
110
110
  checkForMoreThanOneParameter(node);
111
111
  },
112
- ArrowFunctionExpression(node) {
112
+ ArrowFunctionExpression: (node) => {
113
113
  checkForMoreThanOneParameter(node);
114
114
  },
115
115
  };
@@ -0,0 +1,51 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "suggestion",
4
+ docs: {
5
+ description: "Enforce the use of isNullish functions for nullish checks",
6
+ category: "Best Practices",
7
+ recommended: true,
8
+ },
9
+ messages: {
10
+ isNullish:
11
+ "Use isNullish() instead of direct variable check for nullish checks.",
12
+ nonNullish:
13
+ "Use nonNullish() instead of direct variable check for nullish checks.",
14
+ },
15
+ fixable: "code",
16
+ schema: [],
17
+ },
18
+
19
+ create: (context) => {
20
+ const binaryCheck = (node) => {
21
+ if (node.type === "BinaryExpression") {
22
+ return (
23
+ (node.operator === "===" || node.operator === "!==") &&
24
+ ((node.right.type === "Identifier" &&
25
+ node.right.name === "undefined") ||
26
+ (node.right.type === "Literal" && node.right.value === null))
27
+ );
28
+ }
29
+ };
30
+
31
+ const binaryReportCheck = (node) => {
32
+ context.report({
33
+ node,
34
+ messageId: node.operator === "===" ? "isNullish" : "nonNullish",
35
+ fix: (fixer) =>
36
+ fixer.replaceText(
37
+ node,
38
+ `${node.operator === "===" ? "isNullish" : "nonNullish"}(${context.sourceCode.getText(node.left)})`,
39
+ ),
40
+ });
41
+ };
42
+
43
+ return {
44
+ BinaryExpression: (node) => {
45
+ if (binaryCheck(node)) {
46
+ binaryReportCheck(node);
47
+ }
48
+ },
49
+ };
50
+ },
51
+ };
@@ -13,7 +13,7 @@ module.exports = {
13
13
  fixable: "code",
14
14
  schema: [],
15
15
  },
16
- create: function (context) {
16
+ create: (context) => {
17
17
  const checkForOptionType = (node) => {
18
18
  if (
19
19
  node.typeAnnotation.type === "TSUnionType" &&
@@ -38,15 +38,10 @@ module.exports = {
38
38
  data: {
39
39
  type: typeText,
40
40
  },
41
- fix(fixer) {
42
- return fixer.replaceText(
43
- node.typeAnnotation,
44
- `Option<${typeText}>`,
45
- );
46
- },
41
+ fix: (fixer) =>
42
+ fixer.replaceText(node.typeAnnotation, `Option<${typeText}>`),
47
43
  });
48
44
  } catch (e) {
49
- // eslint-disable-next-line no-console
50
45
  console.error(e);
51
46
  // eslint-disable-next-line no-console
52
47
  console.log(type);
@@ -56,10 +51,10 @@ module.exports = {
56
51
  };
57
52
 
58
53
  return {
59
- TSTypeAnnotation(node) {
54
+ TSTypeAnnotation: (node) => {
60
55
  checkForOptionType(node);
61
56
  },
62
- TSTypeAliasDeclaration(node) {
57
+ TSTypeAliasDeclaration: (node) => {
63
58
  checkForOptionType(node);
64
59
  },
65
60
  };
package/svelte.mjs ADDED
@@ -0,0 +1,56 @@
1
+ import svelte from "eslint-plugin-svelte";
2
+ import svelteParser from "svelte-eslint-parser";
3
+ import ts from "typescript-eslint";
4
+ import { join } from "node:path";
5
+ import { nonNullish } from "@dfinity/utils";
6
+ import { languageOptions } from "./configs/eslint.language.mjs";
7
+ import { eslintCoreConfig } from "./configs/eslint.core.mjs";
8
+
9
+ const svelteConfig = join(process.cwd(), "svelte.config.js");
10
+
11
+ export default [
12
+ ...eslintCoreConfig,
13
+ ...svelte.configs["flat/recommended"],
14
+ ...svelte.configs["flat/prettier"],
15
+ languageOptions([".svelte"]),
16
+ {
17
+ files: ["**/*.svelte"],
18
+ languageOptions: {
19
+ parser: svelteParser,
20
+ parserOptions: {
21
+ parser: ts.parser,
22
+ ...(nonNullish(svelteConfig) && { svelteConfig }),
23
+ },
24
+ },
25
+ },
26
+
27
+ {
28
+ files: ["**/*.svelte"],
29
+
30
+ rules: {
31
+ "import/order": [
32
+ "error",
33
+ {
34
+ alphabetize: {
35
+ order: "asc",
36
+ },
37
+ },
38
+ ],
39
+ },
40
+ },
41
+ {
42
+ files: ["scripts/**/*.mjs", "scripts/**/*.ts"],
43
+
44
+ rules: {
45
+ "no-console": "off",
46
+ },
47
+ },
48
+ {
49
+ rules: {
50
+ // TODO: Fix after migration to Svelte v5
51
+ "no-constant-binary-expression": "off",
52
+ "@typescript-eslint/no-empty-object-type": "off",
53
+ "@typescript-eslint/no-unused-expressions": "off",
54
+ },
55
+ },
56
+ ];
package/index.cjs DELETED
@@ -1,74 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-var-requires
2
- const { join } = require("node:path");
3
- // eslint-disable-next-line @typescript-eslint/no-var-requires
4
- const { existsSync } = require("node:fs");
5
-
6
- const resolveProject = () => {
7
- const eslintJson = join(process.cwd(), "tsconfig.eslint.json");
8
-
9
- if (existsSync(eslintJson)) {
10
- return eslintJson;
11
- }
12
-
13
- return join(process.cwd(), "tsconfig.json");
14
- };
15
-
16
- module.exports = {
17
- root: true,
18
- extends: [
19
- "eslint:recommended",
20
- "plugin:@typescript-eslint/recommended",
21
- "prettier",
22
- ],
23
- parser: "@typescript-eslint/parser",
24
- plugins: ["@typescript-eslint", "eslint-plugin-local-rules", "import"],
25
- parserOptions: {
26
- sourceType: "module",
27
- ecmaVersion: 2020,
28
- project: [resolveProject()],
29
- },
30
- env: {
31
- browser: true,
32
- es2017: true,
33
- node: true,
34
- },
35
- overrides: [
36
- {
37
- files: ["*.ts"],
38
- rules: {
39
- "prefer-const": "error",
40
- },
41
- },
42
- ],
43
- rules: {
44
- "@typescript-eslint/consistent-type-definitions": "error",
45
- "@typescript-eslint/no-inferrable-types": "error",
46
- "@typescript-eslint/no-unnecessary-type-assertion": "error",
47
- "@typescript-eslint/no-unused-vars": [
48
- "warn",
49
- {
50
- argsIgnorePattern: "^_",
51
- varsIgnorePattern: "^_",
52
- caughtErrorsIgnorePattern: "^_",
53
- },
54
- ],
55
- "@typescript-eslint/prefer-nullish-coalescing": "error",
56
- "@typescript-eslint/prefer-reduce-type-parameter": "error",
57
- "arrow-body-style": ["warn", "as-needed"],
58
- curly: "error",
59
- "local-rules/prefer-object-params": "warn",
60
- "local-rules/use-option-type-wrapper": "warn",
61
- "import/no-duplicates": ["error", { "prefer-inline": true }],
62
- "import/no-relative-parent-imports": "error",
63
- "no-console": ["error", { allow: ["error", "warn"] }],
64
- "no-continue": "warn",
65
- "no-delete-var": "error",
66
- "no-else-return": ["warn", { allowElseIf: false }],
67
- "no-unused-vars": "off",
68
- "prefer-template": "error",
69
- "require-await": "error",
70
- },
71
- globals: {
72
- NodeJS: true,
73
- },
74
- };
package/svelte.cjs DELETED
@@ -1,32 +0,0 @@
1
- module.exports = {
2
- extends: ["./index.cjs", "plugin:svelte/recommended"],
3
- parserOptions: {
4
- extraFileExtensions: [".svelte"],
5
- },
6
- overrides: [
7
- {
8
- files: ["*.svelte"],
9
- parser: "svelte-eslint-parser",
10
- parserOptions: {
11
- parser: "@typescript-eslint/parser",
12
- },
13
- rules: {
14
- "import/order": [
15
- "error",
16
- {
17
- alphabetize: { order: "asc" },
18
- },
19
- ],
20
- },
21
- },
22
- {
23
- files: ["scripts/**/*.mjs", "scripts/**/*.ts"],
24
- rules: {
25
- "no-console": "off",
26
- },
27
- },
28
- ],
29
- rules: {
30
- "local-rules/no-svelte-store-in-api": "error",
31
- },
32
- };