@dineroregnskab/eslint-plugin-custom-rules 2.0.7 → 2.1.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.
package/README.md CHANGED
@@ -7,39 +7,78 @@ 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
- ## Adding rules
15
+ ## Development
16
+
17
+ Run `npm i` in root and in `/example`.
15
18
 
16
- Add the rule here:
19
+ ### Adding rules
17
20
 
18
- `dinero-web-2.0/Dinero.Packages/Dinero.EslintCustomRules/rules`
21
+ - Add the rule here: `dinero-web-2.0/Dinero.Packages/Dinero.EslintCustomRules/rules`
19
22
 
20
- You can test the rule by adding some code to test it on here.
23
+ - Add the new js rule to this file `eslint-plugin-custom-rules.js`.
21
24
 
22
- `dinero-web-2.0/Dinero.Packages/Dinero.EslintCustomRules/example`
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`
23
26
 
24
- And run:
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
25
30
 
26
31
  ```bash
27
- npx eslint test.ts
32
+ npm run testhtml
28
33
  ```
29
34
 
30
- ### Publish & install new rule locally
31
-
32
- Run:
35
+ Add ts rules in `test.ts` and run test with
33
36
 
34
37
  ```bash
35
- npm patch
38
+ npm run testts
36
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
+
67
+ ```
68
+
69
+ ### Publish & install new rule locally
70
+
71
+ 1. Log in to npm using `npm login`
72
+ 2. run:
73
+
37
74
  ```bash
38
- npm publish
75
+ # {version_type}: The type of version increment (patch, minor, major)
76
+ npm version {version_type} && npm publish
39
77
  ```
40
78
 
41
79
  - In dinero.Frontend and/or dinero.Admin, update the root `package.json` file with the newly published version number.
42
80
  - Locate the `eslintrc` file and add the new rule in the rules property (under `"files": ["*.html"]`, `"files": ["*.ts"]` etc. respectively).
81
+
43
82
  > **Note: The reference here must be in the format of the package name without "eslint-plugin" + rule name**
44
83
 
45
84
  Example:
@@ -52,9 +91,8 @@ Example:
52
91
  npm i
53
92
  ````
54
93
 
55
- > **Note: You might need to restart the IDE for the new rule to apply**
94
+ > **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
95
 
57
- #
58
96
  See [ESLint custom rule tutorial](https://eslint.org/docs/latest/extend/custom-rule-tutorial) for more.
59
97
 
60
98
  Useful tool for working with AST tree: [AST Explorer](https://astexplorer.net/)
@@ -1,12 +1,16 @@
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');
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
- 'reducers-should-always-return': returnReducerRule,
8
- 'attr-camel-case-rule': camelCaseRule,
9
- 'dayjs-with-timezone': dayjsWithTimeZoneRule,
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.7",
3
+ "version": "2.1.1",
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\b/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
+ };
package/willy.yaml ADDED
@@ -0,0 +1,5 @@
1
+ type: package
2
+ name: package-eslint-custom-rules
3
+ aliases:
4
+ - eslint-custom-rules
5
+ - eslintcustomrules