@esgettext/tools 1.0.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.
Files changed (102) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +126 -0
  3. package/bin/esgettext-install.js +5 -0
  4. package/bin/esgettext-mo2json.js +5 -0
  5. package/bin/esgettext-msgfmt-all.js +5 -0
  6. package/bin/esgettext-msgmerge-all.js +5 -0
  7. package/bin/esgettext-xgettext.js +5 -0
  8. package/dist/cli/getopt.d.ts +34 -0
  9. package/dist/cli/getopt.js +150 -0
  10. package/dist/cli/getopt.js.map +1 -0
  11. package/dist/cli/getopt.spec.d.ts +1 -0
  12. package/dist/cli/getopt.spec.js +164 -0
  13. package/dist/cli/getopt.spec.js.map +1 -0
  14. package/dist/cli/install.d.ts +1 -0
  15. package/dist/cli/install.js +91 -0
  16. package/dist/cli/install.js.map +1 -0
  17. package/dist/cli/mo2json-cli.d.ts +1 -0
  18. package/dist/cli/mo2json-cli.js +48 -0
  19. package/dist/cli/mo2json-cli.js.map +1 -0
  20. package/dist/cli/msgfmt-all.d.ts +1 -0
  21. package/dist/cli/msgfmt-all.js +99 -0
  22. package/dist/cli/msgfmt-all.js.map +1 -0
  23. package/dist/cli/msgmerge-all.d.ts +1 -0
  24. package/dist/cli/msgmerge-all.js +85 -0
  25. package/dist/cli/msgmerge-all.js.map +1 -0
  26. package/dist/cli/xgettext-cli.d.ts +1 -0
  27. package/dist/cli/xgettext-cli.js +303 -0
  28. package/dist/cli/xgettext-cli.js.map +1 -0
  29. package/dist/install/install.d.ts +10 -0
  30. package/dist/install/install.js +157 -0
  31. package/dist/install/install.js.map +1 -0
  32. package/dist/msgfmt-all/msgfmt-all.d.ts +8 -0
  33. package/dist/msgfmt-all/msgfmt-all.js +142 -0
  34. package/dist/msgfmt-all/msgfmt-all.js.map +1 -0
  35. package/dist/msgmerge-all/msgmerge-all.d.ts +9 -0
  36. package/dist/msgmerge-all/msgmerge-all.js +157 -0
  37. package/dist/msgmerge-all/msgmerge-all.js.map +1 -0
  38. package/dist/package.d.ts +4 -0
  39. package/dist/package.js +17 -0
  40. package/dist/package.js.map +1 -0
  41. package/dist/parser/__snapshots__/javascript.spec.js.snap +56 -0
  42. package/dist/parser/__snapshots__/po.spec.js.snap +176 -0
  43. package/dist/parser/javascript.d.ts +4 -0
  44. package/dist/parser/javascript.js +47 -0
  45. package/dist/parser/javascript.js.map +1 -0
  46. package/dist/parser/javascript.spec.d.ts +1 -0
  47. package/dist/parser/javascript.spec.js +436 -0
  48. package/dist/parser/javascript.spec.js.map +1 -0
  49. package/dist/parser/parser.d.ts +45 -0
  50. package/dist/parser/parser.js +582 -0
  51. package/dist/parser/parser.js.map +1 -0
  52. package/dist/parser/po.d.ts +22 -0
  53. package/dist/parser/po.js +394 -0
  54. package/dist/parser/po.js.map +1 -0
  55. package/dist/parser/po.spec.d.ts +1 -0
  56. package/dist/parser/po.spec.js +296 -0
  57. package/dist/parser/po.spec.js.map +1 -0
  58. package/dist/parser/typescript.d.ts +4 -0
  59. package/dist/parser/typescript.js +47 -0
  60. package/dist/parser/typescript.js.map +1 -0
  61. package/dist/parser/typescript.spec.d.ts +1 -0
  62. package/dist/parser/typescript.spec.js +106 -0
  63. package/dist/parser/typescript.spec.js.map +1 -0
  64. package/dist/pot/__snapshots__/catalog.spec.js.snap +591 -0
  65. package/dist/pot/catalog.d.ts +26 -0
  66. package/dist/pot/catalog.js +210 -0
  67. package/dist/pot/catalog.js.map +1 -0
  68. package/dist/pot/catalog.spec.d.ts +1 -0
  69. package/dist/pot/catalog.spec.js +240 -0
  70. package/dist/pot/catalog.spec.js.map +1 -0
  71. package/dist/pot/entry.d.ts +35 -0
  72. package/dist/pot/entry.js +322 -0
  73. package/dist/pot/entry.js.map +1 -0
  74. package/dist/pot/entry.spec.d.ts +1 -0
  75. package/dist/pot/entry.spec.js +317 -0
  76. package/dist/pot/entry.spec.js.map +1 -0
  77. package/dist/pot/keyword.d.ts +17 -0
  78. package/dist/pot/keyword.js +181 -0
  79. package/dist/pot/keyword.js.map +1 -0
  80. package/dist/pot/keyword.spec.d.ts +1 -0
  81. package/dist/pot/keyword.spec.js +54 -0
  82. package/dist/pot/keyword.spec.js.map +1 -0
  83. package/dist/xgettext/__snapshots__/xgettext.spec.js.snap +462 -0
  84. package/dist/xgettext/file-resolver.d.ts +5 -0
  85. package/dist/xgettext/file-resolver.js +23 -0
  86. package/dist/xgettext/file-resolver.js.map +1 -0
  87. package/dist/xgettext/file-resolver.spec.d.ts +1 -0
  88. package/dist/xgettext/file-resolver.spec.js +22 -0
  89. package/dist/xgettext/file-resolver.spec.js.map +1 -0
  90. package/dist/xgettext/files-collector.d.ts +5 -0
  91. package/dist/xgettext/files-collector.js +41 -0
  92. package/dist/xgettext/files-collector.js.map +1 -0
  93. package/dist/xgettext/files-collector.spec.d.ts +1 -0
  94. package/dist/xgettext/files-collector.spec.js +78 -0
  95. package/dist/xgettext/files-collector.spec.js.map +1 -0
  96. package/dist/xgettext/xgettext.d.ts +16 -0
  97. package/dist/xgettext/xgettext.js +335 -0
  98. package/dist/xgettext/xgettext.js.map +1 -0
  99. package/dist/xgettext/xgettext.spec.d.ts +1 -0
  100. package/dist/xgettext/xgettext.spec.js +743 -0
  101. package/dist/xgettext/xgettext.spec.js.map +1 -0
  102. package/package.json +88 -0
@@ -0,0 +1,176 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`parse po files encoding should accept cp1252 1`] = `
4
+ "# SOME DESCRIPTIVE TITLE
5
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
6
+ # This file is distributed under the same license as the PACKAGE package.
7
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
8
+ #
9
+ #, fuzzy
10
+ msgid ""
11
+ msgstr ""
12
+ "Project-Id-Version: PACKAGE VERSION\\n"
13
+ "Report-Msgid-Bugs-To: MSGID_BUGS_ADDRESS\\n"
14
+ "POT-Creation-Date: 2020-04-23 08:50+0300\\n"
15
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
16
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
17
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
18
+ "Language: \\n"
19
+ "MIME-Version: 1.0\\n"
20
+ "Content-Type: text/plain; charset=CHARSET\\n"
21
+ "Content-Transfer-Encoding: 8bit\\n"
22
+ "
23
+ `;
24
+
25
+ exports[`parse po files simple file should parse 1`] = `
26
+ "# SOME DESCRIPTIVE TITLE
27
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
28
+ # This file is distributed under the same license as the PACKAGE package.
29
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
30
+ #
31
+ # Translations for smell-o-vision.
32
+ # Copyright (C) 2020 SmellOVision Inc.
33
+ # This file is distributed under the same license as the smell-o-vision package.
34
+ # John Doe <john.doe@example.com>, 2020.
35
+ #
36
+ #, fuzzy
37
+ msgid ""
38
+ msgstr ""
39
+ "Project-Id-Version: PACKAGE VERSION\\n"
40
+ "Report-Msgid-Bugs-To: MSGID_BUGS_ADDRESS\\n"
41
+ "POT-Creation-Date: 2020-04-23 08:50+0300\\n"
42
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
43
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
44
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
45
+ "Language: \\n"
46
+ "MIME-Version: 1.0\\n"
47
+ "Content-Type: text/plain; charset=CHARSET\\n"
48
+ "Content-Transfer-Encoding: 8bit\\n"
49
+
50
+ msgid "no comment"
51
+ msgstr ""
52
+
53
+ #, perl-brace-format, no-perl-format
54
+ msgid "Hello, {name}!"
55
+ msgstr ""
56
+
57
+ #: src/example.ts:2304 src/other.ts:1303
58
+ msgid "strawberry"
59
+ msgstr ""
60
+
61
+ msgid "One universe"
62
+ msgid_plural "Parallel universes"
63
+ msgstr[0] ""
64
+ msgstr[1] ""
65
+
66
+ msgctxt "escapes"
67
+ msgid ""
68
+ "\\\\\\a\\b\\t\\n"
69
+ "\\v\\f\\r\\""
70
+ msgstr ""
71
+
72
+ msgid "Multi-line msgid"
73
+ msgstr ""
74
+
75
+ msgctxt "multi-line"
76
+ msgid ""
77
+ "Hello, world!\\n"
78
+ msgstr ""
79
+
80
+ msgid "One world"
81
+ msgid_plural "Many worlds"
82
+ msgstr[0] ""
83
+ msgstr[1] ""
84
+
85
+ msgid "The translation has to be discarded."
86
+ msgstr ""
87
+
88
+ msgid "And this one, too."
89
+ msgid_plural "And these ones, too."
90
+ msgstr[0] ""
91
+ msgstr[1] ""
92
+
93
+ #. . TRANSLATORS: This is the day of the week, not our star.
94
+ #. . It is the abbreviation for Sunday.
95
+ msgid "Sun"
96
+ msgstr ""
97
+ "
98
+ `;
99
+
100
+ exports[`parse po files warnings should warn about empty flags 1`] = `
101
+ "# SOME DESCRIPTIVE TITLE
102
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
103
+ # This file is distributed under the same license as the PACKAGE package.
104
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
105
+ #
106
+ #, fuzzy
107
+ msgid ""
108
+ msgstr ""
109
+ "Project-Id-Version: PACKAGE VERSION\\n"
110
+ "Report-Msgid-Bugs-To: MSGID_BUGS_ADDRESS\\n"
111
+ "POT-Creation-Date: 2020-04-23 08:50+0300\\n"
112
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
113
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
114
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
115
+ "Language: \\n"
116
+ "MIME-Version: 1.0\\n"
117
+ "Content-Type: text/plain; charset=CHARSET\\n"
118
+ "Content-Transfer-Encoding: 8bit\\n"
119
+
120
+ #, fuzzy, perl-brace-format
121
+ msgid "Hello, {name}!"
122
+ msgstr ""
123
+ "
124
+ `;
125
+
126
+ exports[`parse po files warnings should warn about empty flags reading from standard input 1`] = `
127
+ "# SOME DESCRIPTIVE TITLE
128
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
129
+ # This file is distributed under the same license as the PACKAGE package.
130
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
131
+ #
132
+ #, fuzzy
133
+ msgid ""
134
+ msgstr ""
135
+ "Project-Id-Version: PACKAGE VERSION\\n"
136
+ "Report-Msgid-Bugs-To: MSGID_BUGS_ADDRESS\\n"
137
+ "POT-Creation-Date: 2020-04-23 08:50+0300\\n"
138
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
139
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
140
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
141
+ "Language: \\n"
142
+ "MIME-Version: 1.0\\n"
143
+ "Content-Type: text/plain; charset=CHARSET\\n"
144
+ "Content-Transfer-Encoding: 8bit\\n"
145
+
146
+ #, fuzzy, perl-brace-format
147
+ msgid "Hello, {name}!"
148
+ msgstr ""
149
+ "
150
+ `;
151
+
152
+ exports[`parse po files warnings should warn about invalid references 1`] = `
153
+ "# SOME DESCRIPTIVE TITLE
154
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
155
+ # This file is distributed under the same license as the PACKAGE package.
156
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
157
+ #
158
+ #, fuzzy
159
+ msgid ""
160
+ msgstr ""
161
+ "Project-Id-Version: PACKAGE VERSION\\n"
162
+ "Report-Msgid-Bugs-To: MSGID_BUGS_ADDRESS\\n"
163
+ "POT-Creation-Date: 2020-04-23 08:50+0300\\n"
164
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
165
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
166
+ "Language-Team: LANGUAGE <LL@li.org>\\n"
167
+ "Language: \\n"
168
+ "MIME-Version: 1.0\\n"
169
+ "Content-Type: text/plain; charset=CHARSET\\n"
170
+ "Content-Transfer-Encoding: 8bit\\n"
171
+
172
+ #: src/here.js:2304
173
+ msgid "Hello, {name}!"
174
+ msgstr ""
175
+ "
176
+ `;
@@ -0,0 +1,4 @@
1
+ import { Parser } from './parser';
2
+ export declare class JavaScriptParser extends Parser {
3
+ doParse(input: string, filename: string): boolean;
4
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.JavaScriptParser = void 0;
19
+ var parser_1 = require("@babel/parser");
20
+ var parser_2 = require("./parser");
21
+ var JavaScriptParser = (function (_super) {
22
+ __extends(JavaScriptParser, _super);
23
+ function JavaScriptParser() {
24
+ return _super !== null && _super.apply(this, arguments) || this;
25
+ }
26
+ JavaScriptParser.prototype.doParse = function (input, filename) {
27
+ var ast = (0, parser_1.parse)(input, {
28
+ allowAwaitOutsideFunction: true,
29
+ allowImportExportEverywhere: true,
30
+ allowReturnOutsideFunction: true,
31
+ allowSuperOutsideMethod: true,
32
+ allowUndeclaredExports: true,
33
+ sourceFilename: filename,
34
+ plugins: [
35
+ 'flow',
36
+ 'jsx',
37
+ 'classProperties',
38
+ 'classPrivateMethods',
39
+ 'classPrivateProperties',
40
+ ],
41
+ });
42
+ return this.extract(filename, ast);
43
+ };
44
+ return JavaScriptParser;
45
+ }(parser_2.Parser));
46
+ exports.JavaScriptParser = JavaScriptParser;
47
+ //# sourceMappingURL=javascript.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"javascript.js","sourceRoot":"","sources":["../../src/parser/javascript.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,wCAAsC;AACtC,mCAAkC;AAElC;IAAsC,oCAAM;IAA5C;;IAsBA,CAAC;IArBA,kCAAO,GAAP,UAAQ,KAAa,EAAE,QAAgB;QACtC,IAAM,GAAG,GAAG,IAAA,cAAK,EAAC,KAAK,EAAE;YACxB,yBAAyB,EAAE,IAAI;YAC/B,2BAA2B,EAAE,IAAI;YACjC,0BAA0B,EAAE,IAAI;YAChC,uBAAuB,EAAE,IAAI;YAC7B,sBAAsB,EAAE,IAAI;YAG5B,cAAc,EAAE,QAAQ;YACxB,OAAO,EAAE;gBACR,MAAM;gBACN,KAAK;gBACL,iBAAiB;gBACjB,qBAAqB;gBACrB,wBAAwB;aACxB;SACD,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IACpC,CAAC;IACF,uBAAC;AAAD,CAAC,AAtBD,CAAsC,eAAM,GAsB3C;AAtBY,4CAAgB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,436 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var catalog_1 = require("../pot/catalog");
4
+ var keyword_1 = require("../pot/keyword");
5
+ var javascript_1 = require("./javascript");
6
+ var warnSpy = jest.spyOn(global.console, 'warn').mockImplementation(function () {
7
+ });
8
+ var errorSpy = jest.spyOn(global.console, 'error').mockImplementation(function () {
9
+ });
10
+ describe('JavaScript parser', function () {
11
+ describe('strings', function () {
12
+ afterEach(function () {
13
+ warnSpy.mockClear();
14
+ errorSpy.mockClear();
15
+ });
16
+ it('should extract all kinds of strings', function () {
17
+ var catalog = new catalog_1.Catalog();
18
+ var p = new javascript_1.JavaScriptParser(catalog, { extractAll: true });
19
+ var code = "\n// Directive.\n_(\"double-quoted string\");\n\n// Function call.\n_('single-quoted string');\n\n// perl-brace-format.\n_x('Hello, {name}!');\n";
20
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
21
+ expect(catalog.toString({ omitHeader: true })).toMatchSnapshot();
22
+ expect(errorSpy).not.toHaveBeenCalled();
23
+ expect(warnSpy).not.toHaveBeenCalled();
24
+ });
25
+ it('should extract concatenated strings', function () {
26
+ var catalog = new catalog_1.Catalog();
27
+ var p = new javascript_1.JavaScriptParser(catalog, { extractAll: true });
28
+ var code = '"concatenated" + " str" + "i" + "n" + "g";';
29
+ var buf = Buffer.from(code);
30
+ expect(p.parse(buf, 'example.js')).toBeTruthy();
31
+ var expected = "#: example.js:1\nmsgid \"concatenated string\"\nmsgstr \"\"\n";
32
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
33
+ expect(errorSpy).not.toHaveBeenCalled();
34
+ expect(warnSpy).not.toHaveBeenCalled();
35
+ });
36
+ it('should not extract concatenated strings with a leading variable', function () {
37
+ var catalog = new catalog_1.Catalog();
38
+ var p = new javascript_1.JavaScriptParser(catalog, { extractAll: true });
39
+ var code = 'prefix + "concatenated" + " string";';
40
+ var buf = Buffer.from(code);
41
+ expect(p.parse(buf, 'example.js')).toBeTruthy();
42
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
43
+ expect(errorSpy).not.toHaveBeenCalled();
44
+ expect(warnSpy).not.toHaveBeenCalled();
45
+ });
46
+ it('should not extract concatenated strings mixed with a variable', function () {
47
+ var catalog = new catalog_1.Catalog();
48
+ var p = new javascript_1.JavaScriptParser(catalog, { extractAll: true });
49
+ var code = '"concatenated" + sep + "string";';
50
+ var buf = Buffer.from(code);
51
+ expect(p.parse(buf, 'example.js')).toBeTruthy();
52
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
53
+ expect(errorSpy).not.toHaveBeenCalled();
54
+ expect(warnSpy).not.toHaveBeenCalled();
55
+ });
56
+ it('should not extract concatenated strings with a trailing variable', function () {
57
+ var catalog = new catalog_1.Catalog();
58
+ var p = new javascript_1.JavaScriptParser(catalog, { extractAll: true });
59
+ var code = '"concatenated" + " string" + suffix;';
60
+ var buf = Buffer.from(code);
61
+ expect(p.parse(buf, 'example.js')).toBeTruthy();
62
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
63
+ expect(errorSpy).not.toHaveBeenCalled();
64
+ expect(warnSpy).not.toHaveBeenCalled();
65
+ });
66
+ });
67
+ describe('comments', function () {
68
+ afterEach(function () {
69
+ warnSpy.mockClear();
70
+ errorSpy.mockClear();
71
+ });
72
+ it('should extract all kinds of comments', function () {
73
+ var catalog = new catalog_1.Catalog();
74
+ var p = new javascript_1.JavaScriptParser(catalog, {
75
+ extractAll: true,
76
+ addAllComments: true,
77
+ });
78
+ var code = "\n// Single-line comment.\n_('single line');\n\n/* Block comment. */\n_('block');\n\n// Single-line\n/* and block combined. */\n_('single + block');\n\n/* Block */\n// and single-line combined.\n_('block + single');\n\n// Skip this.\n\n// But catch this.\n_('only immediately preceding');\n\n// Only for one string.\n_('catcher'); _('loser');\n\n// And this is ignored, too.\n";
79
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
80
+ expect(catalog.toString({ omitHeader: true })).toMatchSnapshot();
81
+ expect(errorSpy).not.toHaveBeenCalled();
82
+ expect(warnSpy).not.toHaveBeenCalled();
83
+ });
84
+ });
85
+ describe('messages', function () {
86
+ afterEach(function () {
87
+ warnSpy.mockClear();
88
+ errorSpy.mockClear();
89
+ });
90
+ it('should extract a single argument', function () {
91
+ var catalog = new catalog_1.Catalog();
92
+ var p = new javascript_1.JavaScriptParser(catalog, {
93
+ keyword: [new keyword_1.Keyword('_')],
94
+ });
95
+ var code = '_("Hello, world!")';
96
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
97
+ var expected = "#: example.js:1\nmsgid \"Hello, world!\"\nmsgstr \"\"\n";
98
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
99
+ expect(errorSpy).not.toHaveBeenCalled();
100
+ expect(warnSpy).not.toHaveBeenCalled();
101
+ });
102
+ it('should extract singular and plural form', function () {
103
+ var catalog = new catalog_1.Catalog();
104
+ var p = new javascript_1.JavaScriptParser(catalog, {
105
+ keyword: [new keyword_1.Keyword('_n', ['1', '2'])],
106
+ });
107
+ var code = '_n("There was an error!", "There were errors!", n)';
108
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
109
+ var expected = "#: example.js:1\nmsgid \"There was an error!\"\nmsgid_plural \"There were errors!\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n";
110
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
111
+ expect(errorSpy).not.toHaveBeenCalled();
112
+ expect(warnSpy).not.toHaveBeenCalled();
113
+ });
114
+ it('should extract context, singular and plural form', function () {
115
+ var catalog = new catalog_1.Catalog();
116
+ var p = new javascript_1.JavaScriptParser(catalog, {
117
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
118
+ });
119
+ var code = '_np("context", "There was an error!", "There were errors!", n)';
120
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
121
+ var expected = "#: example.js:1\nmsgctxt \"context\"\nmsgid \"There was an error!\"\nmsgid_plural \"There were errors!\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n";
122
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
123
+ expect(errorSpy).not.toHaveBeenCalled();
124
+ expect(warnSpy).not.toHaveBeenCalled();
125
+ });
126
+ it('should handle other callees', function () {
127
+ var catalog = new catalog_1.Catalog();
128
+ var p = new javascript_1.JavaScriptParser(catalog, {
129
+ keyword: [new keyword_1.Keyword('_')],
130
+ });
131
+ var code = '"string".trim()';
132
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
133
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
134
+ expect(errorSpy).not.toHaveBeenCalled();
135
+ expect(warnSpy).not.toHaveBeenCalled();
136
+ });
137
+ it('should not extract from unknown methods', function () {
138
+ var catalog = new catalog_1.Catalog();
139
+ var p = new javascript_1.JavaScriptParser(catalog, {
140
+ keyword: [new keyword_1.Keyword('_')],
141
+ });
142
+ var code = 'gettext("string")';
143
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
144
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
145
+ expect(errorSpy).not.toHaveBeenCalled();
146
+ expect(warnSpy).not.toHaveBeenCalled();
147
+ });
148
+ it('should honor the total arguments spec', function () {
149
+ var catalog = new catalog_1.Catalog();
150
+ var p = new javascript_1.JavaScriptParser(catalog, {
151
+ keyword: [new keyword_1.Keyword('_', ['1t'])],
152
+ });
153
+ var code = '_("Hello", "world")';
154
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
155
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
156
+ expect(errorSpy).not.toHaveBeenCalled();
157
+ expect(warnSpy).not.toHaveBeenCalled();
158
+ });
159
+ it('should not extract non existing singular arguments', function () {
160
+ var catalog = new catalog_1.Catalog();
161
+ var p = new javascript_1.JavaScriptParser(catalog, {
162
+ keyword: [new keyword_1.Keyword('_')],
163
+ });
164
+ var code = '_()';
165
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
166
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
167
+ expect(errorSpy).not.toHaveBeenCalled();
168
+ expect(warnSpy).not.toHaveBeenCalled();
169
+ });
170
+ it('should not extract non existing plural arguments', function () {
171
+ var catalog = new catalog_1.Catalog();
172
+ var p = new javascript_1.JavaScriptParser(catalog, {
173
+ keyword: [new keyword_1.Keyword('_n', ['1', '2'])],
174
+ });
175
+ var code = '_n("One universe")';
176
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
177
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
178
+ expect(errorSpy).not.toHaveBeenCalled();
179
+ expect(warnSpy).not.toHaveBeenCalled();
180
+ });
181
+ it('should not extract non existing context arguments', function () {
182
+ var catalog = new catalog_1.Catalog();
183
+ var p = new javascript_1.JavaScriptParser(catalog, {
184
+ keyword: [new keyword_1.Keyword('_pn', ['1', '2', '3c'])],
185
+ });
186
+ var code = '_pn("one world", "many worlds")';
187
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
188
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
189
+ expect(errorSpy).not.toHaveBeenCalled();
190
+ expect(warnSpy).not.toHaveBeenCalled();
191
+ });
192
+ it('should reject variables as singular arguments', function () {
193
+ var catalog = new catalog_1.Catalog();
194
+ var p = new javascript_1.JavaScriptParser(catalog, {
195
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
196
+ });
197
+ var code = '_np("world", earth, "many earths")';
198
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
199
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
200
+ expect(errorSpy).not.toHaveBeenCalled();
201
+ expect(warnSpy).not.toHaveBeenCalled();
202
+ });
203
+ it('should allow simple template literals as singular arguments', function () {
204
+ var catalog = new catalog_1.Catalog();
205
+ var p = new javascript_1.JavaScriptParser(catalog, {
206
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
207
+ });
208
+ var code = '_np("world", `one earth`, "many earths")';
209
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
210
+ var expected = "#: example.js:1\nmsgctxt \"world\"\nmsgid \"one earth\"\nmsgid_plural \"many earths\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n";
211
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
212
+ expect(errorSpy).not.toHaveBeenCalled();
213
+ expect(warnSpy).not.toHaveBeenCalled();
214
+ });
215
+ it('should reject template literals with embedded expressions as singular arguments', function () {
216
+ var catalog = new catalog_1.Catalog();
217
+ var p = new javascript_1.JavaScriptParser(catalog, {
218
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
219
+ });
220
+ var code = '_np("world", `one ${planet}`, "many earths")';
221
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeFalsy();
222
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
223
+ expect(errorSpy).toHaveBeenCalledTimes(1);
224
+ expect(errorSpy).toHaveBeenNthCalledWith(1, 'example.js:1:13-1:28: error: template literals with embedded' +
225
+ ' expressions are not allowed as arguments to gettext' +
226
+ ' functions because they are not constant');
227
+ expect(warnSpy).not.toHaveBeenCalled();
228
+ });
229
+ it('should reject variables as plural arguments', function () {
230
+ var catalog = new catalog_1.Catalog();
231
+ var p = new javascript_1.JavaScriptParser(catalog, {
232
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
233
+ });
234
+ var code = '_np("world", "earth", earths)';
235
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
236
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
237
+ expect(errorSpy).not.toHaveBeenCalled();
238
+ expect(warnSpy).not.toHaveBeenCalled();
239
+ });
240
+ it('should allow simple template literals as plural arguments', function () {
241
+ var catalog = new catalog_1.Catalog();
242
+ var p = new javascript_1.JavaScriptParser(catalog, {
243
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
244
+ });
245
+ var code = '_np("world", "one earth", `many earths`)';
246
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
247
+ var expected = "#: example.js:1\nmsgctxt \"world\"\nmsgid \"one earth\"\nmsgid_plural \"many earths\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n";
248
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
249
+ expect(errorSpy).not.toHaveBeenCalled();
250
+ expect(warnSpy).not.toHaveBeenCalled();
251
+ });
252
+ it('should reject template literals with embedded expressions as plural arguments', function () {
253
+ var catalog = new catalog_1.Catalog();
254
+ var p = new javascript_1.JavaScriptParser(catalog, {
255
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
256
+ });
257
+ var code = '_np("world", "one earth", `many ${planets}`)';
258
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeFalsy();
259
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
260
+ expect(errorSpy).toHaveBeenCalledTimes(1);
261
+ expect(errorSpy).toHaveBeenNthCalledWith(1, 'example.js:1:26-1:43: error: template literals with embedded' +
262
+ ' expressions are not allowed as arguments to gettext' +
263
+ ' functions because they are not constant');
264
+ expect(warnSpy).not.toHaveBeenCalled();
265
+ });
266
+ it('should reject variables as msgctxt arguments', function () {
267
+ var catalog = new catalog_1.Catalog();
268
+ var p = new javascript_1.JavaScriptParser(catalog, {
269
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
270
+ });
271
+ var code = '_np(world, "earth", "many earths")';
272
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
273
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
274
+ expect(errorSpy).not.toHaveBeenCalled();
275
+ expect(warnSpy).not.toHaveBeenCalled();
276
+ });
277
+ it('should allow simple template literals as msgctxt arguments', function () {
278
+ var catalog = new catalog_1.Catalog();
279
+ var p = new javascript_1.JavaScriptParser(catalog, {
280
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
281
+ });
282
+ var code = '_np(`world`, "one earth", "many earths")';
283
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
284
+ var expected = "#: example.js:1\nmsgctxt \"world\"\nmsgid \"one earth\"\nmsgid_plural \"many earths\"\nmsgstr[0] \"\"\nmsgstr[1] \"\"\n";
285
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
286
+ expect(errorSpy).not.toHaveBeenCalled();
287
+ expect(warnSpy).not.toHaveBeenCalled();
288
+ });
289
+ it('should reject template literals with embedded expressions as msgctxt arguments', function () {
290
+ var catalog = new catalog_1.Catalog();
291
+ var p = new javascript_1.JavaScriptParser(catalog, {
292
+ keyword: [new keyword_1.Keyword('_np', ['1c', '2', '3'])],
293
+ });
294
+ var code = '_np("world", "one earth", `many ${planets}`)';
295
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeFalsy();
296
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
297
+ expect(errorSpy).toHaveBeenCalledTimes(1);
298
+ expect(errorSpy).toHaveBeenNthCalledWith(1, 'example.js:1:26-1:43: error: template literals with embedded' +
299
+ ' expressions are not allowed as arguments to gettext' +
300
+ ' functions because they are not constant');
301
+ expect(warnSpy).not.toHaveBeenCalled();
302
+ });
303
+ });
304
+ describe('encoding', function () {
305
+ afterEach(function () {
306
+ warnSpy.mockClear();
307
+ errorSpy.mockClear();
308
+ });
309
+ it('should decode to JavaScript strings', function () {
310
+ var catalog = new catalog_1.Catalog();
311
+ var p = new javascript_1.JavaScriptParser(catalog, {
312
+ keyword: [new keyword_1.Keyword('_')],
313
+ });
314
+ var code = '_("Hello, world!")';
315
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
316
+ var expected = "#: example.js:1\nmsgid \"Hello, world!\"\nmsgstr \"\"\n";
317
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
318
+ expect(errorSpy).not.toHaveBeenCalled();
319
+ expect(warnSpy).not.toHaveBeenCalled();
320
+ });
321
+ });
322
+ describe('binary expressions', function () {
323
+ afterEach(function () {
324
+ warnSpy.mockClear();
325
+ errorSpy.mockClear();
326
+ });
327
+ it('should extract concatenated strings', function () {
328
+ var catalog = new catalog_1.Catalog();
329
+ var p = new javascript_1.JavaScriptParser(catalog, {
330
+ keyword: [new keyword_1.Keyword('_')],
331
+ });
332
+ var code = '_("It\'s a " + "sad" + " and beautiful world!")';
333
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
334
+ var expected = "#: example.js:1\nmsgid \"It's a sad and beautiful world!\"\nmsgstr \"\"\n";
335
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
336
+ expect(errorSpy).not.toHaveBeenCalled();
337
+ expect(warnSpy).not.toHaveBeenCalled();
338
+ });
339
+ it('should extract concatenated strings with simple template literals', function () {
340
+ var catalog = new catalog_1.Catalog();
341
+ var p = new javascript_1.JavaScriptParser(catalog, {
342
+ keyword: [new keyword_1.Keyword('_')],
343
+ });
344
+ var code = '_("It\'s a " + `sad` + " and beautiful world!")';
345
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
346
+ var expected = "#: example.js:1\nmsgid \"It's a sad and beautiful world!\"\nmsgstr \"\"\n";
347
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
348
+ expect(errorSpy).not.toHaveBeenCalled();
349
+ expect(warnSpy).not.toHaveBeenCalled();
350
+ });
351
+ it('should ignore concatenated strings with variables', function () {
352
+ var catalog = new catalog_1.Catalog();
353
+ var p = new javascript_1.JavaScriptParser(catalog, {
354
+ keyword: [new keyword_1.Keyword('_')],
355
+ });
356
+ var code = '_("It\'s a " + what + " and beautiful world!")';
357
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
358
+ expect(catalog.toString({ omitHeader: true })).toEqual('');
359
+ expect(errorSpy).not.toHaveBeenCalled();
360
+ expect(warnSpy).not.toHaveBeenCalled();
361
+ });
362
+ it('should report concatenated strings with interpolations', function () {
363
+ var catalog = new catalog_1.Catalog();
364
+ var p = new javascript_1.JavaScriptParser(catalog, {
365
+ keyword: [new keyword_1.Keyword('_')],
366
+ });
367
+ var code = '_("It\'s " + `a ${what}` + " and beautiful world!")';
368
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeFalsy();
369
+ expect(errorSpy).toHaveBeenCalledTimes(1);
370
+ expect(errorSpy).toHaveBeenCalledWith('example.js:1:12-1:23: error:' +
371
+ ' template literals with embedded expressions are not' +
372
+ ' allowed as arguments to gettext functions because' +
373
+ ' they are not constant');
374
+ expect(warnSpy).not.toHaveBeenCalled();
375
+ });
376
+ });
377
+ describe('comments above calls', function () {
378
+ afterEach(function () {
379
+ warnSpy.mockClear();
380
+ errorSpy.mockClear();
381
+ });
382
+ it('should extract xgettext: comments', function () {
383
+ var catalog = new catalog_1.Catalog();
384
+ var p = new javascript_1.JavaScriptParser(catalog, {
385
+ keyword: [new keyword_1.Keyword('_')],
386
+ });
387
+ var code = "// xgettext: no-perl-brace-format\n_(\"It's a {sad} and beautiful world!\")\n";
388
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
389
+ var expected = "#: example.js:2\n#, no-perl-brace-format\nmsgid \"It's a {sad} and beautiful world!\"\nmsgstr \"\"\n";
390
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
391
+ expect(errorSpy).not.toHaveBeenCalled();
392
+ expect(warnSpy).not.toHaveBeenCalled();
393
+ });
394
+ it('should ignore invalid xgettext: comments', function () {
395
+ var catalog = new catalog_1.Catalog();
396
+ var p = new javascript_1.JavaScriptParser(catalog, {
397
+ keyword: [new keyword_1.Keyword('_')],
398
+ });
399
+ var code = "// xgettext: down-by-law\n_(\"It's a sad and beautiful world!\")\n";
400
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
401
+ var expected = "#: example.js:2\nmsgid \"It's a sad and beautiful world!\"\nmsgstr \"\"\n";
402
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
403
+ expect(errorSpy).not.toHaveBeenCalled();
404
+ expect(warnSpy).not.toHaveBeenCalled();
405
+ });
406
+ it('should extract translator comments', function () {
407
+ var catalog = new catalog_1.Catalog();
408
+ var p = new javascript_1.JavaScriptParser(catalog, {
409
+ keyword: [new keyword_1.Keyword('_')],
410
+ addComments: ['TRANSLATORS:'],
411
+ });
412
+ var code = "// TRANSLATORS: Down by Law\n_(\"It's a sad and beautiful world!\")\n";
413
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
414
+ var expected = "#. TRANSLATORS: Down by Law\n#: example.js:2\nmsgid \"It's a sad and beautiful world!\"\nmsgstr \"\"\n";
415
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
416
+ expect(errorSpy).not.toHaveBeenCalled();
417
+ expect(warnSpy).not.toHaveBeenCalled();
418
+ expect(warnSpy).not.toHaveBeenCalled();
419
+ });
420
+ it('should ignore other comments', function () {
421
+ var catalog = new catalog_1.Catalog();
422
+ var p = new javascript_1.JavaScriptParser(catalog, {
423
+ keyword: [new keyword_1.Keyword('_')],
424
+ addComments: ['TRANSLATORS:'],
425
+ });
426
+ var code = "// TERMINATORS: Down by Law\n_(\"It's a sad and beautiful world!\")\n";
427
+ expect(p.parse(Buffer.from(code), 'example.js')).toBeTruthy();
428
+ var expected = "#: example.js:2\nmsgid \"It's a sad and beautiful world!\"\nmsgstr \"\"\n";
429
+ expect(catalog.toString({ omitHeader: true })).toEqual(expected);
430
+ expect(errorSpy).not.toHaveBeenCalled();
431
+ expect(warnSpy).not.toHaveBeenCalled();
432
+ expect(warnSpy).not.toHaveBeenCalled();
433
+ });
434
+ });
435
+ });
436
+ //# sourceMappingURL=javascript.spec.js.map