@dineroregnskab/eslint-plugin-custom-rules 2.0.7 → 2.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/README.md +49 -9
- package/eslint-plugin-custom-rules.js +10 -6
- package/package.json +2 -4
- package/rules/replace-first-with-take.js +31 -0
- package/rules/use-danish-currency-pipe.js +53 -0
- package/willy.yaml +5 -0
package/README.md
CHANGED
|
@@ -7,24 +7,63 @@ Custom extended rules for various Dinero specific standards & conventions.
|
|
|
7
7
|
Bundled with dinero.Frontend & dinero.Admin package.json, so a general npm install will include it as default.
|
|
8
8
|
|
|
9
9
|
Explicit install:
|
|
10
|
+
|
|
10
11
|
```bash
|
|
11
12
|
npm install @dineroregnskab/eslint-plugin-eslint-custom-rules@latest --save-dev
|
|
12
13
|
```
|
|
13
14
|
|
|
14
|
-
##
|
|
15
|
+
## Development
|
|
16
|
+
|
|
17
|
+
Run `npm i` in root and in `/example`.
|
|
18
|
+
|
|
19
|
+
### Adding rules
|
|
15
20
|
|
|
16
|
-
Add the rule here:
|
|
21
|
+
- Add the rule here: `dinero-web-2.0/Dinero.Packages/Dinero.EslintCustomRules/rules`
|
|
17
22
|
|
|
18
|
-
`
|
|
23
|
+
- Add the new js rule to this file `eslint-plugin-custom-rules.js`.
|
|
19
24
|
|
|
20
|
-
You can test the rule by adding some code to test it on here.
|
|
25
|
+
- You can test the rule by adding some HTML code to test it on here `dinero-web-2.0/Dinero.Packages/Dinero.EslintCustomRules/example`
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
- After adding the rule you can restart the ESLint server in vs code by pressing F1 -> ESLint: Restart ESLint Server.
|
|
28
|
+
|
|
29
|
+
Add html rules in `test.html` and run test with
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm run testhtml
|
|
33
|
+
```
|
|
23
34
|
|
|
24
|
-
|
|
35
|
+
Add ts rules in `test.ts` and run test with
|
|
25
36
|
|
|
26
37
|
```bash
|
|
27
|
-
|
|
38
|
+
npm run testts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### Debugging what the eslint sees
|
|
42
|
+
|
|
43
|
+
Add a rule, and log it with console log, to see in terminal what is going on. Then run `npm run testhtml` npm script to see it in terminal.
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
module.exports = {
|
|
47
|
+
meta: {
|
|
48
|
+
type: "suggestion",
|
|
49
|
+
docs: {
|
|
50
|
+
description:
|
|
51
|
+
"Enforce using `danishCurrency` pipe instead of `currency` pipe in Angular HTML templates.",
|
|
52
|
+
},
|
|
53
|
+
fixable: "code",
|
|
54
|
+
schema: [],
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
create(context) {
|
|
58
|
+
return {
|
|
59
|
+
// Target the entire file and traverse each node
|
|
60
|
+
Program(node) {
|
|
61
|
+
console.log("Parsed Node Types:", node);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
28
67
|
```
|
|
29
68
|
|
|
30
69
|
### Publish & install new rule locally
|
|
@@ -34,12 +73,14 @@ Run:
|
|
|
34
73
|
```bash
|
|
35
74
|
npm patch
|
|
36
75
|
```
|
|
76
|
+
|
|
37
77
|
```bash
|
|
38
78
|
npm publish
|
|
39
79
|
```
|
|
40
80
|
|
|
41
81
|
- In dinero.Frontend and/or dinero.Admin, update the root `package.json` file with the newly published version number.
|
|
42
82
|
- Locate the `eslintrc` file and add the new rule in the rules property (under `"files": ["*.html"]`, `"files": ["*.ts"]` etc. respectively).
|
|
83
|
+
|
|
43
84
|
> **Note: The reference here must be in the format of the package name without "eslint-plugin" + rule name**
|
|
44
85
|
|
|
45
86
|
Example:
|
|
@@ -52,9 +93,8 @@ Example:
|
|
|
52
93
|
npm i
|
|
53
94
|
````
|
|
54
95
|
|
|
55
|
-
> **Note: You
|
|
96
|
+
> **Note: You need to restart ESLint to apply new rules. Restart the ESLint server in vs code by pressing F1 -> ESLint: Restart ESLint Server or F1 -> reload window**
|
|
56
97
|
|
|
57
|
-
#
|
|
58
98
|
See [ESLint custom rule tutorial](https://eslint.org/docs/latest/extend/custom-rule-tutorial) for more.
|
|
59
99
|
|
|
60
100
|
Useful tool for working with AST tree: [AST Explorer](https://astexplorer.net/)
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
const returnReducerRule = require(
|
|
2
|
-
const camelCaseRule = require(
|
|
3
|
-
const dayjsWithTimeZoneRule = require(
|
|
1
|
+
const returnReducerRule = require("./rules/reducers-should-always-return");
|
|
2
|
+
const camelCaseRule = require("./rules/camel-case-attributes");
|
|
3
|
+
const dayjsWithTimeZoneRule = require("./rules/dayjs-with-timezone");
|
|
4
|
+
const useDanishCurrencyPipeRule = require("./rules/use-danish-currency-pipe");
|
|
5
|
+
const replaceFirstWithTakeRule = require("./rules/replace-first-with-take");
|
|
4
6
|
|
|
5
7
|
const customRulesPlugin = {
|
|
6
8
|
rules: {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
"reducers-should-always-return": returnReducerRule,
|
|
10
|
+
"attr-camel-case-rule": camelCaseRule,
|
|
11
|
+
"dayjs-with-timezone": dayjsWithTimeZoneRule,
|
|
12
|
+
"use-danish-currency-pipe": useDanishCurrencyPipeRule,
|
|
13
|
+
"replace-first-with-take": replaceFirstWithTakeRule,
|
|
10
14
|
},
|
|
11
15
|
};
|
|
12
16
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dineroregnskab/eslint-plugin-custom-rules",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "ESLint plugin with custom rules for Dinero Regnskab",
|
|
5
5
|
"main": "eslint-plugin-custom-rules.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": ""
|
|
8
|
-
},
|
|
6
|
+
"scripts": {},
|
|
9
7
|
"keywords": [],
|
|
10
8
|
"author": "",
|
|
11
9
|
"license": "ISC",
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "suggestion",
|
|
4
|
+
docs: {
|
|
5
|
+
description:
|
|
6
|
+
"Replace `first()` with `take(1)` in TypeScript files.",
|
|
7
|
+
},
|
|
8
|
+
fixable: "code",
|
|
9
|
+
schema: [],
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
create(context) {
|
|
13
|
+
return {
|
|
14
|
+
// Target CallExpressions like `first()`
|
|
15
|
+
CallExpression(node) {
|
|
16
|
+
// Check if the callee (function being called) is `first`
|
|
17
|
+
if (
|
|
18
|
+
node.callee.type === "Identifier" &&
|
|
19
|
+
node.callee.name === "first" &&
|
|
20
|
+
node.arguments.length === 0 // Ensure `first` is called with no arguments
|
|
21
|
+
) {
|
|
22
|
+
context.report({
|
|
23
|
+
node,
|
|
24
|
+
message:
|
|
25
|
+
"Replace `first()` with `take(1)` to avoid errors if no value is ever emitted.",
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
},
|
|
31
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "suggestion",
|
|
4
|
+
docs: {
|
|
5
|
+
description:
|
|
6
|
+
"Enforce using `danishCurrency` pipe instead of `currency` pipe in Angular HTML templates.",
|
|
7
|
+
},
|
|
8
|
+
fixable: "code",
|
|
9
|
+
schema: [],
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
create(context) {
|
|
13
|
+
return {
|
|
14
|
+
Program(node) {
|
|
15
|
+
const templateContent = node.value;
|
|
16
|
+
const currencyPipeRegex = /\|\s*currency/g;
|
|
17
|
+
|
|
18
|
+
let match;
|
|
19
|
+
while (
|
|
20
|
+
(match = currencyPipeRegex.exec(templateContent)) !== null
|
|
21
|
+
) {
|
|
22
|
+
const matchStart = match.index;
|
|
23
|
+
const matchEnd = match.index + match[0].length;
|
|
24
|
+
|
|
25
|
+
const startLine = templateContent
|
|
26
|
+
.slice(0, matchStart)
|
|
27
|
+
.split("\n").length;
|
|
28
|
+
const startColumn =
|
|
29
|
+
matchStart -
|
|
30
|
+
templateContent.lastIndexOf("\n", matchStart) -
|
|
31
|
+
1;
|
|
32
|
+
|
|
33
|
+
const endLine = templateContent
|
|
34
|
+
.slice(0, matchEnd)
|
|
35
|
+
.split("\n").length;
|
|
36
|
+
const endColumn =
|
|
37
|
+
matchEnd -
|
|
38
|
+
templateContent.lastIndexOf("\n", matchEnd) -
|
|
39
|
+
1;
|
|
40
|
+
|
|
41
|
+
context.report({
|
|
42
|
+
loc: {
|
|
43
|
+
start: { line: startLine, column: startColumn },
|
|
44
|
+
end: { line: endLine, column: endColumn },
|
|
45
|
+
},
|
|
46
|
+
message:
|
|
47
|
+
"Use `danishCurrency` instead of `currency` pipe. We can't use Angular's native `currency` pipe because it doesn't support inputs with commas as decimal separators.",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
};
|