@angular-eslint/eslint-plugin-template 20.6.1-alpha.6 → 20.6.1-alpha.7

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
@@ -68,7 +68,7 @@ Please see https://github.com/angular-eslint/angular-eslint for full usage instr
68
68
  | [`prefer-built-in-pipes`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-built-in-pipes.md) | Encourages the use of Angular built-in pipes (e.g. lowercase, uppercase, titlecase) instead of certain JavaScript methods in Angular templates. | | | | |
69
69
  | [`prefer-contextual-for-variables`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-contextual-for-variables.md) | Ensures that contextual variables are used in @for blocks where possible instead of aliasing them. | | :wrench: | | |
70
70
  | [`prefer-control-flow`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-control-flow.md) | Ensures that the built-in control flow is used. | | | | |
71
- | [`prefer-ngsrc`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-ngsrc.md) | Ensures ngSrc is used instead of src for img elements | | | | |
71
+ | [`prefer-ngsrc`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-ngsrc.md) | Ensures ngSrc is used instead of src for img elements | | | :bulb: | |
72
72
  | [`prefer-template-literal`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/prefer-template-literal.md) | Ensure that template literals are used instead of concatenating strings or expressions. | | :wrench: | | |
73
73
  | [`role-has-required-aria`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/role-has-required-aria.md) | [Accessibility] Ensures elements with ARIA roles have all required properties for that role. | | | :bulb: | :accessibility: |
74
74
  | [`table-scope`](https://github.com/angular-eslint/angular-eslint/blob/main/packages/eslint-plugin-template/docs/rules/table-scope.md) | [Accessibility] Ensures that the `scope` attribute is only used on the `<th>` element | | :wrench: | | :accessibility: |
@@ -1,5 +1,5 @@
1
1
  export type Options = [];
2
- export type MessageIds = 'missingAttribute' | 'invalidDoubleSource';
2
+ export type MessageIds = 'missingAttribute' | 'invalidDoubleSource' | 'suggestReplaceWithNgSrc' | 'suggestRemoveSrc';
3
3
  export declare const RULE_NAME = "prefer-ngsrc";
4
4
  declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, [], import("../utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
5
5
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"prefer-ngsrc.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-ngsrc.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,OAAO,GAAG,EAAE,CAAC;AACzB,MAAM,MAAM,UAAU,GAAG,kBAAkB,GAAG,qBAAqB,CAAC;AACpE,eAAO,MAAM,SAAS,iBAAiB,CAAC;;AAExC,wBA4CG;AAqDH,eAAO,MAAM,mBAAmB;;CAG/B,CAAC"}
1
+ {"version":3,"file":"prefer-ngsrc.d.ts","sourceRoot":"","sources":["../../src/rules/prefer-ngsrc.ts"],"names":[],"mappings":"AAWA,MAAM,MAAM,OAAO,GAAG,EAAE,CAAC;AACzB,MAAM,MAAM,UAAU,GAClB,kBAAkB,GAClB,qBAAqB,GACrB,yBAAyB,GACzB,kBAAkB,CAAC;AACvB,eAAO,MAAM,SAAS,iBAAiB,CAAC;;AAExC,wBA4HG;AAqDH,eAAO,MAAM,mBAAmB;;CAG/B,CAAC"}
@@ -12,15 +12,74 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
12
12
  docs: {
13
13
  description: 'Ensures ngSrc is used instead of src for img elements',
14
14
  },
15
+ hasSuggestions: true,
15
16
  schema: [],
16
17
  messages: {
17
18
  missingAttribute: 'The attribute [ngSrc] should be used for img elements instead of [src].',
18
19
  invalidDoubleSource: 'Only [ngSrc] should exist on an img element. Delete the [src] attribute.',
20
+ suggestReplaceWithNgSrc: 'Replace [src] with [ngSrc]',
21
+ suggestRemoveSrc: 'Remove the [src] attribute',
19
22
  },
20
23
  },
21
24
  defaultOptions: [],
22
25
  create(context) {
23
26
  const parserServices = (0, utils_1.getTemplateParserServices)(context);
27
+ const sourceCode = context.sourceCode;
28
+ function reportMissingNgSrc(srcAttribute) {
29
+ const loc = parserServices.convertNodeSourceSpanToLoc(srcAttribute.sourceSpan);
30
+ context.report({
31
+ loc,
32
+ messageId: 'missingAttribute',
33
+ suggest: [
34
+ {
35
+ messageId: 'suggestReplaceWithNgSrc',
36
+ fix: (fixer) => {
37
+ const originalAttribute = sourceCode
38
+ .getText()
39
+ .slice(srcAttribute.sourceSpan.start.offset, srcAttribute.sourceSpan.end.offset);
40
+ let updatedAttribute;
41
+ if (originalAttribute.startsWith('[attr.src]')) {
42
+ updatedAttribute = originalAttribute.replace(/^\[attr\.src]/, '[ngSrc]');
43
+ }
44
+ else if (originalAttribute.startsWith('[src]')) {
45
+ updatedAttribute = originalAttribute.replace(/^\[src]/, '[ngSrc]');
46
+ }
47
+ else {
48
+ updatedAttribute = originalAttribute.replace(/^src/, 'ngSrc');
49
+ }
50
+ return fixer.replaceTextRange([
51
+ srcAttribute.sourceSpan.start.offset,
52
+ srcAttribute.sourceSpan.end.offset,
53
+ ], updatedAttribute);
54
+ },
55
+ },
56
+ ],
57
+ });
58
+ }
59
+ function reportDoubleSrc(srcAttribute) {
60
+ const loc = parserServices.convertNodeSourceSpanToLoc(srcAttribute.sourceSpan);
61
+ context.report({
62
+ loc,
63
+ messageId: 'invalidDoubleSource',
64
+ suggest: [
65
+ {
66
+ messageId: 'suggestRemoveSrc',
67
+ fix: (fixer) => {
68
+ const fullText = sourceCode.getText();
69
+ let startOffset = srcAttribute.sourceSpan.start.offset;
70
+ // Move back to include preceding whitespace
71
+ while (startOffset > 0 && /\s/.test(fullText[startOffset - 1])) {
72
+ startOffset--;
73
+ }
74
+ return fixer.removeRange([
75
+ startOffset,
76
+ srcAttribute.sourceSpan.end.offset,
77
+ ]);
78
+ },
79
+ },
80
+ ],
81
+ });
82
+ }
24
83
  return {
25
84
  'Element[name=/^img$/i]'(element) {
26
85
  const ngSrcAttribute = getNgSrcAttribute(element);
@@ -29,13 +88,12 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
29
88
  (!ngSrcAttribute && isSrcBase64Image(srcAttribute))) {
30
89
  return;
31
90
  }
32
- const loc = parserServices.convertNodeSourceSpanToLoc(srcAttribute.sourceSpan);
33
- context.report({
34
- loc,
35
- messageId: !ngSrcAttribute
36
- ? 'missingAttribute'
37
- : 'invalidDoubleSource',
38
- });
91
+ if (!ngSrcAttribute) {
92
+ reportMissingNgSrc(srcAttribute);
93
+ }
94
+ else {
95
+ reportDoubleSrc(srcAttribute);
96
+ }
39
97
  },
40
98
  };
41
99
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/eslint-plugin-template",
3
- "version": "20.6.1-alpha.6",
3
+ "version": "20.6.1-alpha.7",
4
4
  "description": "ESLint plugin for Angular Templates",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -20,19 +20,19 @@
20
20
  "dependencies": {
21
21
  "aria-query": "5.3.2",
22
22
  "axobject-query": "4.1.0",
23
- "@angular-eslint/bundled-angular-compiler": "20.6.1-alpha.6",
24
- "@angular-eslint/utils": "20.6.1-alpha.6"
23
+ "@angular-eslint/bundled-angular-compiler": "20.6.1-alpha.7",
24
+ "@angular-eslint/utils": "20.6.1-alpha.7"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/aria-query": "5.0.4",
28
- "@angular-eslint/test-utils": "20.6.1-alpha.6"
28
+ "@angular-eslint/test-utils": "20.6.1-alpha.7"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@typescript-eslint/types": "^7.11.0 || ^8.0.0",
32
32
  "@typescript-eslint/utils": "^7.11.0 || ^8.0.0",
33
33
  "eslint": "^8.57.0 || ^9.0.0",
34
34
  "typescript": "*",
35
- "@angular-eslint/template-parser": "20.6.1-alpha.6"
35
+ "@angular-eslint/template-parser": "20.6.1-alpha.7"
36
36
  },
37
37
  "gitHead": "e2006e5e9c99e5a943d1a999e0efa5247d29ec24"
38
38
  }