@formatjs/cli-lib 8.5.0 → 8.5.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.
Files changed (58) hide show
  1. package/gts_extractor-Dc-C_f4R.js +14 -0
  2. package/gts_extractor-Dc-C_f4R.js.map +1 -0
  3. package/{src/hbs_extractor.js → hbs_extractor-Eiqkz4d7.js} +9 -8
  4. package/hbs_extractor-Eiqkz4d7.js.map +1 -0
  5. package/index.d.ts +144 -7
  6. package/index.js +633 -2
  7. package/index.js.map +1 -0
  8. package/package.json +5 -5
  9. package/parse_script-EspfGgzZ.js +93 -0
  10. package/parse_script-EspfGgzZ.js.map +1 -0
  11. package/{src/svelte_extractor.js → svelte_extractor-Fr1Z3JHX.js} +16 -37
  12. package/svelte_extractor-Fr1Z3JHX.js.map +1 -0
  13. package/vue_extractor-Btw05cfG.js +41 -0
  14. package/vue_extractor-Btw05cfG.js.map +1 -0
  15. package/main.d.ts +0 -1
  16. package/main.js +0 -3
  17. package/src/cli.d.ts +0 -2
  18. package/src/cli.js +0 -145
  19. package/src/compile.d.ts +0 -63
  20. package/src/compile.js +0 -95
  21. package/src/compile_folder.d.ts +0 -2
  22. package/src/compile_folder.js +0 -8
  23. package/src/console_utils.d.ts +0 -7
  24. package/src/console_utils.js +0 -62
  25. package/src/extract.d.ts +0 -87
  26. package/src/extract.js +0 -212
  27. package/src/formatters/crowdin.d.ts +0 -7
  28. package/src/formatters/crowdin.js +0 -21
  29. package/src/formatters/default.d.ts +0 -6
  30. package/src/formatters/default.js +0 -9
  31. package/src/formatters/index.d.ts +0 -9
  32. package/src/formatters/index.js +0 -31
  33. package/src/formatters/lokalise.d.ts +0 -9
  34. package/src/formatters/lokalise.js +0 -18
  35. package/src/formatters/simple.d.ts +0 -4
  36. package/src/formatters/simple.js +0 -8
  37. package/src/formatters/smartling.d.ts +0 -21
  38. package/src/formatters/smartling.js +0 -39
  39. package/src/formatters/transifex.d.ts +0 -9
  40. package/src/formatters/transifex.js +0 -18
  41. package/src/gts_extractor.d.ts +0 -1
  42. package/src/gts_extractor.js +0 -14
  43. package/src/hbs_extractor.d.ts +0 -1
  44. package/src/parse_script.d.ts +0 -7
  45. package/src/parse_script.js +0 -43
  46. package/src/pseudo_locale.d.ts +0 -22
  47. package/src/pseudo_locale.js +0 -231
  48. package/src/svelte_extractor.d.ts +0 -2
  49. package/src/verify/checkExtraKeys.d.ts +0 -1
  50. package/src/verify/checkExtraKeys.js +0 -32
  51. package/src/verify/checkMissingKeys.d.ts +0 -1
  52. package/src/verify/checkMissingKeys.js +0 -33
  53. package/src/verify/checkStructuralEquality.d.ts +0 -1
  54. package/src/verify/checkStructuralEquality.js +0 -71
  55. package/src/verify/index.d.ts +0 -13
  56. package/src/verify/index.js +0 -26
  57. package/src/vue_extractor.d.ts +0 -2
  58. package/src/vue_extractor.js +0 -58
@@ -1,9 +0,0 @@
1
- import { type CompileFn, type FormatFn } from "./default.js";
2
- export type StructuredJson = Record<string, {
3
- string: string;
4
- developer_comment?: string;
5
- context?: string;
6
- character_limit?: string;
7
- }>;
8
- export declare const format: FormatFn<StructuredJson>;
9
- export declare const compile: CompileFn<StructuredJson>;
@@ -1,18 +0,0 @@
1
- import "./default.js";
2
- export const format = (msgs) => {
3
- const results = {};
4
- for (const [id, msg] of Object.entries(msgs)) {
5
- results[id] = {
6
- string: msg.defaultMessage,
7
- developer_comment: typeof msg.description === "string" ? msg.description : JSON.stringify(msg.description)
8
- };
9
- }
10
- return results;
11
- };
12
- export const compile = (msgs) => {
13
- const results = {};
14
- for (const [id, msg] of Object.entries(msgs)) {
15
- results[id] = msg.string;
16
- }
17
- return results;
18
- };
@@ -1 +0,0 @@
1
- export declare function parseFile(source: string, fileName: string, options: any): void;
@@ -1,14 +0,0 @@
1
- import { Preprocessor } from "content-tag";
2
- import { parseFile as parseHbsFile } from "./hbs_extractor.js";
3
- import { parseScript } from "./parse_script.js";
4
- let p = new Preprocessor();
5
- export function parseFile(source, fileName, options) {
6
- const scriptParseFn = parseScript(options, fileName);
7
- const transformedSource = p.process(source, { filename: fileName });
8
- scriptParseFn(transformedSource.code);
9
- // extract template from transformed source to then run through hbs processor
10
- const parseResult = p.parse(source, { filename: fileName });
11
- for (let parsed of parseResult) {
12
- parseHbsFile(parsed.contents, fileName, options);
13
- }
14
- }
@@ -1 +0,0 @@
1
- export declare function parseFile(source: string, fileName: string, options: any): void;
@@ -1,7 +0,0 @@
1
- import { type Opts } from "@formatjs/ts-transformer";
2
- /**
3
- * Invoid TypeScript module transpilation with our TS transformer
4
- * @param opts Formatjs TS Transformer opt
5
- * @param fn filename
6
- */
7
- export declare function parseScript(opts: Opts, fn?: string): (source: string) => void;
@@ -1,43 +0,0 @@
1
- import { transformWithTs } from "@formatjs/ts-transformer";
2
- import * as ts from "typescript";
3
- import { debug } from "./console_utils.js";
4
- /**
5
- * Invoid TypeScript module transpilation with our TS transformer
6
- * @param opts Formatjs TS Transformer opt
7
- * @param fn filename
8
- */
9
- export function parseScript(opts, fn) {
10
- return (source) => {
11
- let output;
12
- try {
13
- debug("Using TS compiler to process file", fn);
14
- output = ts.transpileModule(source, {
15
- compilerOptions: {
16
- allowJs: true,
17
- target: ts.ScriptTarget.ESNext,
18
- noEmit: true,
19
- experimentalDecorators: true
20
- },
21
- reportDiagnostics: true,
22
- fileName: fn,
23
- transformers: { before: [transformWithTs(ts, opts)] }
24
- });
25
- } catch (e) {
26
- if (e instanceof Error) {
27
- e.message = `Error processing file ${fn}
28
- ${e.message || ""}`;
29
- }
30
- throw e;
31
- }
32
- if (output.diagnostics) {
33
- const errs = output.diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Error);
34
- if (errs.length) {
35
- throw new Error(ts.formatDiagnosticsWithColorAndContext(errs, {
36
- getCanonicalFileName: (fileName) => fileName,
37
- getCurrentDirectory: () => process.cwd(),
38
- getNewLine: () => ts.sys.newLine
39
- }));
40
- }
41
- }
42
- };
43
- }
@@ -1,22 +0,0 @@
1
- import { type MessageFormatElement } from "@formatjs/icu-messageformat-parser";
2
- export declare function generateXXLS(msg: string | MessageFormatElement[]): MessageFormatElement[];
3
- export declare function generateXXAC(msg: string | MessageFormatElement[]): MessageFormatElement[];
4
- export declare function generateXXHA(msg: string | MessageFormatElement[]): MessageFormatElement[];
5
- /**
6
- * accented - Ȧȧƈƈḗḗƞŧḗḗḓ Ḗḗƞɠŀīīşħ
7
- * --------------------------------
8
- *
9
- * This locale replaces all Latin characters with their accented equivalents, and duplicates some
10
- * vowels to create roughly 30% longer strings. Strings are wrapped in markers (square brackets),
11
- * which help with detecting truncation.
12
- */
13
- export declare function generateENXA(msg: string | MessageFormatElement[]): MessageFormatElement[];
14
- /**
15
- * bidi - ɥsıʅƃuƎ ıpıԐ
16
- * -------------------
17
- *
18
- * This strategy replaces all Latin characters with their 180 degree rotated versions and enforces
19
- * right to left text flow using Unicode UAX#9 Explicit Directional Embeddings. In this mode, the UI
20
- * directionality will also be set to right-to-left.
21
- */
22
- export declare function generateENXB(msg: string | MessageFormatElement[]): MessageFormatElement[];
@@ -1,231 +0,0 @@
1
- import { parse, TYPE, isLiteralElement, isPluralElement, isSelectElement, isTagElement } from "@formatjs/icu-messageformat-parser";
2
- function forEachLiteralElement(ast, fn) {
3
- ast.forEach((el) => {
4
- if (isLiteralElement(el)) {
5
- fn(el);
6
- } else if (isPluralElement(el) || isSelectElement(el)) {
7
- for (const opt of Object.values(el.options)) {
8
- forEachLiteralElement(opt.value, fn);
9
- }
10
- } else if (isTagElement(el)) {
11
- forEachLiteralElement(el.children, fn);
12
- }
13
- });
14
- }
15
- export function generateXXLS(msg) {
16
- const ast = typeof msg === "string" ? parse(msg) : msg;
17
- const lastChunk = ast[ast.length - 1];
18
- if (lastChunk && isLiteralElement(lastChunk)) {
19
- lastChunk.value += "SSSSSSSSSSSSSSSSSSSSSSSSS";
20
- return ast;
21
- }
22
- return [...ast, {
23
- type: TYPE.literal,
24
- value: "SSSSSSSSSSSSSSSSSSSSSSSSS"
25
- }];
26
- }
27
- export function generateXXAC(msg) {
28
- const ast = typeof msg === "string" ? parse(msg) : msg;
29
- forEachLiteralElement(ast, (el) => {
30
- el.value = el.value.toUpperCase();
31
- });
32
- return ast;
33
- }
34
- export function generateXXHA(msg) {
35
- const ast = typeof msg === "string" ? parse(msg) : msg;
36
- const [firstChunk, ...rest] = ast;
37
- if (firstChunk && isLiteralElement(firstChunk)) {
38
- firstChunk.value = "[javascript]" + firstChunk.value;
39
- return [firstChunk, ...rest];
40
- }
41
- return [{
42
- type: TYPE.literal,
43
- value: "[javascript]"
44
- }, ...ast];
45
- }
46
- const ACCENTED_MAP = {
47
- "caps": [
48
- 550,
49
- 385,
50
- 391,
51
- 7698,
52
- 7702,
53
- 401,
54
- 403,
55
- 294,
56
- 298,
57
- 308,
58
- 310,
59
- 319,
60
- 7742,
61
- 544,
62
- 510,
63
- 420,
64
- 586,
65
- 344,
66
- 350,
67
- 358,
68
- 364,
69
- 7804,
70
- 7814,
71
- 7818,
72
- 7822,
73
- 7824
74
- ],
75
- "small": [
76
- 551,
77
- 384,
78
- 392,
79
- 7699,
80
- 7703,
81
- 402,
82
- 608,
83
- 295,
84
- 299,
85
- 309,
86
- 311,
87
- 320,
88
- 7743,
89
- 414,
90
- 511,
91
- 421,
92
- 587,
93
- 345,
94
- 351,
95
- 359,
96
- 365,
97
- 7805,
98
- 7815,
99
- 7819,
100
- 7823,
101
- 7825
102
- ]
103
- };
104
- const FLIPPED_MAP = {
105
- "caps": [
106
- 8704,
107
- 1296,
108
- 8579,
109
- 5601,
110
- 398,
111
- 8498,
112
- 8513,
113
- 72,
114
- 73,
115
- 383,
116
- 1276,
117
- 8514,
118
- 87,
119
- 78,
120
- 79,
121
- 1280,
122
- 210,
123
- 7450,
124
- 83,
125
- 8869,
126
- 8745,
127
- 581,
128
- 77,
129
- 88,
130
- 8516,
131
- 90
132
- ],
133
- "small": [
134
- 592,
135
- 113,
136
- 596,
137
- 112,
138
- 477,
139
- 607,
140
- 387,
141
- 613,
142
- 305,
143
- 638,
144
- 670,
145
- 645,
146
- 623,
147
- 117,
148
- 111,
149
- 100,
150
- 98,
151
- 633,
152
- 115,
153
- 647,
154
- 110,
155
- 652,
156
- 653,
157
- 120,
158
- 654,
159
- 122
160
- ]
161
- };
162
- /**
163
- * Based on: https://hg.mozilla.org/mozilla-central/file/a1f74e8c8fb72390d22054d6b00c28b1a32f6c43/intl/l10n/L10nRegistry.jsm#l425
164
- */
165
- function transformString(map, elongate = false, msg) {
166
- return msg.replace(/[a-z]/gi, (ch) => {
167
- const cc = ch.charCodeAt(0);
168
- if (cc >= 97 && cc <= 122) {
169
- const newChar = String.fromCodePoint(map.small[cc - 97]);
170
- // duplicate "a", "e", "o" and "u" to emulate ~30% longer text
171
- if (elongate && (cc === 97 || cc === 101 || cc === 111 || cc === 117)) {
172
- return newChar + newChar;
173
- }
174
- return newChar;
175
- }
176
- if (cc >= 65 && cc <= 90) {
177
- return String.fromCodePoint(map.caps[cc - 65]);
178
- }
179
- return ch;
180
- });
181
- }
182
- /**
183
- * accented - Ȧȧƈƈḗḗƞŧḗḗḓ Ḗḗƞɠŀīīşħ
184
- * --------------------------------
185
- *
186
- * This locale replaces all Latin characters with their accented equivalents, and duplicates some
187
- * vowels to create roughly 30% longer strings. Strings are wrapped in markers (square brackets),
188
- * which help with detecting truncation.
189
- */
190
- export function generateENXA(msg) {
191
- const ast = typeof msg === "string" ? parse(msg) : msg;
192
- forEachLiteralElement(ast, (el) => {
193
- el.value = transformString(ACCENTED_MAP, true, el.value);
194
- });
195
- return [
196
- {
197
- type: TYPE.literal,
198
- value: "["
199
- },
200
- ...ast,
201
- {
202
- type: TYPE.literal,
203
- value: "]"
204
- }
205
- ];
206
- }
207
- /**
208
- * bidi - ɥsıʅƃuƎ ıpıԐ
209
- * -------------------
210
- *
211
- * This strategy replaces all Latin characters with their 180 degree rotated versions and enforces
212
- * right to left text flow using Unicode UAX#9 Explicit Directional Embeddings. In this mode, the UI
213
- * directionality will also be set to right-to-left.
214
- */
215
- export function generateENXB(msg) {
216
- const ast = typeof msg === "string" ? parse(msg) : msg;
217
- forEachLiteralElement(ast, (el) => {
218
- el.value = transformString(FLIPPED_MAP, false, el.value);
219
- });
220
- return [
221
- {
222
- type: TYPE.literal,
223
- value: "‮"
224
- },
225
- ...ast,
226
- {
227
- type: TYPE.literal,
228
- value: "‬"
229
- }
230
- ];
231
- }
@@ -1,2 +0,0 @@
1
- export type ScriptParseFn = (source: string) => void;
2
- export declare function parseFile(source: string, filename: string, parseScriptFn: ScriptParseFn): void;
@@ -1 +0,0 @@
1
- export declare function checkExtraKeys(translationFilesContents: Record<string, any>, sourceLocale: string): Promise<boolean>;
@@ -1,32 +0,0 @@
1
- import { debug, writeStderr } from "../console_utils.js";
2
- /**
3
- * Flatten nested obj into list of keys, delimited by `.`
4
- * @param obj
5
- * @param parentKey
6
- * @returns
7
- */
8
- function extractKeys(obj, parentKey = "") {
9
- if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
10
- return [];
11
- }
12
- return Object.keys(obj).map((k) => [parentKey ? `${parentKey}.${k}` : k, ...extractKeys(obj[k], k)]).flat();
13
- }
14
- export async function checkExtraKeys(translationFilesContents, sourceLocale) {
15
- debug("Checking translation files:");
16
- const sourceContent = translationFilesContents[sourceLocale];
17
- if (!sourceContent) {
18
- throw new Error(`Missing source ${sourceLocale}.json file`);
19
- }
20
- const sourceKeys = extractKeys(sourceContent);
21
- return Object.entries(translationFilesContents).filter(([locale]) => locale !== sourceLocale).reduce((result, [locale, content]) => {
22
- const localeKeys = new Set(extractKeys(content));
23
- const extraKeys = new Set(Array.from(localeKeys).filter((k) => !sourceKeys.includes(k)));
24
- if (!extraKeys.size) {
25
- return result;
26
- }
27
- writeStderr("---------------------------------\n");
28
- writeStderr(`Extra translation keys for locale ${locale}:\n`);
29
- extraKeys.forEach((r) => writeStderr(`${r}\n`));
30
- return false;
31
- }, true);
32
- }
@@ -1 +0,0 @@
1
- export declare function checkMissingKeys(translationFilesContents: Record<string, any>, sourceLocale: string): Promise<boolean>;
@@ -1,33 +0,0 @@
1
- import { debug, writeStderr } from "../console_utils.js";
2
- /**
3
- * Flatten nested obj into list of keys, delimited by `.`
4
- * @param obj
5
- * @param parentKey
6
- * @returns
7
- */
8
- function extractKeys(obj, parentKey = "") {
9
- if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
10
- return [];
11
- }
12
- return Object.keys(obj).map((k) => [parentKey ? `${parentKey}.${k}` : k, ...extractKeys(obj[k], k)]).flat();
13
- }
14
- export async function checkMissingKeys(translationFilesContents, sourceLocale) {
15
- debug("Checking translation files:");
16
- const sourceContent = translationFilesContents[sourceLocale];
17
- if (!sourceContent) {
18
- throw new Error(`Missing source ${sourceLocale}.json file`);
19
- }
20
- const sourceKeys = extractKeys(sourceContent);
21
- return Object.entries(translationFilesContents).filter(([locale]) => locale !== sourceLocale).reduce((result, [locale, content]) => {
22
- const localeKeys = new Set(extractKeys(content));
23
- const missingKeys = new Set(sourceKeys.filter((r) => !localeKeys.has(r)));
24
- // We're being lenient here since only missing keys are currently considered breaking
25
- if (!missingKeys.size) {
26
- return result;
27
- }
28
- writeStderr("---------------------------------\n");
29
- writeStderr(`Missing translation keys for locale ${locale}:\n`);
30
- missingKeys.forEach((r) => writeStderr(`${r}\n`));
31
- return false;
32
- }, true);
33
- }
@@ -1 +0,0 @@
1
- export declare function checkStructuralEquality(translationFilesContents: Record<string, any>, sourceLocale: string): Promise<boolean>;
@@ -1,71 +0,0 @@
1
- import { isStructurallySame, parse } from "@formatjs/icu-messageformat-parser";
2
- import { debug, writeStderr } from "../console_utils.js";
3
- import { error } from "console";
4
- /**
5
- * Flatten nested obj into list of keys, delimited by `.`
6
- * @param obj
7
- * @param parentKey
8
- * @returns
9
- */
10
- function flatten(obj, parentKey = "") {
11
- if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
12
- return {};
13
- }
14
- return Object.keys(obj).reduce((all, k) => {
15
- const value = obj[k];
16
- const key = parentKey ? `${parentKey}.${k}` : k;
17
- if (typeof value === "object") {
18
- Object.assign(all, flatten(value, key));
19
- } else {
20
- all[key] = value;
21
- }
22
- return all;
23
- }, {});
24
- }
25
- export async function checkStructuralEquality(translationFilesContents, sourceLocale) {
26
- debug("Checking translation files:");
27
- const sourceContent = translationFilesContents[sourceLocale];
28
- if (!sourceContent) {
29
- throw new Error(`Missing source ${sourceLocale}.json file`);
30
- }
31
- const sourceMessages = Object.entries(flatten(sourceContent)).reduce((all, [key, value]) => {
32
- try {
33
- all[key] = parse(value);
34
- } catch (e) {
35
- error("Error parsing message", key, value, e);
36
- }
37
- return all;
38
- }, {});
39
- return Object.entries(translationFilesContents).filter(([locale]) => locale !== sourceLocale).reduce((result, [locale, content]) => {
40
- const localeMessages = flatten(content);
41
- const problematicKeys = Object.keys(sourceMessages).map((k) => {
42
- if (!localeMessages[k]) {
43
- return {
44
- key: k,
45
- success: true
46
- };
47
- }
48
- const sourceMessage = sourceMessages[k];
49
- try {
50
- const localeMessage = parse(localeMessages[k]);
51
- return {
52
- key: k,
53
- ...isStructurallySame(sourceMessage, localeMessage)
54
- };
55
- } catch (e) {
56
- return {
57
- key: k,
58
- success: false,
59
- error: e instanceof Error ? e : new Error(String(e))
60
- };
61
- }
62
- }).filter((s) => !s.success);
63
- if (!problematicKeys.length) {
64
- return result;
65
- }
66
- writeStderr("---------------------------------\n");
67
- writeStderr(`These translation keys for locale ${locale} are structurally different from ${sourceLocale}:\n`);
68
- problematicKeys.forEach(({ key, error }) => writeStderr(`${key}: ${error?.message}\n`));
69
- return false;
70
- }, true);
71
- }
@@ -1,13 +0,0 @@
1
- export interface VerifyOpts {
2
- sourceLocale: string;
3
- missingKeys: boolean;
4
- extraKeys: boolean;
5
- structuralEquality: boolean;
6
- ignore?: string[];
7
- /**
8
- * Whether to follow symbolic links when traversing directories.
9
- * Defaults to true for compatibility with pnpm symlinked node_modules.
10
- */
11
- followLinks?: boolean;
12
- }
13
- export declare function verify(files: string[], { sourceLocale, missingKeys, extraKeys, structuralEquality }: VerifyOpts): Promise<void>;
@@ -1,26 +0,0 @@
1
- import { basename } from "path";
2
- import { debug } from "../console_utils.js";
3
- import { checkMissingKeys } from "./checkMissingKeys.js";
4
- import { checkExtraKeys } from "./checkExtraKeys.js";
5
- import { readJSON } from "fs-extra/esm";
6
- import { checkStructuralEquality } from "./checkStructuralEquality.js";
7
- export async function verify(files, { sourceLocale, missingKeys, extraKeys, structuralEquality }) {
8
- debug("Checking translation files:");
9
- files.forEach((fn) => debug(fn));
10
- const translationFilesContents = (await Promise.all(files.map(async (fn) => [basename(fn, ".json"), await readJSON(fn)]))).reduce((all, [locale, content]) => {
11
- all[locale] = content;
12
- return all;
13
- }, {});
14
- debug("Verifying files:", files);
15
- let exitCode = 0;
16
- if (missingKeys && !await checkMissingKeys(translationFilesContents, sourceLocale)) {
17
- exitCode = 1;
18
- }
19
- if (extraKeys && !await checkExtraKeys(translationFilesContents, sourceLocale)) {
20
- exitCode = 1;
21
- }
22
- if (structuralEquality && !await checkStructuralEquality(translationFilesContents, sourceLocale)) {
23
- exitCode = 1;
24
- }
25
- process.exit(exitCode);
26
- }
@@ -1,2 +0,0 @@
1
- export type ScriptParseFn = (source: string) => void;
2
- export declare function parseFile(source: string, filename: string, parseScriptFn: ScriptParseFn): void;
@@ -1,58 +0,0 @@
1
- import { NodeTypes } from "@vue/compiler-core";
2
- import { parse } from "vue/compiler-sfc";
3
- function walk(node, visitor) {
4
- if (typeof node !== "object" || node == null) {
5
- return;
6
- }
7
- if (node.type === NodeTypes.ROOT) {
8
- node.children.forEach((n) => walk(n, visitor));
9
- return;
10
- }
11
- if (node.type !== NodeTypes.ELEMENT && node.type !== NodeTypes.COMPOUND_EXPRESSION && node.type !== NodeTypes.INTERPOLATION) {
12
- return;
13
- }
14
- visitor(node);
15
- if (node.type === NodeTypes.INTERPOLATION) {
16
- visitor(node.content);
17
- } else if (node.type === NodeTypes.ELEMENT) {
18
- node.children.forEach((n) => walk(n, visitor));
19
- node.props.filter((prop) => prop.type === NodeTypes.DIRECTIVE).filter((prop) => !!prop.exp).forEach((prop) => visitor(prop.exp));
20
- } else {
21
- node.children.forEach((n) => walk(n, visitor));
22
- }
23
- }
24
- function templateSimpleExpressionNodeVisitor(parseScriptFn) {
25
- return (n) => {
26
- if (typeof n !== "object") {
27
- return;
28
- }
29
- if (n.type !== NodeTypes.SIMPLE_EXPRESSION) {
30
- return;
31
- }
32
- const { content } = n;
33
- // Wrap this in () since a vue comp node attribute can just be
34
- // an object literal which, by itself is invalid TS
35
- // but with () it becomes an ExpressionStatement
36
- try {
37
- parseScriptFn(`(${content})`);
38
- } catch (e) {
39
- console.warn(`Failed to parse "${content}". Ignore this if content has no extractable message`, e);
40
- }
41
- };
42
- }
43
- export function parseFile(source, filename, parseScriptFn) {
44
- const { descriptor, errors } = parse(source, { filename });
45
- if (errors.length) {
46
- throw errors[0];
47
- }
48
- const { script, scriptSetup, template } = descriptor;
49
- if (template) {
50
- walk(template.ast, templateSimpleExpressionNodeVisitor(parseScriptFn));
51
- }
52
- if (script) {
53
- parseScriptFn(script.content);
54
- }
55
- if (scriptSetup) {
56
- parseScriptFn(scriptSetup.content);
57
- }
58
- }