@angular-eslint/eslint-plugin 19.7.0-alpha.0 → 19.7.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.
@@ -2,9 +2,10 @@ export type Options = [
2
2
  {
3
3
  readonly requireDescription?: boolean;
4
4
  readonly requireMeaning?: boolean;
5
+ readonly requireCustomId?: boolean | string;
5
6
  }
6
7
  ];
7
- export type MessageIds = 'requireLocalizeDescription' | 'requireLocalizeMeaning';
8
+ export type MessageIds = 'requireLocalizeDescription' | 'requireLocalizeMeaning' | 'requireLocalizeCustomId';
8
9
  export declare const RULE_NAME = "require-localize-metadata";
9
10
  declare const _default: import("@typescript-eslint/utils/ts-eslint").RuleModule<MessageIds, Options, import("../utils/create-eslint-rule").RuleDocs, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
10
11
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"require-localize-metadata.d.ts","sourceRoot":"","sources":["../../src/rules/require-localize-metadata.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,QAAQ,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;QACtC,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;KACnC;CACF,CAAC;AAmBF,MAAM,MAAM,UAAU,GAClB,4BAA4B,GAC5B,wBAAwB,CAAC;AAC7B,eAAO,MAAM,SAAS,8BAA8B,CAAC;;AAErD,wBAuEG"}
1
+ {"version":3,"file":"require-localize-metadata.d.ts","sourceRoot":"","sources":["../../src/rules/require-localize-metadata.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,OAAO,GAAG;IACpB;QACE,QAAQ,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;QACtC,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;QAClC,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;KAC7C;CACF,CAAC;AAYF,MAAM,MAAM,UAAU,GAClB,4BAA4B,GAC5B,wBAAwB,GACxB,yBAAyB,CAAC;AAC9B,eAAO,MAAM,SAAS,8BAA8B,CAAC;;AAErD,wBAyFG"}
@@ -6,12 +6,11 @@ const create_eslint_rule_1 = require("../utils/create-eslint-rule");
6
6
  const DEFAULT_OPTIONS = {
7
7
  requireDescription: false,
8
8
  requireMeaning: false,
9
+ requireCustomId: false,
9
10
  };
10
- const VALID_LOCALIZED_STRING_WITH_DESCRIPTION = new RegExp(/:(.*\|)?([\w\s]+){1}(@@.*)?:.+/);
11
- const VALID_LOCALIZED_STRING_WITH_MEANING = new RegExp(/:([\w\s]+\|)(.*)?(@@.*)?:.+/);
12
11
  const STYLE_GUIDE_LINK = 'https://angular.dev/guide/i18n';
13
- const STYLE_GUIDE_LINK_COMMON_PREPARE = `${STYLE_GUIDE_LINK}-common-prepare`;
14
- const STYLE_GUIDE_LINK_METADATA_FOR_TRANSLATION = `${STYLE_GUIDE_LINK_COMMON_PREPARE}#i18n-metadata-for-translation`;
12
+ const STYLE_GUIDE_LINK_PREPARE = `${STYLE_GUIDE_LINK}/prepare`;
13
+ const STYLE_GUIDE_LINK_METADATA_FOR_TRANSLATION = `${STYLE_GUIDE_LINK_PREPARE}#i18n-metadata-for-translation`;
15
14
  exports.RULE_NAME = 'require-localize-metadata';
16
15
  exports.default = (0, create_eslint_rule_1.createESLintRule)({
17
16
  name: exports.RULE_NAME,
@@ -32,6 +31,10 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
32
31
  type: 'boolean',
33
32
  default: DEFAULT_OPTIONS.requireMeaning,
34
33
  },
34
+ requireCustomId: {
35
+ oneOf: [{ type: 'boolean' }, { type: 'string' }],
36
+ default: DEFAULT_OPTIONS.requireCustomId,
37
+ },
35
38
  },
36
39
  additionalProperties: false,
37
40
  },
@@ -39,35 +42,79 @@ exports.default = (0, create_eslint_rule_1.createESLintRule)({
39
42
  messages: {
40
43
  requireLocalizeDescription: `$localize tagged messages should contain a description. See more at ${STYLE_GUIDE_LINK_METADATA_FOR_TRANSLATION}`,
41
44
  requireLocalizeMeaning: `$localize tagged messages should contain a meaning. See more at ${STYLE_GUIDE_LINK_METADATA_FOR_TRANSLATION}`,
45
+ requireLocalizeCustomId: `$localize tagged messages should contain a custom id{{patternMessage}}. See more at ${STYLE_GUIDE_LINK_METADATA_FOR_TRANSLATION}`,
42
46
  },
43
47
  },
44
48
  defaultOptions: [DEFAULT_OPTIONS],
45
- create(context, [{ requireDescription, requireMeaning }]) {
49
+ create(context, [{ requireDescription, requireMeaning, requireCustomId }]) {
46
50
  return {
47
51
  TaggedTemplateExpression(taggedTemplateExpression) {
48
- if ((requireDescription || requireMeaning) &&
52
+ if ((requireDescription || requireMeaning || requireCustomId) &&
49
53
  utils_1.ASTUtils.isIdentifier(taggedTemplateExpression.tag)) {
50
54
  const identifierName = taggedTemplateExpression.tag.name;
51
55
  const templateElement = taggedTemplateExpression.quasi.quasis[0];
52
56
  if (identifierName === '$localize' && !!templateElement) {
53
- const templateElementRawValue = templateElement.value.raw;
54
- if (requireDescription &&
55
- !VALID_LOCALIZED_STRING_WITH_DESCRIPTION.test(templateElementRawValue)) {
57
+ const metadata = parseMetadata(templateElement.value.raw.trim());
58
+ if (requireDescription && !metadata.description) {
56
59
  context.report({
57
60
  loc: templateElement.loc,
58
61
  messageId: 'requireLocalizeDescription',
59
62
  });
60
63
  }
61
- if (requireMeaning &&
62
- !VALID_LOCALIZED_STRING_WITH_MEANING.test(templateElementRawValue)) {
64
+ if (requireMeaning && !metadata.meaning) {
63
65
  context.report({
64
66
  loc: templateElement.loc,
65
67
  messageId: 'requireLocalizeMeaning',
66
68
  });
67
69
  }
70
+ if (requireCustomId &&
71
+ !(metadata.customId &&
72
+ (typeof requireCustomId === 'string'
73
+ ? RegExp(requireCustomId).test(metadata.customId)
74
+ : true))) {
75
+ context.report({
76
+ loc: templateElement.loc,
77
+ messageId: 'requireLocalizeCustomId',
78
+ data: {
79
+ patternMessage: typeof requireCustomId === 'string'
80
+ ? ` matching the pattern /${requireCustomId}/ on '${metadata.customId}'`
81
+ : '',
82
+ },
83
+ });
84
+ }
68
85
  }
69
86
  }
70
87
  },
71
88
  };
72
89
  },
73
90
  });
91
+ // see https://github.com/angular/angular/blob/main/packages/localize/src/utils/src/messages.ts#L247
92
+ const BLOCK_MARKER = ':';
93
+ const MEANING_SEPARATOR = '|';
94
+ const ID_SEPARATOR = '@@';
95
+ function parseMetadata(rawText) {
96
+ const output = {
97
+ rawText,
98
+ meaning: undefined,
99
+ description: undefined,
100
+ customId: undefined,
101
+ };
102
+ if (rawText.charAt(0) !== BLOCK_MARKER) {
103
+ return output;
104
+ }
105
+ const endOfTheBlock = rawText.lastIndexOf(BLOCK_MARKER);
106
+ if (endOfTheBlock === -1) {
107
+ return output;
108
+ }
109
+ const text = rawText.slice(1, endOfTheBlock);
110
+ const [meaningAndDesc, customId] = text.split(ID_SEPARATOR, 2);
111
+ let [meaning, description] = meaningAndDesc.split(MEANING_SEPARATOR, 2);
112
+ if (description === undefined) {
113
+ description = meaning;
114
+ meaning = undefined;
115
+ }
116
+ if (description === '') {
117
+ description = undefined;
118
+ }
119
+ return { rawText, meaning, description, customId };
120
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/eslint-plugin",
3
- "version": "19.7.0-alpha.0",
3
+ "version": "19.7.0",
4
4
  "description": "ESLint plugin for Angular applications, following https://angular.dev/style-guide",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -18,11 +18,11 @@
18
18
  "LICENSE"
19
19
  ],
20
20
  "dependencies": {
21
- "@angular-eslint/bundled-angular-compiler": "19.7.0-alpha.0",
22
- "@angular-eslint/utils": "19.7.0-alpha.0"
21
+ "@angular-eslint/bundled-angular-compiler": "19.7.0",
22
+ "@angular-eslint/utils": "19.7.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@angular-eslint/test-utils": "19.7.0-alpha.0"
25
+ "@angular-eslint/test-utils": "19.7.0"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "@typescript-eslint/utils": "^7.11.0 || ^8.0.0",