@wp-blocks/make-pot 1.3.0 → 1.3.1

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/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ # Project exclude paths
2
+ /temp/
3
+ /lib/
4
+ /node_modules/
5
+ /.idea/
6
+ .run
7
+ /src/makeJson.ts
8
+ /tests/fixtures/
9
+ languages
10
+ example
11
+ *.pot
package/.npmignore ADDED
@@ -0,0 +1,12 @@
1
+ node_modules
2
+ tests/fixtures
3
+ tests/__snapshots__
4
+ src
5
+ docs/coverage
6
+ languages
7
+ .github
8
+ .prettierrc
9
+ .prettierignore
10
+ .idea
11
+ example
12
+ *.pot
package/package.json CHANGED
@@ -1,68 +1,77 @@
1
- {
2
- "name": "@wp-blocks/make-pot",
3
- "version": "1.3.0",
4
- "license": "GPL-3.0-or-later",
5
- "homepage": "https://wp-blocks.github.io/make-pot/",
6
- "description": "A Node.js script for generating a POT file from source code",
7
- "authors": [
8
- "Erik Golinelli <erik@codekraft.it> (https://codekraft.it/)",
9
- "John Hooks <bitmachina@outlook.com> (https://johnhooks.io/)"
10
- ],
11
- "main": "lib/run.js",
12
- "bin": {
13
- "make-pot": "lib/index.js"
14
- },
15
- "type": "commonjs",
16
- "engines": {
17
- "node": ">=16.0.0"
18
- },
19
- "files": ["lib", "*.md", "*.json", "LICENSE"],
20
- "keywords": [
21
- "gettext",
22
- "i18n",
23
- "pot",
24
- "WordPress",
25
- "translation",
26
- "internalization"
27
- ],
28
- "scripts": {
29
- "postinstall": "npm rebuild tree-sitter tree-sitter-typescript tree-sitter-php tree-sitter-javascript --force",
30
- "build": "npx esbuild ./src/**/* --format=cjs --minify --outdir=lib --platform=node",
31
- "watch": "tsc --watch",
32
- "lint": "npx @biomejs/biome check --apply src",
33
- "test": "node --test",
34
- "ci:bundle": "npx esbuild ./src/index.ts --format=cjs --outdir=lib --bundle --external:tree-sitter --external:tree-sitter-typescript --external:tree-sitter-php --external:tree-sitter-javascript --platform=node",
35
- "test:ci": "npm run ci:bundle && node --test",
36
- "test:watch": "node --test --watch",
37
- "test:coverage": "node --test --experimental-test-coverage",
38
- "run": "node ./lib/index.js tests/fixtures/vinyl/"
39
- },
40
- "repository": {
41
- "type": "git",
42
- "url": "https://github.com/wp-blocks/makePot.git"
43
- },
44
- "bugs": {
45
- "url": "https://github.com/wp-blocks/makePot/issues"
46
- },
47
- "dependencies": {
48
- "axios": "^1.6.8",
49
- "cli-progress": "^3.12.0",
50
- "gettext-merger": "^1.2.1",
51
- "gettext-parser": "^4.0.4",
52
- "glob": "^10.3.10",
53
- "tree-sitter": "^0.20.6",
54
- "tree-sitter-javascript": "^0.20.4",
55
- "tree-sitter-php": "^0.20.0",
56
- "tree-sitter-typescript": "^0.20.5",
57
- "yargs": "^17.7.1"
58
- },
59
- "devDependencies": {
60
- "@biomejs/biome": "1.7.3",
61
- "@types/cli-progress": "^3.11.5",
62
- "@types/gettext-parser": "^4.0.4",
63
- "@types/node": "^20.11.28",
64
- "@types/yargs": "^17.0.32",
65
- "esbuild": "0.21.1",
66
- "typescript": "^5.4.5"
67
- }
68
- }
1
+ {
2
+ "name": "@wp-blocks/make-pot",
3
+ "version": "1.3.1",
4
+ "license": "GPL-3.0-or-later",
5
+ "homepage": "https://wp-blocks.github.io/make-pot/",
6
+ "description": "A Node.js script for generating a POT file from source code",
7
+ "authors": [
8
+ "Erik Golinelli <erik@codekraft.it> (https://codekraft.it/)",
9
+ "John Hooks <bitmachina@outlook.com> (https://johnhooks.io/)"
10
+ ],
11
+ "type": "commonjs",
12
+ "main": "lib/run.js",
13
+ "bin": {
14
+ "make-pot": "lib/index.js"
15
+ },
16
+ "engines": {
17
+ "node": ">=16.0.0"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/wp-blocks/makePot.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/wp-blocks/makePot/issues"
25
+ },
26
+ "files": [
27
+ "lib",
28
+ "*.md",
29
+ "*.json",
30
+ "tests/*.js",
31
+ "LICENSE",
32
+ "editorconfig",
33
+ ".gitignore",
34
+ ".npmignore"
35
+ ],
36
+ "keywords": [
37
+ "gettext",
38
+ "i18n",
39
+ "pot",
40
+ "WordPress",
41
+ "translation",
42
+ "internalization"
43
+ ],
44
+ "scripts": {
45
+ "postinstall": "npm rebuild tree-sitter tree-sitter-typescript tree-sitter-php tree-sitter-javascript --force",
46
+ "build": "npx esbuild ./src/**/* --format=cjs --minify --outdir=lib --platform=node",
47
+ "watch": "tsc --watch",
48
+ "lint": "npx @biomejs/biome check --apply src",
49
+ "test": "node --test",
50
+ "ci:bundle": "npx esbuild ./src/index.ts --format=cjs --outdir=lib --bundle --external:tree-sitter --external:tree-sitter-typescript --external:tree-sitter-php --external:tree-sitter-javascript --platform=node",
51
+ "test:ci": "npm run ci:bundle && node --test",
52
+ "test:watch": "node --test --watch",
53
+ "test:coverage": "node --test --experimental-test-coverage",
54
+ "run": "node ./lib/index.js tests/fixtures/vinyl/"
55
+ },
56
+ "dependencies": {
57
+ "axios": "^1.6.8",
58
+ "cli-progress": "^3.12.0",
59
+ "gettext-merger": "^1.2.1",
60
+ "gettext-parser": "^4.0.4",
61
+ "glob": "^10.3.10",
62
+ "tree-sitter": "^0.20.6",
63
+ "tree-sitter-javascript": "^0.20.4",
64
+ "tree-sitter-php": "^0.20.0",
65
+ "tree-sitter-typescript": "^0.20.5",
66
+ "yargs": "^17.7.1"
67
+ },
68
+ "devDependencies": {
69
+ "@biomejs/biome": "1.7.3",
70
+ "@types/cli-progress": "^3.11.5",
71
+ "@types/gettext-parser": "^4.0.4",
72
+ "@types/node": "^20.11.28",
73
+ "@types/yargs": "^17.0.32",
74
+ "esbuild": "0.21.1",
75
+ "typescript": "^5.4.5"
76
+ }
77
+ }
@@ -0,0 +1,381 @@
1
+ const { describe, it, beforeEach, before } = require("node:test");
2
+ const assert = require("node:assert");
3
+ const { Block } = require("gettext-merger");
4
+ const { doTree } = require("../lib/");
5
+
6
+ describe("getStrings", () => {
7
+ it("should extract translations from js", () => {
8
+ const content = `var foo = __('Hello World', 'greeting');`;
9
+ const filename = "filename.js";
10
+
11
+ const result = doTree(content, filename);
12
+ const expected = new Block([]);
13
+ expected.msgid = "Hello World";
14
+ expected.comments = {
15
+ reference: ["filename.js:1"],
16
+ translator: [""],
17
+ };
18
+
19
+ assert.strictEqual(result.blocks[0].msgid, expected.msgid);
20
+ });
21
+
22
+ it("should extract translations from ts", () => {
23
+ const content = `__('Hello World', 'greeting');`;
24
+ const filename = "filename.ts";
25
+
26
+ const result = doTree(content, filename).blocks[0].toJson();
27
+
28
+ const expected = new Block([]);
29
+ expected.msgid = "Hello World";
30
+ expected.comments = {
31
+ reference: ["filename.ts:1"],
32
+ translator: [""],
33
+ };
34
+
35
+ assert.strictEqual(result.msgid, expected.msgid);
36
+ assert.strictEqual(expected.comments?.reference.length, 1);
37
+ assert.deepStrictEqual(
38
+ [result.comments.reference],
39
+ expected.comments.reference,
40
+ );
41
+ });
42
+
43
+ it("should extract translations from tsx", () => {
44
+ const content = `const element = <h1>{ __('Hello World', 'greeting')}</h1>;`;
45
+
46
+ const filename = "filename.tsx";
47
+
48
+ const result = doTree(content, filename);
49
+
50
+ const expected = new Block([]);
51
+ expected.msgid = "Hello World";
52
+ expected.msgstr = [""];
53
+ expected.comments = {
54
+ reference: ["filename.tsx:1"],
55
+ translator: [""],
56
+ };
57
+
58
+ assert.deepStrictEqual(result.blocks[0].toJson(), expected.toJson());
59
+ });
60
+
61
+ it("should extract translations with context", () => {
62
+ const content = `<?php __('Hello World', 'greeting'); ?>`;
63
+ const filename = "filename.php";
64
+
65
+ const result = doTree(content, filename);
66
+
67
+ const expected = new Block([]);
68
+ expected.msgid = "Hello World";
69
+ expected.msgstr = [""];
70
+ expected.comments = {
71
+ reference: ["filename.php:1"],
72
+ translator: [""],
73
+ };
74
+
75
+ assert.deepStrictEqual(result.blocks[0].toJson(), expected.toJson());
76
+ });
77
+
78
+ it("should extract translations from code content with no context or translator comments", () => {
79
+ const content = `<?php _e('Hello World'); ?>`;
80
+ const expected = new Block([]);
81
+ expected.msgctxt = undefined;
82
+ expected.msgid = "Hello World";
83
+ expected.msgid_plural = undefined;
84
+ expected.msgstr = [""];
85
+ expected.comments = {
86
+ translator: undefined,
87
+ reference: ["filename.php:1"],
88
+ };
89
+ const filename = "filename.php";
90
+
91
+ const result = doTree(content, filename);
92
+
93
+ assert.deepEqual(result.blocks[0], expected);
94
+ });
95
+
96
+ it("should extract translations with comments", () => {
97
+ const filename = "filename.php";
98
+ const content = `
99
+ <?php /** translators: ciao! */ echo _x('Hello World', 'greeting'); ?>`;
100
+ const expectedComments = {
101
+ reference: ["filename.php:2"],
102
+ translator: ["ciao!"],
103
+ };
104
+
105
+ const result = doTree(content, filename);
106
+
107
+ assert.deepEqual(result.blocks[0].comments, expectedComments);
108
+ });
109
+
110
+ it("should extract translations with comments reporting the right position", () => {
111
+ const filename = "filename.php";
112
+ const content = `
113
+
114
+
115
+
116
+ /** line 5*/
117
+
118
+
119
+
120
+
121
+ <?php echo _x('Hello World', 'greeting'); ?>`;
122
+ const expected = `#: filename.php:10
123
+ msgctxt "greeting"
124
+ msgid "Hello World"
125
+ msgstr ""`;
126
+
127
+ const result = doTree(content, filename);
128
+
129
+ assert.strictEqual(result.blocks[0].toStr(), expected);
130
+ });
131
+
132
+ it("should extract translations inside a sprint", () => {
133
+ const filename = "filename.php";
134
+ const content = ` <?php
135
+ $url = 'http://example.com';
136
+ $link = sprintf( wp_kses( __( 'Check out this link to my <a href="%s">website</a> made with WordPress.', 'my-text-domain' ), array( 'a' => array( 'href' => array() ) ) ), esc_url( $url ) );
137
+ echo $link;`;
138
+ const expected = {
139
+ "": {
140
+ 'Check out this link to my <a href="%s">website</a> made with WordPress.':
141
+ {
142
+ comments: {
143
+ extracted: "",
144
+ flag: "",
145
+ previous: "",
146
+ reference: "filename.php:3",
147
+ translator: "",
148
+ },
149
+ msgctxt: "",
150
+ msgid:
151
+ 'Check out this link to my <a href="%s">website</a> made with WordPress.',
152
+ msgid_plural: undefined,
153
+ msgstr: [""],
154
+ },
155
+ },
156
+ };
157
+
158
+ const result = doTree(content, filename);
159
+
160
+ assert.deepStrictEqual(result.toJson(), expected);
161
+ });
162
+ });
163
+
164
+ describe("getStrings wp cli", () => {
165
+ it("should extract from an array of translations", () => {
166
+ const filename = "filename.php";
167
+ const content = `<?php $var = 'don't do this'; $instructions = array(
168
+ "Overview" => array(
169
+ "title" => __( 'Overview', '3d-product-viewer' ),
170
+ "text" => __( "Hold down the right button to move the model", '3d-product-viewer' ),
171
+ "icon" => 'icon-book'
172
+ ),
173
+ "Rotation" => array(
174
+ "title" => __( 'Rotation', '3d-product-viewer' ),
175
+ "text" => __( "Left-click and drag to change the angle", '3d-product-viewer' ),
176
+ "icon" => 'icon-rotation'
177
+ ),
178
+ "Zoom" => array(
179
+ "title" => __( 'Zoom', '3d-product-viewer' ),
180
+ "text" => __( "Variable is % and not {$var}", '3d-product-viewer' ),
181
+ "icon" => 'icon-zoom'
182
+ )
183
+ );
184
+ `;
185
+
186
+ const result = doTree(content, filename);
187
+ assert.deepStrictEqual(result.blocks[0].msgid, "Overview");
188
+ });
189
+
190
+ it("should extract translations with translator comments inside the formatting hell", () => {
191
+ const filename = "filename.php";
192
+ const content = `<?php if ( count( $errors_in_remigrate_batch ) > 0 ) {
193
+ $formatted_errors = wp_json_encode( $errors_in_remigrate_batch, JSON_PRETTY_PRINT );
194
+ WP_CLI::warning(
195
+ sprintf(
196
+ /* Translators: %1$d is number of errors and %2$s is the formatted array of order IDs. */
197
+ _n(
198
+ '%1$d error found: %2$s when re-migrating order. Please review the error above.',
199
+ '%1$d errors found: %2$s when re-migrating orders. Please review the errors above.',
200
+ count( %s ),
201
+ 'woocommerce'
202
+ ),
203
+ count( $errors_in_remigrate_batch ),
204
+ $formatted_errors
205
+ )
206
+ );
207
+ } else {
208
+ WP_CLI::warning( 'Re-migration successful.', 'woocommerce' );
209
+ }
210
+ `;
211
+
212
+ const result = doTree(content, filename);
213
+ assert.strictEqual(
214
+ "%1$d error found: %2$s when re-migrating order. Please review the error above.",
215
+ result.blocks[0].msgid,
216
+ );
217
+ });
218
+
219
+ /** see https://github.com/wp-cli/i18n-command/blob/main/features/makepot.feature */
220
+ it("should extract translations and comments from code content", () => {
221
+ const content = `<?php
222
+
223
+ And a foo-plugin/foo-plugin.php file:
224
+ """
225
+ <?php
226
+ /**
227
+ */
228
+ __( 'Hello World', 'foo-plugin' );
229
+ """
230
+ And a foo-plugin/vendor/ignored.php file:
231
+ """
232
+ <?php
233
+ __( 'I am being ignored', 'foo-plugin' );
234
+ """
235
+ __( '__', 'foo-plugin' );
236
+ esc_attr__( 'esc_attr__', 'foo-plugin' );
237
+ esc_html__( 'esc_html__', 'foo-plugin' );
238
+ esc_xml__( 'esc_xml__', 'foo-plugin' );
239
+ _e( '_e', 'foo-plugin' );
240
+ esc_attr_e( 'esc_attr_e', 'foo-plugin' );
241
+ esc_html_e( 'esc_html_e', 'foo-plugin' );
242
+ esc_xml_e( 'esc_xml_e', 'foo-plugin' );
243
+ _x( '_x', '_x_context', 'foo-plugin' );
244
+ _ex( '_ex', '_ex_context', 'foo-plugin' );
245
+ esc_attr_x( 'esc_attr_x', 'esc_attr_x_context', 'foo-plugin' );
246
+ esc_html_x( 'esc_html_x', 'esc_html_x_context', 'foo-plugin' );
247
+ esc_xml_x( 'esc_xml_x', 'esc_xml_x_context', 'foo-plugin' );
248
+ _n( '_n_single', '_n_plural', $number, 'foo-plugin' );
249
+ _nx( '_nx_single', '_nx_plural', $number, '_nx_context', 'foo-plugin' );
250
+ _n_noop( '_n_noop_single', '_n_noop_plural', 'foo-plugin' );
251
+ _nx_noop( '_nx_noop_single', '_nx_noop_plural', '_nx_noop_context', 'foo-plugin' );
252
+
253
+ // Compat.
254
+ _( '_', 'foo-plugin' );
255
+
256
+ // Deprecated.
257
+ _c( '_c', 'foo-plugin' );
258
+ _nc( '_nc_single', '_nc_plural', $number, 'foo-plugin' );
259
+ __ngettext( '__ngettext_single', '__ngettext_plural', $number, 'foo-plugin' );
260
+ __ngettext_noop( '__ngettext_noop_single', '__ngettext_noop_plural', 'foo-plugin' );
261
+
262
+ __unsupported_func( '__unsupported_func', 'foo-plugin' );
263
+ __( 'wrong-domain', 'wrong-domain' );
264
+
265
+ // See https://github.com/wp-cli/i18n-command/issues/344
266
+ \\__( '\\__', 'foo-plugin' );
267
+ \\_e( '\\_e', 'foo-plugin' );
268
+ // Included to test if peast correctly parses regexes containing a quote.
269
+ // See: https://github.com/wp-cli/i18n-command/issues/98
270
+ n = n.replace(/"/g, '&quot;');
271
+ n = n.replace(/"|'/g, '&quot;');
272
+
273
+ __( '__', 'foo-plugin' );
274
+ _x( '_x', '_x_context', 'foo-plugin' );
275
+ _n( '_n_single', '_n_plural', number, 'foo-plugin' );
276
+ _nx( '_nx_single', '_nx_plural', number, '_nx_context', 'foo-plugin' );
277
+
278
+ __( 'wrong-domain', 'wrong-domain' );
279
+
280
+ __( 'Hello world' ); // translators: Greeting`;
281
+
282
+ const filename = "filename.php";
283
+
284
+ const result = doTree(content, filename);
285
+
286
+ assert.strictEqual(result.blocks.length, 26);
287
+ });
288
+
289
+ /** see wp cli tests */
290
+ it("should extract translations and comments from code content", () => {
291
+ const content = `<?php
292
+
293
+ // translators: Foo Bar Comment
294
+ __( 'Foo Bar', 'foo-plugin' );
295
+
296
+ // TrANslAtORs: Bar Baz Comment
297
+ __( 'Bar Baz', 'foo-plugin' );
298
+
299
+ // translators: Software name
300
+ const string = __( 'WordPress', 'foo-plugin' );
301
+
302
+ // translators: So much space
303
+
304
+ __( 'Spacey text', 'foo-plugin' );
305
+
306
+ /* translators: Long comment
307
+ spanning multiple
308
+ lines */
309
+ const string = __( 'Short text', 'foo-plugin' );
310
+
311
+ ReactDOM.render(
312
+ <h1>{__( 'Hello JSX', 'foo-plugin' )}</h1>,
313
+ document.getElementById('root')
314
+ );
315
+
316
+ wp.i18n.__( 'wp.i18n.__', 'foo-plugin' );
317
+ wp.i18n._n( 'wp.i18n._n_single', 'wp.i18n._n_plural', number, 'foo-plugin' );
318
+
319
+ const translate = wp.i18n;
320
+ translate.__( 'translate.__', 'foo-plugin' );
321
+
322
+ Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__["__"])( 'webpack.__', 'foo-plugin' );
323
+ Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__[/* __ */ "a"])( 'webpack.mangle.__', 'foo-plugin' );
324
+
325
+ Object(u.__)( 'minified.__', 'foo-plugin' );
326
+ Object(j._x)( 'minified._x', 'minified._x_context', 'foo-plugin' );
327
+
328
+ /* translators: babel */
329
+ (0, __)( 'babel.__', 'foo-plugin' );
330
+ (0, _i18n.__)( 'babel-i18n.__', 'foo-plugin' );
331
+ (0, _i18n._x)( 'babel-i18n._x', 'babel-i18n._x_context', 'foo-plugin' );
332
+
333
+ eval( "__( 'Hello Eval World', 'foo-plugin' );" );
334
+
335
+ __( \`This is a \${bug}\`, 'foo-plugin' );
336
+
337
+ /**
338
+ * Plugin Name: Plugin name
339
+ */
340
+
341
+ /* translators: Translators 1! */
342
+ _e( 'hello world', 'foo-plugin' );
343
+
344
+ /* Translators: Translators 2! */
345
+ $foo = __( 'foo', 'foo-plugin' );
346
+
347
+ /* translators: localized date and time format, see https://secure.php.net/date */
348
+ __( 'F j, Y g:i a', 'foo-plugin' );
349
+
350
+ // translators: let your ears fly!
351
+ __( 'on', 'foo-plugin' );
352
+
353
+ /*
354
+ * Translators: If there are characters in your language that are not supported
355
+ * by Lato, translate this to 'off'. Do not translate into your own language.
356
+ */
357
+ __( 'off', 'foo-plugin' );
358
+
359
+ /* translators: this should get extracted. */ $foo = __( 'baba', 'foo-plugin' );
360
+
361
+ /* translators: boo */ /* translators: this should get extracted too. */ /* some other comment */ $bar = g( __( 'bubu', 'foo-plugin' ) );
362
+
363
+ {TAB}/*
364
+ {TAB} * translators: this comment block is indented with a tab and should get extracted too.
365
+ {TAB} */
366
+ {TAB}__( 'yolo', 'foo-plugin' );
367
+
368
+ /* translators: This is a comment */
369
+ __( 'Plugin name', 'foo-plugin' );
370
+
371
+ /* Translators: This is another comment! */
372
+ __( 'https://example.com', 'foo-plugin' );
373
+ `;
374
+
375
+ const filename = "filename.php";
376
+
377
+ const result = doTree(content, filename);
378
+
379
+ assert.strictEqual(result.blocks.length, 20);
380
+ });
381
+ });
@@ -0,0 +1,85 @@
1
+ const { sep } = require("node:path");
2
+ const { describe, it } = require("node:test");
3
+ const assert = require("node:assert");
4
+ const { parseCliArgs } = require("./lib/cli/parseCli.js");
5
+ const { getFiles } = require("./lib/fs/glob.js");
6
+
7
+ describe("getFiles", () => {
8
+ const DEFAULTS = parseCliArgs({
9
+ domain: "plugin",
10
+ slug: "plugin-slug",
11
+ paths: { cwd: "tests/fixtures/", out: "tests/fixtures/" },
12
+ options: {
13
+ silent: true,
14
+ },
15
+ $0: "makepot",
16
+ _: [0, 1],
17
+ });
18
+
19
+ it("should retrieve a all files", async () => {
20
+ const args = { ...DEFAULTS, domain: "theme" };
21
+ const pattern = { include: ["./src/**"], exclude: [] };
22
+
23
+ const files = getFiles(args, pattern);
24
+ const collected = [];
25
+ for (const file of files) {
26
+ assert.equal(!!file, true);
27
+ collected.push(file);
28
+ }
29
+ assert.deepStrictEqual(collected.length, 28);
30
+ assert.strictEqual(
31
+ collected.find((e) => e.includes("const.ts")),
32
+ "src\\const.ts",
33
+ );
34
+ });
35
+
36
+ it("Should retrieve a list of txt files based on the provided plugin pattern", async () => {
37
+ const args = {
38
+ ...DEFAULTS,
39
+ };
40
+ const pattern = {
41
+ include: ["**/*.txt"],
42
+ exclude: ["node_modules", "dist"],
43
+ };
44
+ const expectedFiles = [
45
+ `tests${sep}fixtures${sep}file1.txt`,
46
+ `tests${sep}fixtures${sep}sourcedir${sep}file2.txt`,
47
+ `tests${sep}fixtures${sep}block${sep}readme.txt`,
48
+ ];
49
+
50
+ const files = getFiles(args, pattern);
51
+ const collected = [];
52
+ for (const file of files) {
53
+ assert.equal(!!file, true);
54
+ collected.push(file);
55
+ }
56
+
57
+ assert.deepStrictEqual(collected, expectedFiles);
58
+ });
59
+
60
+ it("should retrieve a list of theme.json files based on the provided theme pattern", async () => {
61
+ const args = {
62
+ ...DEFAULTS,
63
+ };
64
+ const pattern = {
65
+ include: ["tests/fixtures/sourcedir/**/*.json"],
66
+ exclude: [],
67
+ };
68
+ const expectedFiles = [
69
+ `tests${sep}fixtures${sep}sourcedir${sep}theme.json`,
70
+ `tests${sep}fixtures${sep}sourcedir${sep}package.json`,
71
+ `tests${sep}fixtures${sep}sourcedir${sep}node_modules${sep}module${sep}block.json`,
72
+ ];
73
+
74
+ const files = getFiles(args, pattern);
75
+ const collected = [];
76
+ for (const file of files) {
77
+ assert.equal(!!file, true);
78
+ collected.push(file);
79
+ }
80
+
81
+ const fileFound = collected.filter((file) => expectedFiles.includes(file));
82
+
83
+ assert.deepStrictEqual(fileFound.length, 3);
84
+ });
85
+ });
@@ -0,0 +1,49 @@
1
+ const fs = require("node:fs");
2
+ const { describe, it } = require("node:test");
3
+ const assert = require("node:assert");
4
+ const { parseJsonFile } = require("../lib/extractors/json.js");
5
+
6
+ describe("should parse json", () => {
7
+ it("theme.json", async () => {
8
+ const expected = {
9
+ "block style label": {
10
+ label: {
11
+ comments: {
12
+ reference: "block/block.json",
13
+ },
14
+ msgctxt: "block style label",
15
+ msgid: "label",
16
+ msgstr: [],
17
+ },
18
+ },
19
+ "block description": {
20
+ description: {
21
+ comments: {
22
+ reference: "block/block.json",
23
+ },
24
+ msgctxt: "block description",
25
+ msgid: "description",
26
+ msgstr: [],
27
+ },
28
+ },
29
+ "block keyword": {
30
+ undefined: {
31
+ comments: {
32
+ reference: "block/block.json",
33
+ },
34
+ msgctxt: "block keyword",
35
+ msgid: undefined,
36
+ msgstr: [],
37
+ },
38
+ },
39
+ };
40
+
41
+ const result = await parseJsonFile({
42
+ fileContent: fs.readFileSync("tests/fixtures/block/block.json", "utf8"),
43
+ filename: "block.json",
44
+ filepath: "block/block.json",
45
+ });
46
+
47
+ assert.strictEqual(result, expected);
48
+ });
49
+ });
@@ -0,0 +1,158 @@
1
+ const path = require("node:path");
2
+ const fs = require("node:fs");
3
+ const { describe, it } = require("node:test");
4
+ const assert = require("node:assert");
5
+ const { doTree } = require("../lib/");
6
+
7
+ describe("doTree js", () => {
8
+ const filepath = "tests/fixtures/block/javascript.js";
9
+ const filePath = path.join(process.cwd(), filepath);
10
+ const fileContent = fs.readFileSync(filePath, "utf8");
11
+ it("Should parse TSX file and extract strings", () => {
12
+ const fileParsed = doTree(fileContent, filepath);
13
+ assert.deepEqual(fileParsed.blocks[0], {
14
+ comments: {
15
+ reference: ["tests\\fixtures\\block\\javascript.js:7"],
16
+ translator: undefined,
17
+ },
18
+ msgctxt: undefined,
19
+ msgid: "Simple Block",
20
+ msgid_plural: undefined,
21
+ msgstr: [""],
22
+ });
23
+ });
24
+ });
25
+
26
+ describe("doTree php", () => {
27
+ const filepath = "tests/fixtures/plugin/plugin.php";
28
+ const filePath = path.join(process.cwd(), filepath);
29
+ const fileContent = fs.readFileSync(filePath, "utf8");
30
+ it("Should parse TSX file and extract strings", () => {
31
+ const fileParsed = doTree(fileContent, filepath);
32
+ assert.deepEqual(fileParsed.blocks[1], {
33
+ comments: {
34
+ reference: ["tests\\fixtures\\plugin\\plugin.php:65"],
35
+ translator: undefined,
36
+ },
37
+ msgctxt: undefined,
38
+ msgid: "You\\'re a silly monkey",
39
+ msgid_plural: undefined,
40
+ msgstr: [""],
41
+ });
42
+ });
43
+ });
44
+
45
+ describe("doTree tsx file", () => {
46
+ const filepath = "tests/fixtures/block/SvgControls.tsx";
47
+ const filePath = path.join(process.cwd(), filepath);
48
+ const fileContent = fs.readFileSync(filePath, "utf8");
49
+ it("Should parse TSX file and extract strings", () => {
50
+ const fileParsed = doTree(fileContent, filepath);
51
+ assert.deepEqual(fileParsed.blocks[2], {
52
+ comments: {
53
+ reference: ["tests\\fixtures\\block\\SvgControls.tsx:107"],
54
+ translator: undefined,
55
+ },
56
+ msgctxt: undefined,
57
+ msgid: "Replace SVG",
58
+ msgid_plural: undefined,
59
+ msgstr: [""],
60
+ });
61
+ });
62
+ });
63
+
64
+ describe("doTree php test file", async () => {
65
+ /** see wp cli tests */
66
+ it("should extract translations and comments from code content", () => {
67
+ const content = `<?php
68
+
69
+ // translators: Foo Bar Comment
70
+ __( 'Foo Bar', 'foo-plugin' );
71
+
72
+ // TrANslAtORs: Bar Baz Comment
73
+ __( 'Bar Baz', 'foo-plugin' );
74
+
75
+ // translators: Software name
76
+ const string = __( 'WordPress', 'foo-plugin' );
77
+
78
+ // translators: So much space
79
+
80
+ __( 'Spacey text', 'foo-plugin' );
81
+
82
+ /* translators: Long comment
83
+ spanning multiple
84
+ lines */
85
+ const string = __( 'Short text', 'foo-plugin' );
86
+
87
+ ReactDOM.render(
88
+ <h1>{__( 'Hello JSX', 'foo-plugin' )}</h1>,
89
+ document.getElementById('root')
90
+ );
91
+
92
+ wp.i18n.__( 'wp.i18n.__', 'foo-plugin' );
93
+
94
+ const translate = wp.i18n;
95
+ translate.__( 'translate.__', 'foo-plugin' );
96
+
97
+ Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__["__"])( 'webpack.__', 'foo-plugin' );
98
+ Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__[/* __ */ "a"])( 'webpack.mangle.__', 'foo-plugin' );
99
+
100
+ Object(u.__)( 'minified.__', 'foo-plugin' );
101
+ Object(j._x)( 'minified._x', 'minified._x_context', 'foo-plugin' );
102
+
103
+ /* translators: babel */
104
+ (0, __)( 'babel.__', 'foo-plugin' );
105
+ (0, _i18n.__)( 'babel-i18n.__', 'foo-plugin' );
106
+ (0, _i18n._x)( 'babel-i18n._x', 'babel-i18n._x_context', 'foo-plugin' );
107
+
108
+ eval( "__( 'Hello Eval World', 'foo-plugin' );" );
109
+
110
+ __( "ASDASDASD', 'foo-plugin' );
111
+
112
+ /**
113
+ * Plugin Name: Plugin name
114
+ */
115
+
116
+ /* translators: Translators 1! */
117
+ _e( 'hello world', 'foo-plugin' );
118
+
119
+ /* Translators: Translators 2! */
120
+ $foo = __( 'foo', 'foo-plugin' );
121
+
122
+ /* translators: localized date and time format, see https://secure.php.net/date */
123
+ __( 'F j, Y g:i a', 'foo-plugin' );
124
+
125
+ // translators: let your ears fly!
126
+ __( 'on', 'foo-plugin' );
127
+
128
+ /*
129
+ * Translators: If there are characters in your language that are not supported
130
+ * by Lato, translate this to 'off'. Do not translate into your own language.
131
+ */
132
+ __( 'off', 'foo-plugin' );
133
+
134
+ /* translators: this should get extracted. */ $foo = __( 'baba', 'foo-plugin' );
135
+
136
+ /* translators: boo */ /* translators: this should get extracted too. */ /* some other comment */ $bar = g( __( 'bubu', 'foo-plugin' ) );
137
+
138
+ {TAB}/*
139
+ {TAB} * translators: this comment block is indented with a tab and should get extracted too.
140
+ {TAB} */
141
+ {TAB}__( 'yolo', 'foo-plugin' );
142
+
143
+ /* translators: This is a comment */
144
+ __( 'Plugin name', 'foo-plugin' );
145
+
146
+ /* Translators: This is another comment! */
147
+ __( 'https://example.com', 'foo-plugin' );
148
+ `;
149
+
150
+ const filename = "filename.php";
151
+
152
+ const r = doTree(content, filename).blocks;
153
+ const res = Object.values(r)[0];
154
+
155
+ assert.strictEqual(r.map((block) => block).length, 11);
156
+ assert.strictEqual(r.filter((block) => block.comments).length, 11);
157
+ });
158
+ });
@@ -0,0 +1,32 @@
1
+ const fs = require("node:fs");
2
+ const path = require("node:path");
3
+ const { describe, it } = require("node:test");
4
+ const assert = require("node:assert");
5
+
6
+ const { makePot } = require("../lib/");
7
+ const { getArgs } = require("../lib/cli/getArgs.js");
8
+
9
+ describe("doTree is like wpcli", () => {
10
+ it("Should emit a pot file for theme like the wp-cli makepot", async () => {
11
+ const filePath = path.join(process.cwd(), "./tests/fixtures/theme/");
12
+ const potPath = path.join(process.cwd(), "./tests/fixtures/theme.pot");
13
+ const potContent = fs.readFileSync(potPath, "utf8");
14
+ const args = getArgs({
15
+ _: [filePath],
16
+ });
17
+ const fileParsed = await makePot(args);
18
+ assert.deepEqual(fileParsed, potContent);
19
+ });
20
+
21
+ it("should emit a pot file for plugin like the wp-cli makepot", async () => {
22
+ const filePath = path.join(process.cwd(), "./tests/fixtures/vinyl/");
23
+ const potPath = path.join(process.cwd(), "./tests/fixtures/vinyl.pot");
24
+ const potContent = fs.readFileSync(potPath, "utf8");
25
+
26
+ const args = getArgs({
27
+ _: [filePath],
28
+ });
29
+ const fileParsed = await makePot(args);
30
+ assert.deepEqual(fileParsed, potContent);
31
+ });
32
+ });