@asd14/eslint-plugin 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.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrei Dumitrescu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ [![npm version](https://badge.fury.io/js/%40asd14%2Feslint-plugin.svg)](https://badge.fury.io/js/%40asd14%2Feslint-plugin)
2
+ ![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)
3
+
4
+ # @asd14/eslint-plugin
5
+
6
+ > ESLint rules for opinionated DX not covered by existing plugins.
7
+
8
+ <!-- vim-markdown-toc GFM -->
9
+
10
+ - [Install](#install)
11
+ - [Rules](#rules)
12
+ - [`@asd14/error-message-format`](#asd14error-message-format)
13
+ - [Develop](#develop)
14
+
15
+ <!-- vim-markdown-toc -->
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ npm install @asd14/eslint-plugin --save-dev
21
+ ```
22
+
23
+ > NOTE: requires peerDependency `eslint^9` or `eslint^10`
24
+
25
+ ## Rules
26
+
27
+ ### `@asd14/error-message-format`
28
+
29
+ Enforce consistent error message format in `throw` statements.
30
+
31
+ - Per error class: `Error`, `TypeError` or custom `DBError`
32
+ - Each is an array of `RegExp` patterns
33
+ - `OR` matching, first match wins
34
+
35
+ ```js
36
+ // eslint.config.js
37
+ import asd14Plugin from "@asd14/eslint-plugin"
38
+
39
+ export default [
40
+ {
41
+ plugins: { "@asd14": asd14Plugin },
42
+ rules: {
43
+ "@asd14/error-message-format": [
44
+ "error",
45
+ {
46
+ TypeError: [
47
+ {
48
+ pattern:
49
+ "^@asd14/m/\\w+: expected '\\w+' to be '\\w+' or '\\w+', got '\\w+'",
50
+ message:
51
+ "Format: @asd14/m/<fn>: expected '<param>' to be '<Type>' or '<Type>', got '<Actual>'"
52
+ },
53
+ {
54
+ pattern:
55
+ "^@asd14/m/\\w+: expected '\\w+' to be '\\w+', got '\\w+'",
56
+ message:
57
+ "Format: @asd14/m/<fn>: expected '<param>' to be '<Type>', got '<Actual>'"
58
+ }
59
+ ]
60
+ }
61
+ ]
62
+ }
63
+ }
64
+ ]
65
+ ```
66
+
67
+ Examples of **correct** code:
68
+
69
+ ```js
70
+ // Template literal with interpolation
71
+ throw new TypeError(
72
+ `@asd14/m/sort: expected 'input' to be 'Array', got '${type(input)}'`
73
+ )
74
+
75
+ // String concatenation
76
+ throw new TypeError(
77
+ "@asd14/m/sort: expected 'input' to be 'Array', got '" + type(input) + "'"
78
+ )
79
+
80
+ // "or" variant - multiple expected types
81
+ throw new TypeError(
82
+ `@asd14/m/all: expected 'fn' to be 'Function' or 'Array', got '${type(fn)}'`
83
+ )
84
+
85
+ // Unconfigured error classes are ignored
86
+ throw new Error("something went wrong")
87
+ ```
88
+
89
+ Examples of **incorrect** code:
90
+
91
+ ```js
92
+ // Missing @asd14/m/ prefix
93
+ throw new TypeError("expected 'input' to be 'Array', got '" + type(x) + "'")
94
+
95
+ // Unquoted types
96
+ throw new TypeError(`@asd14/m/sort: expected Array, got ${type(input)}`)
97
+
98
+ // Can't statically evaluate
99
+ throw new TypeError(message)
100
+ ```
101
+
102
+ ## Develop
103
+
104
+ ```bash
105
+ npm run lint # eslint
106
+ npm run typecheck # tsc --noEmit
107
+ npm run test # node:test runner
108
+ npm run coverage # c8 --100
109
+ ```
@@ -0,0 +1,12 @@
1
+ import { errorMessageFormat } from "./rules/error-message-format/error-message-format.js";
2
+ declare const plugin: {
3
+ meta: {
4
+ name: string;
5
+ version: string;
6
+ };
7
+ rules: {
8
+ "error-message-format": import("eslint").Rule.RuleModule;
9
+ };
10
+ };
11
+ export default plugin;
12
+ export { errorMessageFormat };
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import packageJSON from "../package.json" with {
2
+ type: "json"
3
+ };
4
+ import { errorMessageFormat } from "./rules/error-message-format/error-message-format.js";
5
+ const plugin = {
6
+ meta: {
7
+ name: packageJSON.name,
8
+ version: packageJSON.version
9
+ },
10
+ rules: {
11
+ "error-message-format": errorMessageFormat
12
+ }
13
+ };
14
+ export default plugin;
15
+ export { errorMessageFormat };
@@ -0,0 +1,3 @@
1
+ import type { Rule } from "eslint";
2
+ declare const errorMessageFormat: Rule.RuleModule;
3
+ export { errorMessageFormat };
@@ -0,0 +1,92 @@
1
+ import { flattenExpression } from "./utils/flatten-expression.js";
2
+ const errorMessageFormat = {
3
+ meta: {
4
+ type: "suggestion",
5
+ docs: {
6
+ description: "Enforce consistent error message format in throw statements"
7
+ },
8
+ messages: {
9
+ formatViolation: "{{message}}",
10
+ cannotEvaluate: "Cannot statically evaluate error message. Use a string literal or template literal"
11
+ },
12
+ schema: [
13
+ {
14
+ type: "object",
15
+ additionalProperties: {
16
+ type: "array",
17
+ items: {
18
+ type: "object",
19
+ properties: {
20
+ pattern: {
21
+ type: "string"
22
+ },
23
+ message: {
24
+ type: "string"
25
+ }
26
+ },
27
+ required: [
28
+ "pattern",
29
+ "message"
30
+ ],
31
+ additionalProperties: false
32
+ }
33
+ }
34
+ }
35
+ ]
36
+ },
37
+ create (context) {
38
+ const ruleOptions = context.options[0] ?? {};
39
+ const errorClasses = Object.keys(ruleOptions);
40
+ if (errorClasses.length === 0) {
41
+ return {};
42
+ }
43
+ return {
44
+ ThrowStatement (node) {
45
+ // Only matching `throw new Error(...)`
46
+ const argument = node.argument;
47
+ if (argument.type !== "NewExpression") {
48
+ return;
49
+ }
50
+ const callee = argument.callee;
51
+ if (callee.type !== "Identifier") {
52
+ return;
53
+ }
54
+ const checks = ruleOptions[callee.name];
55
+ if (!checks || checks.length === 0) {
56
+ return;
57
+ }
58
+ const firstArgument = argument.arguments[0];
59
+ if (!firstArgument) {
60
+ return;
61
+ }
62
+ // Turn AST -> string representation which we can match against the
63
+ // user's RegExps
64
+ const fullMessage = flattenExpression(firstArgument);
65
+ // Can't validate entirely opaque variable, function call, etc.
66
+ if (fullMessage === "EXPR") {
67
+ context.report({
68
+ node: firstArgument,
69
+ messageId: "cannotEvaluate"
70
+ });
71
+ return;
72
+ }
73
+ // If ANY pattern matches, the message is valid.
74
+ // Report the first check's message if none match.
75
+ for (const check of checks){
76
+ const re = new RegExp(check.pattern);
77
+ if (re.test(fullMessage)) {
78
+ return;
79
+ }
80
+ }
81
+ context.report({
82
+ node: firstArgument,
83
+ messageId: "formatViolation",
84
+ data: {
85
+ message: checks[0]?.message
86
+ }
87
+ });
88
+ }
89
+ };
90
+ }
91
+ };
92
+ export { errorMessageFormat };
@@ -0,0 +1,8 @@
1
+ import type { Expression } from "acorn";
2
+ /**
3
+ * Flatten an AST expression node into a plain string, replacing all
4
+ * non-literal parts (interpolations, function calls, identifiers) with
5
+ * the placeholder "EXPR".
6
+ */
7
+ declare const flattenExpression: (node: Expression) => string;
8
+ export { flattenExpression };
@@ -0,0 +1,34 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion -- loop bounds guarantee valid indices */ /**
2
+ * Flatten an AST expression node into a plain string, replacing all
3
+ * non-literal parts (interpolations, function calls, identifiers) with
4
+ * the placeholder "EXPR".
5
+ */ const flattenExpression = (node)=>{
6
+ // String literals: "hello" → "hello"
7
+ if (node.type === "Literal" && typeof node.value === "string") {
8
+ return node.value;
9
+ }
10
+ // Template literals: `got '${type(x)}'` → `got 'EXPR'`
11
+ if (node.type === "TemplateLiteral") {
12
+ let result = "";
13
+ for(let i = 0; i < node.quasis.length; i++){
14
+ result += node.quasis[i].value.raw;
15
+ if (i < node.quasis.length - 1) {
16
+ result += "EXPR";
17
+ }
18
+ }
19
+ return result;
20
+ }
21
+ // Binary `+` chains: `"a" + expr + "b"` → "aEXPRb"
22
+ if (node.type === "BinaryExpression" && node.operator === "+") {
23
+ // Acorn types `left` as `Expression | PrivateIdentifier` because
24
+ // BinaryExpression covers `a in #field` (the `in` operator can have
25
+ // a PrivateIdentifier on the right). For `+` this can never happen.
26
+ const left = flattenExpression(node.left);
27
+ const right = flattenExpression(node.right);
28
+ return left + right;
29
+ }
30
+ // Any unrecognized node (call expressions, identifiers, etc.)
31
+ // is treated as an opaque interpolation.
32
+ return "EXPR";
33
+ };
34
+ export { flattenExpression };
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@asd14/eslint-plugin",
3
+ "version": "0.1.0",
4
+ "description": "ESLint rules for opinionated DX not covered by existing plugins",
5
+ "license": "MIT",
6
+ "author": {
7
+ "name": "Andrei Dumitrescu",
8
+ "url": "https://github.com/andreidmt"
9
+ },
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "sideEffects": false,
20
+ "files": [
21
+ "/dist"
22
+ ],
23
+ "nx": {
24
+ "projectType": "library",
25
+ "tags": [
26
+ "type:library",
27
+ "target:node"
28
+ ]
29
+ },
30
+ "scripts": {
31
+ "----BUNDLE": "",
32
+ "build:code": "swc src -d dist --ignore '**/*.test.ts' --strip-leading-paths",
33
+ "build:types": "tsc --declaration --emitDeclarationOnly --project ./.tsconfig/code.json",
34
+ "prebuild": "rm -rf dist/* node_modules/.tmp/*.tsbuildinfo",
35
+ "build": "npm run build:code && npm run build:types",
36
+ "----LINT": "",
37
+ "lint": "eslint --quiet .",
38
+ "typecheck:code": "tsc --noEmit --project ./.tsconfig/code.json",
39
+ "typecheck:config": "tsc --noEmit --project ./.tsconfig/config.json",
40
+ "typecheck:tests": "tsc --noEmit --project ./.tsconfig/tests.json",
41
+ "typecheck": "npm run typecheck:code && npm run typecheck:config && npm run typecheck:tests",
42
+ "----TEST": "",
43
+ "test": "node --import tsx --test 'src/**/*.test.ts'",
44
+ "coverage": "c8 --100 --exclude='src/**/*.test.ts' node --import tsx --test 'src/**/*.test.ts'",
45
+ "----PUBLISH": "",
46
+ "release": "semantic-release"
47
+ },
48
+ "dependencies": {
49
+ "acorn": "^8.16.0"
50
+ },
51
+ "devDependencies": {
52
+ "@asd14/eslint-config": "^15.2.0",
53
+ "@asd14/prettier-config": "^1.2.0",
54
+ "@asd14/ts-config": "^1.3.0",
55
+ "@commitlint/cli": "^20.4.3",
56
+ "@commitlint/config-conventional": "^20.4.3",
57
+ "@semantic-release/git": "^10.0.1",
58
+ "@swc/cli": "^0.5.0",
59
+ "@swc/core": "^1.15.18",
60
+ "c8": "^11.0.0",
61
+ "conventional-changelog-conventionalcommits": "^9.3.0",
62
+ "eslint": "^9.39.2",
63
+ "prettier": "^3.8.1",
64
+ "semantic-release": "^24.2.9",
65
+ "tsx": "^4.21.0",
66
+ "typescript": "^5.9.3"
67
+ },
68
+ "peerDependencies": {
69
+ "eslint": "^9 || ^10"
70
+ },
71
+ "engines": {
72
+ "node": ">=22.0.0"
73
+ }
74
+ }