auto-translate-json-library 1.5.4 → 2.0.2
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 +1141 -148
- package/build/package.json +97 -21
- package/build/src/aws.d.ts +1 -1
- package/build/src/aws.js +27 -38
- package/build/src/azure.d.ts +1 -1
- package/build/src/azure.js +50 -63
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +196 -37
- package/build/src/config.d.ts +1 -0
- package/build/src/config.js +1 -2
- package/build/src/deepl.d.ts +1 -1
- package/build/src/deepl.js +18 -64
- package/build/src/files.d.ts +11 -2
- package/build/src/files.js +356 -69
- package/build/src/folderFiles.d.ts +6 -3
- package/build/src/folderFiles.js +33 -44
- package/build/src/format/arb-handler.d.ts +33 -0
- package/build/src/format/arb-handler.js +679 -0
- package/build/src/format/base-format-handler.d.ts +75 -0
- package/build/src/format/base-format-handler.js +166 -0
- package/build/src/format/csv-handler.d.ts +38 -0
- package/build/src/format/csv-handler.js +604 -0
- package/build/src/format/index.d.ts +17 -0
- package/build/src/format/index.js +48 -0
- package/build/src/format/json-handler.d.ts +17 -0
- package/build/src/format/json-handler.js +294 -0
- package/build/src/format/po-handler.d.ts +17 -0
- package/build/src/format/po-handler.js +338 -0
- package/build/src/format/po-utils.d.ts +86 -0
- package/build/src/format/po-utils.js +385 -0
- package/build/src/format/pot-handler.d.ts +22 -0
- package/build/src/format/pot-handler.js +331 -0
- package/build/src/format/properties-handler.d.ts +26 -0
- package/build/src/format/properties-handler.js +500 -0
- package/build/src/format/tsv-handler.d.ts +13 -0
- package/build/src/format/tsv-handler.js +67 -0
- package/build/src/format/xliff-handler.d.ts +24 -0
- package/build/src/format/xliff-handler.js +433 -0
- package/build/src/format/xmb-handler.d.ts +33 -0
- package/build/src/format/xmb-handler.js +484 -0
- package/build/src/format/xmb-xtb-utils.d.ts +43 -0
- package/build/src/format/xmb-xtb-utils.js +180 -0
- package/build/src/format/xml-handler.d.ts +28 -0
- package/build/src/format/xml-handler.js +561 -0
- package/build/src/format/xtb-handler.d.ts +33 -0
- package/build/src/format/xtb-handler.js +518 -0
- package/build/src/format/yaml-handler.d.ts +26 -0
- package/build/src/format/yaml-handler.js +522 -0
- package/build/src/format-detector.d.ts +7 -0
- package/build/src/format-detector.js +58 -0
- package/build/src/format-handler-factory.d.ts +9 -0
- package/build/src/format-handler-factory.js +18 -0
- package/build/src/format.interface.d.ts +56 -0
- package/build/src/format.interface.js +1 -0
- package/build/src/google.d.ts +1 -1
- package/build/src/google.js +32 -49
- package/build/src/index.d.ts +4 -3
- package/build/src/index.js +15 -17
- package/build/src/lib.d.ts +1 -1
- package/build/src/lib.js +188 -133
- package/build/src/openai.d.ts +2 -2
- package/build/src/openai.js +59 -50
- package/build/src/translate.interface.d.ts +1 -1
- package/build/src/translate.interface.js +1 -2
- package/build/src/util.js +4 -9
- package/build/src/validation/enhanced-validation-result.d.ts +96 -0
- package/build/src/validation/enhanced-validation-result.js +198 -0
- package/build/src/validation/error-messages.d.ts +88 -0
- package/build/src/validation/error-messages.js +436 -0
- package/build/src/validation/error-recovery.d.ts +78 -0
- package/build/src/validation/error-recovery.js +593 -0
- package/build/src/validation/example-usage.d.ts +31 -0
- package/build/src/validation/example-usage.js +148 -0
- package/build/src/validation/format-rules.d.ts +37 -0
- package/build/src/validation/format-rules.js +550 -0
- package/build/src/validation/format-validator.d.ts +70 -0
- package/build/src/validation/format-validator.js +279 -0
- package/build/src/validation/index.d.ts +13 -0
- package/build/src/validation/index.js +30 -0
- package/build/src/validation/validation-service.d.ts +56 -0
- package/build/src/validation/validation-service.js +262 -0
- package/build/tests/arb-handler.test.d.ts +1 -0
- package/build/tests/arb-handler.test.js +694 -0
- package/build/tests/cli-integration.test.d.ts +1 -0
- package/build/tests/cli-integration.test.js +352 -0
- package/build/tests/csv-handler.test.d.ts +1 -0
- package/build/tests/csv-handler.test.js +507 -0
- package/build/tests/error-handling.test.d.ts +1 -0
- package/build/tests/error-handling.test.js +456 -0
- package/build/tests/files-integration.test.d.ts +1 -0
- package/build/tests/files-integration.test.js +421 -0
- package/build/tests/json-handler.test.d.ts +1 -0
- package/build/tests/json-handler.test.js +526 -0
- package/build/tests/po-handler.test.d.ts +1 -0
- package/build/tests/po-handler.test.js +768 -0
- package/build/tests/properties-handler.test.d.ts +1 -0
- package/build/tests/properties-handler.test.js +316 -0
- package/build/tests/setup.d.ts +1 -0
- package/build/tests/setup.js +5 -0
- package/build/tests/xliff-handler.test.d.ts +1 -0
- package/build/tests/xliff-handler.test.js +459 -0
- package/build/tests/xmb-xtb-handler.test.d.ts +1 -0
- package/build/tests/xmb-xtb-handler.test.js +758 -0
- package/build/tests/xml-handler.test.d.ts +1 -0
- package/build/tests/xml-handler.test.js +570 -0
- package/build/tests/yaml-handler.test.d.ts +1 -0
- package/build/tests/yaml-handler.test.js +156 -0
- package/package.json +97 -21
package/build/src/openai.js
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.OpenAITranslate = void 0;
|
|
13
|
-
const openai_1 = require("openai");
|
|
14
|
-
const util_1 = require("./util");
|
|
1
|
+
import { OpenAI } from "openai";
|
|
2
|
+
import { Util } from "./util.js";
|
|
15
3
|
const supportedLanguages = {
|
|
16
4
|
af: "Afrikaans",
|
|
17
5
|
sq: "Albanian",
|
|
@@ -122,7 +110,15 @@ const supportedLanguages = {
|
|
|
122
110
|
yo: "Yoruba",
|
|
123
111
|
zu: "Zulu",
|
|
124
112
|
};
|
|
125
|
-
class OpenAITranslate {
|
|
113
|
+
export class OpenAITranslate {
|
|
114
|
+
model;
|
|
115
|
+
maxTokens;
|
|
116
|
+
temperature;
|
|
117
|
+
topP;
|
|
118
|
+
n;
|
|
119
|
+
frequencyPenalty;
|
|
120
|
+
presencePenalty;
|
|
121
|
+
openai;
|
|
126
122
|
constructor(apiKey, baseUrl, model, maxTokens, temperature, topP, n, frequencyPenalty, presencePenalty) {
|
|
127
123
|
this.model = model;
|
|
128
124
|
this.maxTokens = maxTokens;
|
|
@@ -135,47 +131,60 @@ class OpenAITranslate {
|
|
|
135
131
|
apiKey: apiKey,
|
|
136
132
|
baseURL: baseUrl,
|
|
137
133
|
};
|
|
138
|
-
this.openai = new
|
|
134
|
+
this.openai = new OpenAI(configuration);
|
|
139
135
|
}
|
|
140
136
|
isValidLocale(targetLocale) {
|
|
141
137
|
return targetLocale in supportedLanguages;
|
|
142
138
|
}
|
|
143
|
-
translateText(text, sourceLocale, targetLocale) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
139
|
+
async translateText(text, sourceLocale, targetLocale, context) {
|
|
140
|
+
if (sourceLocale !== "en") {
|
|
141
|
+
throw Error(`${sourceLocale} is not supported.Currently we support just English `);
|
|
142
|
+
}
|
|
143
|
+
// DEBUG: Log what text is being sent for translation
|
|
144
|
+
console.log(`🔍 DEBUG - Translating text: "${text}" (length: ${text.length})`);
|
|
145
|
+
console.log(`🔍 DEBUG - Source locale: ${sourceLocale}, Target locale: ${targetLocale}`);
|
|
146
|
+
if (context) {
|
|
147
|
+
console.log(`🔍 DEBUG - Context: ${context}`);
|
|
148
|
+
}
|
|
149
|
+
let result = "";
|
|
150
|
+
let args;
|
|
151
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
152
|
+
// DEBUG: Log text after context variable replacement
|
|
153
|
+
console.log(`🔍 DEBUG - Text after context replacement: "${text}" (length: ${text.length})`);
|
|
154
|
+
let systemPrompt = `You will be provided with a sentence or words in English, and your task is to translate it into ${supportedLanguages[targetLocale]}. Return ONLY the translation, without any additional text or explanations.`;
|
|
155
|
+
if (context) {
|
|
156
|
+
systemPrompt += `\nContext for the translation: ${context}`;
|
|
157
|
+
}
|
|
158
|
+
const userPrompt = text;
|
|
159
|
+
// DEBUG: Log the prompts being sent
|
|
160
|
+
console.log(`🔍 DEBUG - System prompt: "${systemPrompt}"`);
|
|
161
|
+
console.log(`🔍 DEBUG - User prompt: "${userPrompt}"`);
|
|
162
|
+
const response = await this.openai.chat.completions.create({
|
|
163
|
+
model: this.model,
|
|
164
|
+
messages: [
|
|
165
|
+
{ role: "system", content: systemPrompt },
|
|
166
|
+
{ role: "user", content: userPrompt },
|
|
167
|
+
],
|
|
168
|
+
temperature: this.temperature,
|
|
169
|
+
max_tokens: this.maxTokens,
|
|
170
|
+
top_p: this.topP,
|
|
171
|
+
n: this.n,
|
|
172
|
+
frequency_penalty: this.frequencyPenalty,
|
|
173
|
+
presence_penalty: this.presencePenalty,
|
|
174
|
+
});
|
|
175
|
+
if (response.choices[0].message.content !== null) {
|
|
176
|
+
result = Util.replaceArgumentsWithNumbers(args, response.choices[0].message.content);
|
|
177
|
+
result = result.replace(/^\n+|\n+$/g, "");
|
|
178
|
+
// DEBUG: Log the translation result
|
|
179
|
+
console.log(`🔍 DEBUG - Translation result: "${result}"`);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
console.error(`can not translate text with
|
|
172
183
|
system prompt : ${systemPrompt}
|
|
173
184
|
and
|
|
174
185
|
user prompt : ${userPrompt} `);
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
});
|
|
186
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
187
|
+
}
|
|
188
|
+
return result;
|
|
179
189
|
}
|
|
180
190
|
}
|
|
181
|
-
exports.OpenAITranslate = OpenAITranslate;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export interface ITranslate {
|
|
2
2
|
isValidLocale(targetLocale: string): boolean;
|
|
3
|
-
translateText(text: string, sourceLocale: string, targetLocale: string): Promise<string>;
|
|
3
|
+
translateText(text: string, sourceLocale: string, targetLocale: string, context?: string): Promise<string>;
|
|
4
4
|
}
|
|
5
5
|
export interface TranslationFile {
|
|
6
6
|
[key: string]: any;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/build/src/util.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class Util {
|
|
1
|
+
export class Util {
|
|
2
|
+
static _startDelimiter = "{";
|
|
3
|
+
static _endDelimiter = "}";
|
|
4
|
+
static pattern = new RegExp(`${Util._startDelimiter}(.*?)${Util._endDelimiter}`, "g");
|
|
6
5
|
static set startDelimiter(value) {
|
|
7
6
|
Util._startDelimiter = value;
|
|
8
7
|
// update regex
|
|
@@ -36,7 +35,3 @@ class Util {
|
|
|
36
35
|
return { args, text };
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
|
-
exports.Util = Util;
|
|
40
|
-
Util._startDelimiter = "{";
|
|
41
|
-
Util._endDelimiter = "}";
|
|
42
|
-
Util.pattern = new RegExp(`${Util._startDelimiter}(.*?)${Util._endDelimiter}`, "g");
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ValidationError, ValidationResult, ValidationWarning } from "../format.interface.js";
|
|
2
|
+
import { type ErrorMessageContext } from "./error-messages.js";
|
|
3
|
+
/**
|
|
4
|
+
* Enhanced validation error with detailed messaging and context
|
|
5
|
+
*/
|
|
6
|
+
export interface EnhancedValidationError extends ValidationError {
|
|
7
|
+
severity: "error";
|
|
8
|
+
category?: string;
|
|
9
|
+
suggestion?: string;
|
|
10
|
+
documentation?: string;
|
|
11
|
+
actionable?: boolean;
|
|
12
|
+
context?: ErrorMessageContext;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Enhanced validation warning with detailed messaging and context
|
|
16
|
+
*/
|
|
17
|
+
export interface EnhancedValidationWarning extends ValidationWarning {
|
|
18
|
+
severity: "warning" | "info";
|
|
19
|
+
category?: string;
|
|
20
|
+
suggestion?: string;
|
|
21
|
+
documentation?: string;
|
|
22
|
+
actionable?: boolean;
|
|
23
|
+
context?: ErrorMessageContext;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Enhanced validation result with detailed error information and formatting capabilities
|
|
27
|
+
*/
|
|
28
|
+
export declare class EnhancedValidationResult implements ValidationResult {
|
|
29
|
+
readonly isValid: boolean;
|
|
30
|
+
readonly errors: EnhancedValidationError[];
|
|
31
|
+
readonly warnings: EnhancedValidationWarning[];
|
|
32
|
+
constructor(errors?: Array<ValidationError | EnhancedValidationError>, warnings?: Array<ValidationWarning | EnhancedValidationWarning>);
|
|
33
|
+
/**
|
|
34
|
+
* Create from a basic ValidationResult
|
|
35
|
+
*/
|
|
36
|
+
static fromValidationResult(result: ValidationResult): EnhancedValidationResult;
|
|
37
|
+
/**
|
|
38
|
+
* Get total number of issues (errors + warnings)
|
|
39
|
+
*/
|
|
40
|
+
getTotalIssueCount(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Get issues by severity level
|
|
43
|
+
*/
|
|
44
|
+
getIssuesBySeverity(severity: "error" | "warning" | "info"): Array<EnhancedValidationError | EnhancedValidationWarning>;
|
|
45
|
+
/**
|
|
46
|
+
* Get issues by category
|
|
47
|
+
*/
|
|
48
|
+
getIssuesByCategory(category: string): Array<EnhancedValidationError | EnhancedValidationWarning>;
|
|
49
|
+
/**
|
|
50
|
+
* Get only actionable issues (those that can be fixed by the user)
|
|
51
|
+
*/
|
|
52
|
+
getActionableIssues(): Array<EnhancedValidationError | EnhancedValidationWarning>;
|
|
53
|
+
/**
|
|
54
|
+
* Format all issues for display
|
|
55
|
+
*/
|
|
56
|
+
formatForDisplay(includeDetails?: boolean): string;
|
|
57
|
+
/**
|
|
58
|
+
* Format issues for JSON output
|
|
59
|
+
*/
|
|
60
|
+
toJSON(): {
|
|
61
|
+
isValid: boolean;
|
|
62
|
+
summary: {
|
|
63
|
+
errorCount: number;
|
|
64
|
+
warningCount: number;
|
|
65
|
+
totalIssues: number;
|
|
66
|
+
};
|
|
67
|
+
errors: EnhancedValidationError[];
|
|
68
|
+
warnings: EnhancedValidationWarning[];
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Get a brief summary string
|
|
72
|
+
*/
|
|
73
|
+
getSummary(): string;
|
|
74
|
+
/**
|
|
75
|
+
* Check if there are any actionable issues
|
|
76
|
+
*/
|
|
77
|
+
hasActionableIssues(): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Get suggestions for fixing issues
|
|
80
|
+
*/
|
|
81
|
+
getSuggestions(): string[];
|
|
82
|
+
/**
|
|
83
|
+
* Merge with another validation result
|
|
84
|
+
*/
|
|
85
|
+
merge(other: ValidationResult | EnhancedValidationResult): EnhancedValidationResult;
|
|
86
|
+
/**
|
|
87
|
+
* Filter issues by a predicate function
|
|
88
|
+
*/
|
|
89
|
+
filter(predicate: (issue: EnhancedValidationError | EnhancedValidationWarning) => boolean): EnhancedValidationResult;
|
|
90
|
+
/**
|
|
91
|
+
* Convert to basic ValidationResult for compatibility
|
|
92
|
+
*/
|
|
93
|
+
toBasicValidationResult(): ValidationResult;
|
|
94
|
+
private enhanceError;
|
|
95
|
+
private enhanceWarning;
|
|
96
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { ErrorMessageFormatter, } from "./error-messages.js";
|
|
2
|
+
/**
|
|
3
|
+
* Enhanced validation result with detailed error information and formatting capabilities
|
|
4
|
+
*/
|
|
5
|
+
export class EnhancedValidationResult {
|
|
6
|
+
isValid;
|
|
7
|
+
errors;
|
|
8
|
+
warnings;
|
|
9
|
+
constructor(errors = [], warnings = []) {
|
|
10
|
+
this.errors = errors.map((error) => this.enhanceError(error));
|
|
11
|
+
this.warnings = warnings.map((warning) => this.enhanceWarning(warning));
|
|
12
|
+
this.isValid = this.errors.length === 0;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create from a basic ValidationResult
|
|
16
|
+
*/
|
|
17
|
+
static fromValidationResult(result) {
|
|
18
|
+
return new EnhancedValidationResult(result.errors, result.warnings);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get total number of issues (errors + warnings)
|
|
22
|
+
*/
|
|
23
|
+
getTotalIssueCount() {
|
|
24
|
+
return this.errors.length + this.warnings.length;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get issues by severity level
|
|
28
|
+
*/
|
|
29
|
+
getIssuesBySeverity(severity) {
|
|
30
|
+
if (severity === "error") {
|
|
31
|
+
return this.errors;
|
|
32
|
+
}
|
|
33
|
+
return this.warnings.filter((warning) => warning.severity === severity);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get issues by category
|
|
37
|
+
*/
|
|
38
|
+
getIssuesByCategory(category) {
|
|
39
|
+
const allIssues = [...this.errors, ...this.warnings];
|
|
40
|
+
return allIssues.filter((issue) => issue.category === category);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get only actionable issues (those that can be fixed by the user)
|
|
44
|
+
*/
|
|
45
|
+
getActionableIssues() {
|
|
46
|
+
const allIssues = [...this.errors, ...this.warnings];
|
|
47
|
+
return allIssues.filter((issue) => issue.actionable);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Format all issues for display
|
|
51
|
+
*/
|
|
52
|
+
formatForDisplay(includeDetails = false) {
|
|
53
|
+
const lines = [];
|
|
54
|
+
// Add summary
|
|
55
|
+
const summary = ErrorMessageFormatter.createValidationSummary(this.errors, this.warnings);
|
|
56
|
+
lines.push(summary);
|
|
57
|
+
if (this.getTotalIssueCount() === 0) {
|
|
58
|
+
return lines.join("\n");
|
|
59
|
+
}
|
|
60
|
+
lines.push(""); // Empty line
|
|
61
|
+
// Add errors
|
|
62
|
+
if (this.errors.length > 0) {
|
|
63
|
+
lines.push("Errors:");
|
|
64
|
+
for (const error of this.errors) {
|
|
65
|
+
const message = ErrorMessageFormatter.createDisplayMessage(error.code, error.context || {}, includeDetails);
|
|
66
|
+
lines.push(` ${message}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Add warnings
|
|
70
|
+
if (this.warnings.length > 0) {
|
|
71
|
+
if (this.errors.length > 0) {
|
|
72
|
+
lines.push(""); // Empty line between errors and warnings
|
|
73
|
+
}
|
|
74
|
+
lines.push("Warnings:");
|
|
75
|
+
for (const warning of this.warnings) {
|
|
76
|
+
const message = ErrorMessageFormatter.createDisplayMessage(warning.code, warning.context || {}, includeDetails);
|
|
77
|
+
lines.push(` ${message}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Format issues for JSON output
|
|
84
|
+
*/
|
|
85
|
+
toJSON() {
|
|
86
|
+
return {
|
|
87
|
+
isValid: this.isValid,
|
|
88
|
+
summary: {
|
|
89
|
+
errorCount: this.errors.length,
|
|
90
|
+
warningCount: this.warnings.length,
|
|
91
|
+
totalIssues: this.getTotalIssueCount(),
|
|
92
|
+
},
|
|
93
|
+
errors: this.errors,
|
|
94
|
+
warnings: this.warnings,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get a brief summary string
|
|
99
|
+
*/
|
|
100
|
+
getSummary() {
|
|
101
|
+
return ErrorMessageFormatter.createValidationSummary(this.errors, this.warnings);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Check if there are any actionable issues
|
|
105
|
+
*/
|
|
106
|
+
hasActionableIssues() {
|
|
107
|
+
return this.getActionableIssues().length > 0;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get suggestions for fixing issues
|
|
111
|
+
*/
|
|
112
|
+
getSuggestions() {
|
|
113
|
+
const suggestions = [];
|
|
114
|
+
const allIssues = [...this.errors, ...this.warnings];
|
|
115
|
+
for (const issue of allIssues) {
|
|
116
|
+
if (issue.suggestion && issue.actionable) {
|
|
117
|
+
suggestions.push(`${issue.code}: ${issue.suggestion}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return suggestions;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Merge with another validation result
|
|
124
|
+
*/
|
|
125
|
+
merge(other) {
|
|
126
|
+
const otherEnhanced = other instanceof EnhancedValidationResult
|
|
127
|
+
? other
|
|
128
|
+
: EnhancedValidationResult.fromValidationResult(other);
|
|
129
|
+
return new EnhancedValidationResult([...this.errors, ...otherEnhanced.errors], [...this.warnings, ...otherEnhanced.warnings]);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Filter issues by a predicate function
|
|
133
|
+
*/
|
|
134
|
+
filter(predicate) {
|
|
135
|
+
const allIssues = [...this.errors, ...this.warnings];
|
|
136
|
+
const filteredIssues = allIssues.filter(predicate);
|
|
137
|
+
const filteredErrors = filteredIssues.filter((issue) => issue.severity === "error");
|
|
138
|
+
const filteredWarnings = filteredIssues.filter((issue) => issue.severity !== "error");
|
|
139
|
+
return new EnhancedValidationResult(filteredErrors, filteredWarnings);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Convert to basic ValidationResult for compatibility
|
|
143
|
+
*/
|
|
144
|
+
toBasicValidationResult() {
|
|
145
|
+
return {
|
|
146
|
+
isValid: this.isValid,
|
|
147
|
+
errors: this.errors.map((error) => ({
|
|
148
|
+
code: error.code,
|
|
149
|
+
message: error.message,
|
|
150
|
+
line: error.line,
|
|
151
|
+
column: error.column,
|
|
152
|
+
})),
|
|
153
|
+
warnings: this.warnings.map((warning) => ({
|
|
154
|
+
code: warning.code,
|
|
155
|
+
message: warning.message,
|
|
156
|
+
line: warning.line,
|
|
157
|
+
column: warning.column,
|
|
158
|
+
})),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
enhanceError(error) {
|
|
162
|
+
if ("severity" in error && error.severity === "error") {
|
|
163
|
+
return error;
|
|
164
|
+
}
|
|
165
|
+
const template = ErrorMessageFormatter.getErrorDetails(error.code);
|
|
166
|
+
return {
|
|
167
|
+
...error,
|
|
168
|
+
severity: "error",
|
|
169
|
+
category: template?.category,
|
|
170
|
+
suggestion: template?.suggestion,
|
|
171
|
+
documentation: template?.documentation,
|
|
172
|
+
actionable: template?.actionable,
|
|
173
|
+
context: {
|
|
174
|
+
line: error.line,
|
|
175
|
+
column: error.column,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
enhanceWarning(warning) {
|
|
180
|
+
if ("severity" in warning &&
|
|
181
|
+
(warning.severity === "warning" || warning.severity === "info")) {
|
|
182
|
+
return warning;
|
|
183
|
+
}
|
|
184
|
+
const template = ErrorMessageFormatter.getErrorDetails(warning.code);
|
|
185
|
+
return {
|
|
186
|
+
...warning,
|
|
187
|
+
severity: template?.severity === "info" ? "info" : "warning",
|
|
188
|
+
category: template?.category,
|
|
189
|
+
suggestion: template?.suggestion,
|
|
190
|
+
documentation: template?.documentation,
|
|
191
|
+
actionable: template?.actionable,
|
|
192
|
+
context: {
|
|
193
|
+
line: warning.line,
|
|
194
|
+
column: warning.column,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error message templates and localization support for validation errors
|
|
3
|
+
*/
|
|
4
|
+
export interface ErrorMessageTemplate {
|
|
5
|
+
code: string;
|
|
6
|
+
template: string;
|
|
7
|
+
description: string;
|
|
8
|
+
severity: "error" | "warning" | "info";
|
|
9
|
+
category: "structure" | "format" | "content" | "syntax" | "metadata";
|
|
10
|
+
actionable: boolean;
|
|
11
|
+
suggestion?: string;
|
|
12
|
+
documentation?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ErrorMessageContext {
|
|
15
|
+
format?: string;
|
|
16
|
+
filePath?: string;
|
|
17
|
+
line?: number;
|
|
18
|
+
column?: number;
|
|
19
|
+
value?: any;
|
|
20
|
+
expected?: any;
|
|
21
|
+
actual?: any;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Error message templates organized by category
|
|
26
|
+
*/
|
|
27
|
+
export declare const errorMessageTemplates: Record<string, ErrorMessageTemplate>;
|
|
28
|
+
/**
|
|
29
|
+
* Error message formatter with template interpolation and localization support
|
|
30
|
+
*/
|
|
31
|
+
export declare class ErrorMessageFormatter {
|
|
32
|
+
private static locale;
|
|
33
|
+
private static customTemplates;
|
|
34
|
+
/**
|
|
35
|
+
* Set the locale for error messages (for future localization)
|
|
36
|
+
*/
|
|
37
|
+
static setLocale(locale: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Register custom error message templates
|
|
40
|
+
*/
|
|
41
|
+
static registerCustomTemplates(templates: Record<string, ErrorMessageTemplate>): void;
|
|
42
|
+
/**
|
|
43
|
+
* Format an error message with context interpolation
|
|
44
|
+
*/
|
|
45
|
+
static formatMessage(code: string, context?: ErrorMessageContext): string;
|
|
46
|
+
/**
|
|
47
|
+
* Get detailed error information including suggestions and documentation
|
|
48
|
+
*/
|
|
49
|
+
static getErrorDetails(code: string): ErrorMessageTemplate | null;
|
|
50
|
+
/**
|
|
51
|
+
* Format a complete error message with details and suggestions
|
|
52
|
+
*/
|
|
53
|
+
static formatDetailedMessage(code: string, context?: ErrorMessageContext): string;
|
|
54
|
+
/**
|
|
55
|
+
* Get error severity level
|
|
56
|
+
*/
|
|
57
|
+
static getErrorSeverity(code: string): "error" | "warning" | "info";
|
|
58
|
+
/**
|
|
59
|
+
* Check if an error is actionable (can be fixed by the user)
|
|
60
|
+
*/
|
|
61
|
+
static isActionable(code: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Get errors by category
|
|
64
|
+
*/
|
|
65
|
+
static getErrorsByCategory(category: string): ErrorMessageTemplate[];
|
|
66
|
+
/**
|
|
67
|
+
* Interpolate template variables with context values
|
|
68
|
+
*/
|
|
69
|
+
private static interpolateTemplate;
|
|
70
|
+
/**
|
|
71
|
+
* Create a formatted error message for display in CLI or UI
|
|
72
|
+
*/
|
|
73
|
+
static createDisplayMessage(code: string, context?: ErrorMessageContext, includeDetails?: boolean): string;
|
|
74
|
+
/**
|
|
75
|
+
* Get icon for severity level
|
|
76
|
+
*/
|
|
77
|
+
private static getSeverityIcon;
|
|
78
|
+
/**
|
|
79
|
+
* Create a summary of validation results
|
|
80
|
+
*/
|
|
81
|
+
static createValidationSummary(errors: Array<{
|
|
82
|
+
code: string;
|
|
83
|
+
message: string;
|
|
84
|
+
}>, warnings: Array<{
|
|
85
|
+
code: string;
|
|
86
|
+
message: string;
|
|
87
|
+
}>): string;
|
|
88
|
+
}
|