@discord/eslint-plugin-discord-intl 0.27.3 → 0.27.4

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.
@@ -4,30 +4,38 @@
4
4
  /** @typedef {import('eslint').Rule.ReportDescriptorLocation} ReportDescriptorLocation */
5
5
  /** @typedef {import('eslint').AST.SourceLocation} SourceLocation */
6
6
  /** @typedef {import('eslint').SourceCode} SourceCode */
7
- /** @typedef {import("@discord/intl-loader-core/types").IntlDiagnostic} IntlDiagnostic */
7
+ /** @typedef {import("@discord/intl-message-database").IntlDiagnostic} IntlDiagnostic */
8
8
 
9
9
  const crypto = require('node:crypto');
10
- const {
11
- processDefinitionsFile,
12
- database,
13
- processAllMessagesFiles,
14
- findAllMessagesFiles,
15
- } = require('@discord/intl-loader-core');
10
+ const path = require('node:path');
16
11
  const { traverseMessageDefinitions, isDefinitionsFile } = require('./traverse');
17
- const diagnostics_channel = require('node:diagnostics_channel');
12
+ const {
13
+ IntlMessagesDatabase,
14
+ IntlDatabaseInsertStrategy,
15
+ } = require('@discord/intl-message-database');
18
16
  /** @type {Map<SourceCode, Record<string, IntlDiagnostic[]>>} */
19
17
  const FILE_VALIDATIONS = new Map();
20
18
 
21
- let isInitialized = false;
19
+ /** @type {Map<string, IntlMessagesDatabase>} */
20
+ const PROJECT_DATABASE_MAP = new Map();
22
21
 
23
22
  /**
24
23
  * @param {string} directory
24
+ * @returns {IntlMessagesDatabase}
25
25
  */
26
26
  function ensureInitialized(directory) {
27
- if (isInitialized) return;
27
+ const key = path.resolve(directory);
28
+ const existing = PROJECT_DATABASE_MAP.get(key);
29
+ if (existing != null) return existing;
30
+
31
+ const database = new IntlMessagesDatabase();
32
+ database.processAllMessagesFiles(
33
+ database.findAllMessagesFiles([directory], 'en-US'),
34
+ IntlDatabaseInsertStrategy.Create,
35
+ );
28
36
 
29
- processAllMessagesFiles(findAllMessagesFiles([directory]));
30
- isInitialized = true;
37
+ PROJECT_DATABASE_MAP.set(key, database);
38
+ return database;
31
39
  }
32
40
 
33
41
  /**
@@ -38,12 +46,13 @@ function ensureInitialized(directory) {
38
46
  * validations, allowing them to be reused across multiple rules without wasting work validating
39
47
  * the same messages multiple times.
40
48
  *
49
+ * @param {IntlMessagesDatabase} database
41
50
  * @param {SourceCode} sourceCode
42
51
  * @param {string} fileName
43
52
  * @param {string} content
44
53
  * @return {Record<string, IntlDiagnostic[]>}
45
54
  */
46
- function processAndValidateNative(sourceCode, fileName, content) {
55
+ function processAndValidateNative(database, sourceCode, fileName, content) {
47
56
  const fileKey = crypto.hash('sha1', content);
48
57
  const existing = FILE_VALIDATIONS.get(sourceCode);
49
58
  if (existing != null) return existing;
@@ -52,30 +61,31 @@ function processAndValidateNative(sourceCode, fileName, content) {
52
61
  // native database that this is a JS message definition _somehow_, so we'll make a random
53
62
  // name that should be relatively unique.
54
63
  const processingFileName = fileName === '<input>' ? `${fileKey}.messages.js` : fileName;
55
- const processResult = processDefinitionsFile(processingFileName, content, {
56
- processTranslations: false,
57
- });
64
+ const processResult = database.processDefinitionsFileContent(
65
+ processingFileName,
66
+ content,
67
+ 'en-US',
68
+ IntlDatabaseInsertStrategy.Update,
69
+ );
58
70
 
59
71
  /** @type {Record<string, IntlDiagnostic[]>} */
60
72
  const validations = {};
61
73
 
62
- if (!processResult.succeeded) {
63
- for (const error of processResult.errors) {
64
- const key = error.key ?? 'file';
65
- (validations[key] ??= []).push({
66
- name: 'Processing::' + error.name,
67
- description: error.message,
68
- key: error.key ?? 'file',
69
- file: error.file ?? processingFileName,
70
- messageLine: error.line ?? 0,
71
- messageCol: error.col ?? 0,
72
- start: 0,
73
- end: content.length,
74
- locale: error.locale ?? 'definition',
75
- category: 'correctness',
76
- fixes: [],
77
- });
78
- }
74
+ for (const error of processResult.errors) {
75
+ const key = error.key ?? 'file';
76
+ (validations[key] ??= []).push({
77
+ name: 'Processing::' + error.name,
78
+ description: error.message,
79
+ key: error.key ?? 'file',
80
+ file: error.file ?? processingFileName,
81
+ messageLine: error.line ?? 0,
82
+ messageCol: error.col ?? 0,
83
+ start: 0,
84
+ end: 2,
85
+ locale: error.locale ?? 'definition',
86
+ category: 'correctness',
87
+ fixes: [],
88
+ });
79
89
  }
80
90
 
81
91
  for (const diagnostic of database.validateMessages()) {
@@ -100,8 +110,9 @@ function traverseAndReportMatchingNativeValidations(context, predicate) {
100
110
  if (!isDefinitionsFile(context.filename, context.sourceCode.text)) return {};
101
111
 
102
112
  const projectDirectory = context.settings['intl']?.['projectDirectory'] ?? context.cwd;
103
- ensureInitialized(projectDirectory);
113
+ const database = ensureInitialized(projectDirectory);
104
114
  const validations = processAndValidateNative(
115
+ database,
105
116
  context.sourceCode,
106
117
  context.filename,
107
118
  context.sourceCode.text,
package/lib/traverse.js CHANGED
@@ -1,4 +1,4 @@
1
- const { isMessageDefinitionsFile } = require('@discord/intl-loader-core');
1
+ const { isMessageDefinitionsFile } = require('@discord/intl-message-database');
2
2
 
3
3
  /** @typedef {import('eslint').Rule.NodeParentExtension} NodeParentExtension */
4
4
  /** @typedef {import('eslint').SourceCode} SourceCode */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/eslint-plugin-discord-intl",
3
- "version": "0.27.3",
3
+ "version": "0.27.4",
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-loader-core": "0.27.3"
14
+ "@discord/intl-message-database": "0.27.4"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@types/eslint": "^8.56.12",