@discord/eslint-plugin-discord-intl 0.30.0 → 0.32.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/index.js CHANGED
@@ -16,6 +16,7 @@ module.exports = {
16
16
 
17
17
  'use-static-access': require('./rules/use-static-access'),
18
18
  'no-opaque-messages-objects': require('./rules/no-opaque-messages-objects'),
19
+ 'no-invalid-meta': require('./rules/no-invalid-meta'),
19
20
  },
20
21
  configs: {
21
22
  recommended: {
@@ -38,6 +39,7 @@ module.exports = {
38
39
  // JS rules
39
40
  '@discord/discord-intl/use-static-access': 'error',
40
41
  '@discord/discord-intl/no-opaque-messages-objects': 'error',
42
+ // '@discord/discord-intl/no-invalid-meta': 'error',
41
43
  },
42
44
  },
43
45
  },
@@ -13,7 +13,8 @@ const {
13
13
  IntlMessagesDatabase,
14
14
  IntlDatabaseInsertStrategy,
15
15
  } = require('@discord/intl-message-database');
16
- /** @type {Map<SourceCode, Record<string, IntlDiagnostic[]>>} */
16
+ /** @typedef {{ sourceFileKey: string, validations: Record<string, IntlDiagnostic[]> }} FileValidations */
17
+ /** @type {Map<SourceCode, FileValidations>} */
17
18
  const FILE_VALIDATIONS = new Map();
18
19
 
19
20
  /** @type {Map<string, IntlMessagesDatabase>} */
@@ -50,7 +51,7 @@ function ensureInitialized(directory) {
50
51
  * @param {SourceCode} sourceCode
51
52
  * @param {string} fileName
52
53
  * @param {string} content
53
- * @return {Record<string, IntlDiagnostic[]>}
54
+ * @return {FileValidations}
54
55
  */
55
56
  function processAndValidateNative(database, sourceCode, fileName, content) {
56
57
  const fileKey = crypto.hash('sha1', content);
@@ -67,6 +68,10 @@ function processAndValidateNative(database, sourceCode, fileName, content) {
67
68
  'en-US',
68
69
  IntlDatabaseInsertStrategy.Update,
69
70
  );
71
+ // The database interns file paths and may store a different spelling than the one we passed in
72
+ // (on Windows it normalizes the drive letter and separators). Every diagnostic it hands back is
73
+ // tagged with _its_ spelling, so match on this rather than on `processingFileName`.
74
+ const sourceFileKey = processResult.fileKey;
70
75
 
71
76
  /** @type {Record<string, IntlDiagnostic[]>} */
72
77
  const validations = {};
@@ -77,7 +82,7 @@ function processAndValidateNative(database, sourceCode, fileName, content) {
77
82
  name: 'Processing::' + error.name,
78
83
  description: error.message,
79
84
  key: error.key ?? 'file',
80
- file: error.file ?? processingFileName,
85
+ file: error.file ?? sourceFileKey,
81
86
  messageLine: error.line ?? 0,
82
87
  messageCol: error.col ?? 0,
83
88
  start: 0,
@@ -89,13 +94,14 @@ function processAndValidateNative(database, sourceCode, fileName, content) {
89
94
  }
90
95
 
91
96
  for (const diagnostic of database.validateMessages()) {
92
- if (diagnostic.file === processingFileName) {
97
+ if (diagnostic.file === sourceFileKey) {
93
98
  (validations[diagnostic.key] ??= []).push(diagnostic);
94
99
  }
95
100
  }
96
101
 
97
- FILE_VALIDATIONS.set(sourceCode, validations);
98
- return validations;
102
+ const result = { sourceFileKey, validations };
103
+ FILE_VALIDATIONS.set(sourceCode, result);
104
+ return result;
99
105
  }
100
106
 
101
107
  /**
@@ -111,7 +117,7 @@ function traverseAndReportMatchingNativeValidations(context, predicate) {
111
117
 
112
118
  const projectDirectory = context.settings['intl']?.['projectDirectory'] ?? context.cwd;
113
119
  const database = ensureInitialized(projectDirectory);
114
- const validations = processAndValidateNative(
120
+ const { sourceFileKey, validations } = processAndValidateNative(
115
121
  database,
116
122
  context.sourceCode,
117
123
  context.filename,
@@ -133,7 +139,7 @@ function traverseAndReportMatchingNativeValidations(context, predicate) {
133
139
  // log the diagnostic to be able to investigate further when it happens.
134
140
  const textLen = context.sourceCode.text.length;
135
141
  if (
136
- diagnostic.file !== context.physicalFilename ||
142
+ diagnostic.file !== sourceFileKey ||
137
143
  messageOffset + diagnostic.start > textLen ||
138
144
  messageOffset + diagnostic.end > textLen
139
145
  ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/eslint-plugin-discord-intl",
3
- "version": "0.30.0",
3
+ "version": "0.32.0",
4
4
  "license": "MIT",
5
5
  "type": "commonjs",
6
6
  "description": "ESLint plugin for validating and linting messages and translations using @discord/intl",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@typescript-eslint/scope-manager": "^8.8.1",
14
- "@discord/intl-message-database": "0.30.0"
14
+ "@discord/intl-message-database": "0.32.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@types/eslint": "^8.56.12",
@@ -0,0 +1,65 @@
1
+ const { isDefinitionsFile } = require('../lib/traverse');
2
+
3
+ module.exports = /** @type {import('eslint').Rule.RuleModule} */ ({
4
+ meta: {
5
+ docs: {
6
+ description:
7
+ "Check that meta information is defined correctly and won't be silently ignored while processing.",
8
+ category: 'Correctness',
9
+ },
10
+ messages: {
11
+ metaMustBeInitialized: '`meta` must have an initializer',
12
+ metaMustBeObjectLiteral: '`meta` must be defined using an object literal',
13
+ disallowSpread: 'meta properties must be defined inline',
14
+ disallowComputedKey: 'meta properties cannot be defined using computed keys',
15
+ invalidTagsValue:
16
+ '`tags` must be initialized using an array literal containing only string literals',
17
+ },
18
+ },
19
+ create(context) {
20
+ if (!isDefinitionsFile(context.filename, context.sourceCode.text)) return {};
21
+
22
+ return {
23
+ /**
24
+ * @param {import('estree').VariableDeclarator} node
25
+ */
26
+ 'VariableDeclarator[id.name="meta"]'(node) {
27
+ if (node.init == null) {
28
+ context.report({ node, messageId: 'metaMustBeInitialized' });
29
+ return;
30
+ }
31
+ if (node.init.type !== 'ObjectExpression') {
32
+ context.report({ node, messageId: 'metaMustBeObjectLiteral' });
33
+ return;
34
+ }
35
+
36
+ const initializer = node.init;
37
+
38
+ for (const property of initializer.properties) {
39
+ if (property.type === 'SpreadElement') {
40
+ context.report({ node: property, messageId: 'disallowSpread' });
41
+ continue;
42
+ }
43
+ if (property.computed || property.key.type !== 'Identifier') {
44
+ context.report({ node: property, messageId: 'disallowComputedKey' });
45
+ continue;
46
+ }
47
+ switch (property.key.name) {
48
+ // Tags are more than just a single literal. Every element of the array must also be a
49
+ // string literal for it to be valid.
50
+ case 'tags':
51
+ if (property.value.type !== 'ArrayExpression') {
52
+ context.report({ node: property.value, messageId: 'invalidTagsValue' });
53
+ continue;
54
+ }
55
+ for (const tag of property.value.elements) {
56
+ if (tag == null || tag.type !== 'Literal' || typeof tag.value !== 'string') {
57
+ context.report({ node: tag ?? property.value, messageId: 'invalidTagsValue' });
58
+ }
59
+ }
60
+ }
61
+ }
62
+ },
63
+ };
64
+ },
65
+ });
@@ -0,0 +1,126 @@
1
+ const { RuleTester } = require('eslint');
2
+ const noInvalidMeta = require('./no-invalid-meta');
3
+
4
+ const ruleTester = new RuleTester({
5
+ // Must use at least ecmaVersion 2015 because
6
+ // that's when `const` variables were introduced.
7
+ // Using 2022 to make sure the spread operator is supported.
8
+ parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
9
+ });
10
+
11
+ // @ts-ignore
12
+ const errorMessages = noInvalidMeta.meta.messages ?? {};
13
+
14
+ ruleTester.run('no-invalid-meta', noInvalidMeta, {
15
+ valid: [
16
+ {
17
+ name: 'no meta defined',
18
+ code: `
19
+ import {defineMessages} from '@discord/intl';
20
+ export default defineMessages({HELLO: "world"});
21
+ `,
22
+ },
23
+ {
24
+ name: 'not a messages file',
25
+ code: `
26
+ export const meta = {foobar: thisWouldBeInvalid};
27
+ `,
28
+ },
29
+ {
30
+ name: 'all literals',
31
+ code: `
32
+ import {defineMessages} from '@discord/intl';
33
+ export const meta = {
34
+ project: 'some-feature',
35
+ secret: false,
36
+ translate: true,
37
+ translationsPath: './translations',
38
+ };
39
+
40
+ export default defineMessages({HELLO: "world"});
41
+ `,
42
+ },
43
+ ],
44
+ invalid: [
45
+ {
46
+ name: 'no spread',
47
+ code: `
48
+ import {defineMessages} from '@discord/intl';
49
+ export const meta = {
50
+ ...something,
51
+ };
52
+
53
+ export default defineMessages({HELLO: "world"});
54
+ `,
55
+ errors: [errorMessages.disallowSpread],
56
+ },
57
+ {
58
+ name: 'no computed key',
59
+ code: `
60
+ import {defineMessages} from '@discord/intl';
61
+ export const meta = {
62
+ [computed]: true,
63
+ };
64
+
65
+ export default defineMessages({HELLO: "world"});
66
+ `,
67
+ errors: [errorMessages.disallowComputedKey],
68
+ },
69
+ {
70
+ name: 'no empty tags',
71
+ code: `
72
+ import {defineMessages} from '@discord/intl';
73
+ export const meta = {
74
+ tags: [,'foo']
75
+ };
76
+
77
+ export default defineMessages({HELLO: "world"});
78
+ `,
79
+ errors: [errorMessages.invalidTagsValue],
80
+ },
81
+ {
82
+ name: 'no variable tags',
83
+ code: `
84
+ import {defineMessages} from '@discord/intl';
85
+ export const meta = {
86
+ tags: [aVariable]
87
+ };
88
+
89
+ export default defineMessages({HELLO: "world"});
90
+ `,
91
+ errors: [errorMessages.invalidTagsValue],
92
+ },
93
+ {
94
+ name: 'no missing meta initializer',
95
+ code: `
96
+ import {defineMessages} from '@discord/intl';
97
+ export let meta;
98
+
99
+ export default defineMessages({HELLO: "world"});
100
+ `,
101
+ errors: [errorMessages.metaMustBeInitialized],
102
+ },
103
+ {
104
+ name: 'require object literal initializer',
105
+ code: `
106
+ import {defineMessages} from '@discord/intl';
107
+ export const meta = false;
108
+
109
+ export default defineMessages({HELLO: "world"});
110
+ `,
111
+ errors: [errorMessages.metaMustBeObjectLiteral],
112
+ },
113
+ {
114
+ name: 'require object literal initializer',
115
+ code: `
116
+ import {defineMessages} from '@discord/intl';
117
+ export const meta = someObject;
118
+
119
+ export default defineMessages({HELLO: "world"});
120
+ `,
121
+ errors: [errorMessages.metaMustBeObjectLiteral],
122
+ },
123
+ ],
124
+ });
125
+
126
+ console.log('All tests passed!');