@yoo-digital/eslint-plugin-angular 1.1.0 → 1.2.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
|
@@ -6,32 +6,17 @@ Here should live all custom Angular lint rules that eslint does not already prov
|
|
|
6
6
|
|
|
7
7
|
## Linting
|
|
8
8
|
|
|
9
|
-
Wrong code is yellow underlined in VScode, it can also be raises running : `npm run lint`
|
|
9
|
+
Wrong code is yellow/red underlined in VScode, it can also be raises running : `npm run lint`
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Rule 1 : boolean input conversion
|
|
12
14
|
|
|
13
15
|
`booleanAttribute @angular/core`
|
|
14
16
|
|
|
15
17
|
`BooleanInput @angular/cdk/coercion`
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
**Current Implementation:** This rule enforces shorthand syntax (`<x a />` instead of `<x [a]="true" />`) but **cannot automatically verify**:
|
|
20
|
-
- Whether an input has `transform: booleanAttribute`
|
|
21
|
-
- What the default value of an input is
|
|
22
|
-
|
|
23
|
-
This is a technical limitation of Angular ESLint - template rules cannot access the component's TypeScript code.
|
|
24
|
-
|
|
25
|
-
### Recommended Usage
|
|
26
|
-
|
|
27
|
-
This rule works best when:
|
|
28
|
-
1. **All boolean inputs** in your project use `booleanAttribute` transform
|
|
29
|
-
2. **Most boolean inputs** have `default = false` or no default
|
|
30
|
-
3. You treat this as a **style guide enforcer** rather than a safety checker
|
|
31
|
-
|
|
32
|
-
### Configuration
|
|
33
|
-
|
|
34
|
-
By default, the rule only flags `[attr]="true"` bindings (safe default):
|
|
19
|
+
The rule only flags components inputs `[isVegan]="true"` bindings and ignores `[isVegan]="false"` bindings:
|
|
35
20
|
|
|
36
21
|
```json
|
|
37
22
|
{
|
|
@@ -41,73 +26,24 @@ By default, the rule only flags `[attr]="true"` bindings (safe default):
|
|
|
41
26
|
}
|
|
42
27
|
```
|
|
43
28
|
|
|
44
|
-
To also enforce removal of `[attr]="false"` bindings:
|
|
45
|
-
|
|
46
|
-
```json
|
|
47
|
-
{
|
|
48
|
-
"rules": {
|
|
49
|
-
"@yoo-digital/eslint-plugin-angular/prefer-boolean-attribute-shorthand": [
|
|
50
|
-
"error",
|
|
51
|
-
{ "allowFalseLiteral": false }
|
|
52
|
-
]
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
29
|
### Examples
|
|
58
30
|
|
|
59
|
-
#### ✅ Recommended Pattern (Default false or no default)
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
// Component
|
|
63
|
-
@Input({ transform: booleanAttribute }) disabled: boolean = false;
|
|
64
|
-
// OR
|
|
65
|
-
disabled = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
**Template:**
|
|
69
|
-
- ❌ `<button [disabled]="true">` → Should be `<button disabled>`
|
|
70
|
-
- ✅ `<button disabled>` (shorthand for true)
|
|
71
|
-
- ✅ `<button>` (omit for false)
|
|
72
|
-
- ✅ `<button [disabled]="isLoading">` (expressions are allowed)
|
|
73
|
-
|
|
74
|
-
#### ⚠️ Special Case: Default true (Disable rule if needed)
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
// Component
|
|
78
|
-
@Input({ transform: booleanAttribute }) enabled: boolean = true;
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
**Template:**
|
|
82
31
|
```html
|
|
83
|
-
<!--
|
|
84
|
-
<
|
|
32
|
+
<!-- this will raise lint issue -->
|
|
33
|
+
<myMealComponent [isVegan]="true" />
|
|
34
|
+
<!-- false value raises no lint issue (ignored) -->
|
|
35
|
+
<myMealComponent [isVegan]="false" />
|
|
85
36
|
```
|
|
86
37
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
#### ❌ Without booleanAttribute (Won't work!)
|
|
38
|
+
Lint enforces use of boolean attribute :
|
|
90
39
|
|
|
91
40
|
```typescript
|
|
92
|
-
//
|
|
93
|
-
|
|
41
|
+
// Modern signal way
|
|
42
|
+
isVegan = input<boolean, BooleanInput>(false, { transform: booleanAttribute });
|
|
43
|
+
// Old decorator way
|
|
44
|
+
@Input({ transform: booleanAttribute }) isVegan: boolean = false;
|
|
94
45
|
```
|
|
95
46
|
|
|
96
|
-
|
|
97
|
-
- ❌ `<input checked>` → Will pass `""` (empty string), NOT boolean!
|
|
98
|
-
- ✅ `<input [checked]="true">` (must use property binding)
|
|
99
|
-
|
|
100
|
-
### Decorator Syntax
|
|
101
|
-
|
|
102
|
-
`@Input({ transform: booleanAttribute }) myInput: boolean = false;`
|
|
103
|
-
|
|
104
|
-
### Signal Input Syntax
|
|
105
|
-
|
|
106
|
-
`myInput = input<boolean, BooleanInput>(false, { transform: booleanAttribute });`
|
|
107
|
-
|
|
108
|
-
### Summary
|
|
47
|
+
---
|
|
109
48
|
|
|
110
|
-
|
|
111
|
-
- **Rule assumes**: Inputs have `booleanAttribute` and `default ≠ true`
|
|
112
|
-
- **Manual override**: Use eslint-disable comments for special cases
|
|
113
|
-
- **Best practice**: Ensure all boolean inputs use `booleanAttribute`
|
|
49
|
+
## Rule 2 : ...
|
|
@@ -1,37 +1,25 @@
|
|
|
1
1
|
import type { TSESLint } from '@typescript-eslint/utils';
|
|
2
|
-
|
|
3
|
-
allowFalseLiteral?: boolean;
|
|
4
|
-
}
|
|
5
|
-
type MessageIds = 'preferTrue' | 'preferFalse' | 'suggestTrue' | 'suggestRemove';
|
|
2
|
+
type MessageIds = 'preferTrue' | 'suggestTrue';
|
|
6
3
|
export declare const RULE_NAME = "prefer-boolean-attribute-shorthand";
|
|
7
4
|
/**
|
|
8
|
-
*
|
|
5
|
+
* This rule enforces shorthand syntax for boolean inputs bound to true.
|
|
9
6
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* 3. Complex AST traversal and decorator/signal input analysis
|
|
7
|
+
* BEHAVIOR:
|
|
8
|
+
* - [attr]="true" → Warns and suggests: attr
|
|
9
|
+
* - [attr]="false" → No warning (ignored)
|
|
10
|
+
* - attr → OK (already shorthand)
|
|
15
11
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
12
|
+
* ASSUMPTIONS:
|
|
13
|
+
* This rule assumes that boolean inputs use Angular's booleanAttribute transform,
|
|
14
|
+
* which allows the shorthand syntax to work correctly. The presence of the attribute
|
|
15
|
+
* alone (without a binding) will be interpreted as true.
|
|
20
16
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
17
|
+
* LIMITATIONS:
|
|
18
|
+
* Cannot verify at lint-time whether:
|
|
19
|
+
* - The input actually has `transform: booleanAttribute`
|
|
20
|
+
* - The input's default value
|
|
25
21
|
*
|
|
26
|
-
*
|
|
27
|
-
* To implement the full feature set requested would require creating a SEPARATE
|
|
28
|
-
* TypeScript ESLint rule that:
|
|
29
|
-
* - Runs on .ts component files (not templates)
|
|
30
|
-
* - Analyzes @Component decorators to find template references
|
|
31
|
-
* - Parses templates and cross-references with component inputs
|
|
32
|
-
* - Reports errors in both .ts and template files
|
|
33
|
-
*
|
|
34
|
-
* This would be a significantly more complex multi-file analysis rule.
|
|
22
|
+
* Use this rule in projects where boolean inputs consistently use booleanAttribute.
|
|
35
23
|
*/
|
|
36
|
-
export declare const preferBooleanAttributeShorthandRule: TSESLint.RuleModule<MessageIds,
|
|
24
|
+
export declare const preferBooleanAttributeShorthandRule: TSESLint.RuleModule<MessageIds, []>;
|
|
37
25
|
export {};
|
|
@@ -4,67 +4,40 @@ exports.preferBooleanAttributeShorthandRule = exports.RULE_NAME = void 0;
|
|
|
4
4
|
const utils_1 = require("@angular-eslint/utils");
|
|
5
5
|
exports.RULE_NAME = 'prefer-boolean-attribute-shorthand';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* This rule enforces shorthand syntax for boolean inputs bound to true.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* 3. Complex AST traversal and decorator/signal input analysis
|
|
9
|
+
* BEHAVIOR:
|
|
10
|
+
* - [attr]="true" → Warns and suggests: attr
|
|
11
|
+
* - [attr]="false" → No warning (ignored)
|
|
12
|
+
* - attr → OK (already shorthand)
|
|
14
13
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* ASSUMPTIONS:
|
|
15
|
+
* This rule assumes that boolean inputs use Angular's booleanAttribute transform,
|
|
16
|
+
* which allows the shorthand syntax to work correctly. The presence of the attribute
|
|
17
|
+
* alone (without a binding) will be interpreted as true.
|
|
19
18
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
19
|
+
* LIMITATIONS:
|
|
20
|
+
* Cannot verify at lint-time whether:
|
|
21
|
+
* - The input actually has `transform: booleanAttribute`
|
|
22
|
+
* - The input's default value
|
|
24
23
|
*
|
|
25
|
-
*
|
|
26
|
-
* To implement the full feature set requested would require creating a SEPARATE
|
|
27
|
-
* TypeScript ESLint rule that:
|
|
28
|
-
* - Runs on .ts component files (not templates)
|
|
29
|
-
* - Analyzes @Component decorators to find template references
|
|
30
|
-
* - Parses templates and cross-references with component inputs
|
|
31
|
-
* - Reports errors in both .ts and template files
|
|
32
|
-
*
|
|
33
|
-
* This would be a significantly more complex multi-file analysis rule.
|
|
24
|
+
* Use this rule in projects where boolean inputs consistently use booleanAttribute.
|
|
34
25
|
*/
|
|
35
26
|
exports.preferBooleanAttributeShorthandRule = {
|
|
36
27
|
meta: {
|
|
37
28
|
type: 'suggestion',
|
|
38
29
|
docs: {
|
|
39
|
-
description: 'Prefer boolean input attribute shorthand
|
|
30
|
+
description: 'Prefer boolean input attribute shorthand when binding to true (e.g., use "disabled" instead of [disabled]="true").',
|
|
40
31
|
},
|
|
41
32
|
hasSuggestions: true,
|
|
42
|
-
schema: [
|
|
43
|
-
{
|
|
44
|
-
type: 'object',
|
|
45
|
-
properties: {
|
|
46
|
-
allowFalseLiteral: { type: 'boolean' },
|
|
47
|
-
},
|
|
48
|
-
additionalProperties: false,
|
|
49
|
-
},
|
|
50
|
-
],
|
|
33
|
+
schema: [],
|
|
51
34
|
messages: {
|
|
52
|
-
preferTrue: 'Use attribute shorthand "{{attr}}" instead of [{{attr}}]="true".
|
|
53
|
-
preferFalse: 'Avoid binding [{{attr}}]="false"; remove the binding if default is false.',
|
|
35
|
+
preferTrue: 'Use attribute shorthand "{{attr}}" instead of [{{attr}}]="true".',
|
|
54
36
|
suggestTrue: 'Replace with attribute shorthand {{attr}}',
|
|
55
|
-
suggestRemove: 'Remove the false binding',
|
|
56
37
|
},
|
|
57
38
|
},
|
|
58
|
-
defaultOptions: [
|
|
59
|
-
{
|
|
60
|
-
allowFalseLiteral: true, // Changed default to true for safety
|
|
61
|
-
},
|
|
62
|
-
],
|
|
39
|
+
defaultOptions: [],
|
|
63
40
|
create(context) {
|
|
64
|
-
// Robust to missing options: default to [{}] if undefined
|
|
65
|
-
const optionsArr = context.options ?? [{}];
|
|
66
|
-
const options = optionsArr[0] ?? {};
|
|
67
|
-
const allowFalseLiteral = options.allowFalseLiteral ?? true; // Default to true for safety
|
|
68
41
|
const parserServices = (0, utils_1.getTemplateParserServices)(context);
|
|
69
42
|
return {
|
|
70
43
|
BoundAttribute(node) {
|
|
@@ -72,14 +45,14 @@ exports.preferBooleanAttributeShorthandRule = {
|
|
|
72
45
|
if (!value || !value.ast)
|
|
73
46
|
return;
|
|
74
47
|
const ast = value.ast;
|
|
48
|
+
// Only check for boolean literals
|
|
75
49
|
if (ast?.constructor?.name === 'LiteralPrimitive' && typeof ast.value === 'boolean') {
|
|
76
|
-
|
|
77
|
-
const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
|
|
78
|
-
const start = node.sourceSpan.start.offset;
|
|
79
|
-
const end = node.sourceSpan.end.offset;
|
|
50
|
+
// Only warn for [attr]="true", ignore [attr]="false"
|
|
80
51
|
if (ast.value === true) {
|
|
81
|
-
|
|
82
|
-
|
|
52
|
+
const attrName = node.name;
|
|
53
|
+
const loc = parserServices.convertNodeSourceSpanToLoc(node.sourceSpan);
|
|
54
|
+
const start = node.sourceSpan.start.offset;
|
|
55
|
+
const end = node.sourceSpan.end.offset;
|
|
83
56
|
context.report({
|
|
84
57
|
loc,
|
|
85
58
|
messageId: 'preferTrue',
|
|
@@ -93,21 +66,7 @@ exports.preferBooleanAttributeShorthandRule = {
|
|
|
93
66
|
],
|
|
94
67
|
});
|
|
95
68
|
}
|
|
96
|
-
|
|
97
|
-
// Only flag false bindings if explicitly configured
|
|
98
|
-
context.report({
|
|
99
|
-
loc,
|
|
100
|
-
messageId: 'preferFalse',
|
|
101
|
-
data: { attr: attrName },
|
|
102
|
-
suggest: [
|
|
103
|
-
{
|
|
104
|
-
messageId: 'suggestRemove',
|
|
105
|
-
data: { attr: attrName },
|
|
106
|
-
fix: (fixer) => fixer.replaceTextRange([start, end], ''),
|
|
107
|
-
},
|
|
108
|
-
],
|
|
109
|
-
});
|
|
110
|
-
}
|
|
69
|
+
// [attr]="false" is explicitly ignored - no warning
|
|
111
70
|
}
|
|
112
71
|
},
|
|
113
72
|
};
|
package/package.json
CHANGED