@esgettext/tools 1.0.0 → 1.2.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 (113) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +197 -22
  3. package/dist/command.js +2 -0
  4. package/dist/commands/convert.js +286 -0
  5. package/dist/commands/install.js +257 -0
  6. package/dist/commands/msgfmt-all.js +219 -0
  7. package/dist/commands/msgmerge-all.js +230 -0
  8. package/dist/commands/xgettext/file-resolver.js +20 -0
  9. package/dist/commands/xgettext/files-collector.js +39 -0
  10. package/dist/commands/xgettext.js +534 -0
  11. package/dist/configuration.js +277 -0
  12. package/dist/esgettext-package-json.js +2 -0
  13. package/dist/index.js +135 -0
  14. package/dist/package.js +10 -12
  15. package/dist/parser/javascript.js +9 -28
  16. package/dist/parser/parser.js +176 -148
  17. package/dist/parser/po.js +144 -128
  18. package/dist/parser/typescript.js +10 -28
  19. package/dist/pot/catalog.js +134 -83
  20. package/dist/pot/entry.js +173 -115
  21. package/dist/pot/keyword.js +66 -92
  22. package/package.json +38 -25
  23. package/bin/esgettext-install.js +0 -5
  24. package/bin/esgettext-mo2json.js +0 -5
  25. package/bin/esgettext-msgfmt-all.js +0 -5
  26. package/bin/esgettext-msgmerge-all.js +0 -5
  27. package/bin/esgettext-xgettext.js +0 -5
  28. package/dist/cli/getopt.d.ts +0 -34
  29. package/dist/cli/getopt.js +0 -150
  30. package/dist/cli/getopt.js.map +0 -1
  31. package/dist/cli/getopt.spec.d.ts +0 -1
  32. package/dist/cli/getopt.spec.js +0 -164
  33. package/dist/cli/getopt.spec.js.map +0 -1
  34. package/dist/cli/install.d.ts +0 -1
  35. package/dist/cli/install.js +0 -91
  36. package/dist/cli/install.js.map +0 -1
  37. package/dist/cli/mo2json-cli.d.ts +0 -1
  38. package/dist/cli/mo2json-cli.js +0 -48
  39. package/dist/cli/mo2json-cli.js.map +0 -1
  40. package/dist/cli/msgfmt-all.d.ts +0 -1
  41. package/dist/cli/msgfmt-all.js +0 -99
  42. package/dist/cli/msgfmt-all.js.map +0 -1
  43. package/dist/cli/msgmerge-all.d.ts +0 -1
  44. package/dist/cli/msgmerge-all.js +0 -85
  45. package/dist/cli/msgmerge-all.js.map +0 -1
  46. package/dist/cli/xgettext-cli.d.ts +0 -1
  47. package/dist/cli/xgettext-cli.js +0 -303
  48. package/dist/cli/xgettext-cli.js.map +0 -1
  49. package/dist/install/install.d.ts +0 -10
  50. package/dist/install/install.js +0 -157
  51. package/dist/install/install.js.map +0 -1
  52. package/dist/msgfmt-all/msgfmt-all.d.ts +0 -8
  53. package/dist/msgfmt-all/msgfmt-all.js +0 -142
  54. package/dist/msgfmt-all/msgfmt-all.js.map +0 -1
  55. package/dist/msgmerge-all/msgmerge-all.d.ts +0 -9
  56. package/dist/msgmerge-all/msgmerge-all.js +0 -157
  57. package/dist/msgmerge-all/msgmerge-all.js.map +0 -1
  58. package/dist/package.d.ts +0 -4
  59. package/dist/package.js.map +0 -1
  60. package/dist/parser/__snapshots__/javascript.spec.js.snap +0 -56
  61. package/dist/parser/__snapshots__/po.spec.js.snap +0 -176
  62. package/dist/parser/javascript.d.ts +0 -4
  63. package/dist/parser/javascript.js.map +0 -1
  64. package/dist/parser/javascript.spec.d.ts +0 -1
  65. package/dist/parser/javascript.spec.js +0 -436
  66. package/dist/parser/javascript.spec.js.map +0 -1
  67. package/dist/parser/parser.d.ts +0 -45
  68. package/dist/parser/parser.js.map +0 -1
  69. package/dist/parser/po.d.ts +0 -22
  70. package/dist/parser/po.js.map +0 -1
  71. package/dist/parser/po.spec.d.ts +0 -1
  72. package/dist/parser/po.spec.js +0 -296
  73. package/dist/parser/po.spec.js.map +0 -1
  74. package/dist/parser/typescript.d.ts +0 -4
  75. package/dist/parser/typescript.js.map +0 -1
  76. package/dist/parser/typescript.spec.d.ts +0 -1
  77. package/dist/parser/typescript.spec.js +0 -106
  78. package/dist/parser/typescript.spec.js.map +0 -1
  79. package/dist/pot/__snapshots__/catalog.spec.js.snap +0 -591
  80. package/dist/pot/catalog.d.ts +0 -26
  81. package/dist/pot/catalog.js.map +0 -1
  82. package/dist/pot/catalog.spec.d.ts +0 -1
  83. package/dist/pot/catalog.spec.js +0 -240
  84. package/dist/pot/catalog.spec.js.map +0 -1
  85. package/dist/pot/entry.d.ts +0 -35
  86. package/dist/pot/entry.js.map +0 -1
  87. package/dist/pot/entry.spec.d.ts +0 -1
  88. package/dist/pot/entry.spec.js +0 -317
  89. package/dist/pot/entry.spec.js.map +0 -1
  90. package/dist/pot/keyword.d.ts +0 -17
  91. package/dist/pot/keyword.js.map +0 -1
  92. package/dist/pot/keyword.spec.d.ts +0 -1
  93. package/dist/pot/keyword.spec.js +0 -54
  94. package/dist/pot/keyword.spec.js.map +0 -1
  95. package/dist/xgettext/__snapshots__/xgettext.spec.js.snap +0 -462
  96. package/dist/xgettext/file-resolver.d.ts +0 -5
  97. package/dist/xgettext/file-resolver.js +0 -23
  98. package/dist/xgettext/file-resolver.js.map +0 -1
  99. package/dist/xgettext/file-resolver.spec.d.ts +0 -1
  100. package/dist/xgettext/file-resolver.spec.js +0 -22
  101. package/dist/xgettext/file-resolver.spec.js.map +0 -1
  102. package/dist/xgettext/files-collector.d.ts +0 -5
  103. package/dist/xgettext/files-collector.js +0 -41
  104. package/dist/xgettext/files-collector.js.map +0 -1
  105. package/dist/xgettext/files-collector.spec.d.ts +0 -1
  106. package/dist/xgettext/files-collector.spec.js +0 -78
  107. package/dist/xgettext/files-collector.spec.js.map +0 -1
  108. package/dist/xgettext/xgettext.d.ts +0 -16
  109. package/dist/xgettext/xgettext.js +0 -335
  110. package/dist/xgettext/xgettext.js.map +0 -1
  111. package/dist/xgettext/xgettext.spec.d.ts +0 -1
  112. package/dist/xgettext/xgettext.spec.js +0 -743
  113. package/dist/xgettext/xgettext.spec.js.map +0 -1
@@ -27,40 +27,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.Parser = void 0;
30
- var iconv_lite_1 = require("iconv-lite");
31
- var traverse_1 = __importDefault(require("@babel/traverse"));
32
- var t = __importStar(require("@babel/types"));
33
- var runtime_1 = require("@esgettext/runtime");
34
- var entry_1 = require("../pot/entry");
35
- var keyword_1 = require("../pot/keyword");
36
- var gtx = runtime_1.Textdomain.getInstance('tools');
37
- var Parser = (function () {
38
- function Parser(catalog, options) {
39
- if (options === void 0) { options = {}; }
40
- var _this = this;
30
+ const iconv_lite_1 = require("iconv-lite");
31
+ const traverse_1 = __importDefault(require("@babel/traverse"));
32
+ const t = __importStar(require("@babel/types"));
33
+ const runtime_1 = require("@esgettext/runtime");
34
+ const entry_1 = require("../pot/entry");
35
+ const keyword_1 = require("../pot/keyword");
36
+ const gtx = runtime_1.Textdomain.getInstance('tools');
37
+ class Parser {
38
+ constructor(catalog, options = {}) {
41
39
  this.catalog = catalog;
42
40
  this.options = options;
41
+ this.filename = '';
42
+ this.errors = 0;
43
+ this.comments = [];
43
44
  this.keywords = {};
44
- (options.keyword || Parser.cookedDefaultKeywords()).forEach(function (keyword) {
45
- _this.keywords[keyword.method] = keyword;
45
+ (options.keyword || Parser.cookedDefaultKeywords()).forEach(keyword => {
46
+ this.keywords[keyword.method] = keyword;
46
47
  });
47
48
  if (options.instance) {
48
49
  this.instances = new Array();
49
- for (var i = 0; i < options.instance.length; ++i) {
50
+ for (let i = 0; i < options.instance.length; ++i) {
50
51
  this.instances[i] = options.instance[i].split('.').reverse();
51
52
  }
52
53
  }
53
54
  }
54
- Parser.prototype.parse = function (buf, filename) {
55
+ parse(buf, filename) {
55
56
  this.filename = filename;
56
- var encoding = typeof this.options.fromCode === 'undefined'
57
+ const encoding = typeof this.options.fromCode === 'undefined'
57
58
  ? 'ascii'
58
59
  : this.options.fromCode;
59
- var canonical = this.canonicalizeEncoding(encoding);
60
- var input;
61
- var pos = 0;
60
+ const canonical = this.canonicalizeEncoding(encoding);
61
+ let input;
62
+ let pos = 0;
62
63
  if ('ascii' === canonical) {
63
- if (buf.find(function (ord, idx) {
64
+ if (buf.find((ord, idx) => {
64
65
  if (ord > 0x7f) {
65
66
  pos = idx;
66
67
  return true;
@@ -69,17 +70,22 @@ var Parser = (function () {
69
70
  return false;
70
71
  }
71
72
  })) {
72
- var loc = {
73
+ const loc = {
73
74
  start: {
74
75
  line: 1,
75
- column: pos,
76
- index: 0
76
+ column: 1,
77
+ index: 1,
78
+ },
79
+ end: {
80
+ line: 0,
81
+ column: 0,
82
+ index: 0,
77
83
  },
78
- end: null,
79
- filename: filename,
80
- identifierName: null
84
+ filename,
85
+ identifierName: null,
81
86
  };
82
- for (var i = 0; i < pos; ++i) {
87
+ // Find the offending character.
88
+ for (let i = 0; i < pos; ++i) {
83
89
  if (buf[i] === 10) {
84
90
  ++loc.start.line;
85
91
  loc.start.column = 1;
@@ -95,7 +101,7 @@ var Parser = (function () {
95
101
  input = buf.toString();
96
102
  }
97
103
  else if ('utf8' === canonical) {
98
- var loc = this.findNonUtf8(buf);
104
+ const loc = this.findNonUtf8(buf);
99
105
  if (loc !== null) {
100
106
  this.error(gtx._('invalid multibyte sequence'), loc);
101
107
  return false;
@@ -103,18 +109,19 @@ var Parser = (function () {
103
109
  input = buf.toString();
104
110
  }
105
111
  else {
112
+ // Convert.
106
113
  try {
107
114
  input = (0, iconv_lite_1.decode)(buf, this.options.fromCode);
108
115
  }
109
116
  catch (e) {
110
- var usedFilename = '-' === filename ? gtx._('[standard input]') : filename;
111
- console.error("".concat(usedFilename, ": ").concat(e));
117
+ const usedFilename = '-' === filename ? gtx._('[standard input]') : filename;
118
+ console.error(`${usedFilename}: ${e}`);
112
119
  return false;
113
120
  }
114
121
  }
115
122
  return this.doParse(input, filename);
116
- };
117
- Parser.prototype.extract = function (filename, ast) {
123
+ }
124
+ extract(filename, ast) {
118
125
  this.errors = 0;
119
126
  this.comments = this.filterComments(ast.comments);
120
127
  this.filename = filename;
@@ -125,33 +132,38 @@ var Parser = (function () {
125
132
  this.extractStrings(ast);
126
133
  }
127
134
  return !this.errors;
128
- };
129
- Parser.prototype.extractStrings = function (ast) {
130
- var _this = this;
135
+ }
136
+ extractStrings(ast) {
131
137
  (0, traverse_1.default)(ast, {
132
- CallExpression: function (path) {
133
- _this.extractArguments(path);
138
+ CallExpression: path => {
139
+ this.extractArguments(path);
134
140
  },
135
141
  });
136
- };
137
- Parser.prototype.extractAllStrings = function (ast) {
138
- var _this = this;
142
+ }
143
+ extractAllStrings(ast) {
144
+ // Step 1: Transform string concatenations into one string.
139
145
  (0, traverse_1.default)(ast, {
140
- StringLiteral: function (path) {
141
- _this.concatStrings(path);
146
+ StringLiteral: path => {
147
+ this.concatStrings(path);
142
148
  },
143
149
  });
150
+ // Step 2: Extract the remaining strings. Those that are part of a
151
+ // binary expression have not been recognized in step 1 and must be
152
+ // ignored.
144
153
  (0, traverse_1.default)(ast, {
145
- StringLiteral: function (path) {
154
+ StringLiteral: path => {
146
155
  if (!t.isBinaryExpression(path.parentPath.node)) {
147
- var loc = path.node.loc;
148
- _this.addEntry({ msgid: path.node.value, loc: loc });
156
+ const loc = path.node.loc;
157
+ this.addEntry({
158
+ msgid: path.node.value,
159
+ loc: loc,
160
+ });
149
161
  }
150
162
  },
151
163
  });
152
- };
153
- Parser.prototype.checkPath = function (wanted, got) {
154
- for (var i = 0; i < got.length; ++i) {
164
+ }
165
+ checkPath(wanted, got) {
166
+ for (let i = 0; i < got.length; ++i) {
155
167
  if (i >= wanted.length) {
156
168
  return true;
157
169
  }
@@ -160,25 +172,25 @@ var Parser = (function () {
160
172
  }
161
173
  }
162
174
  return true;
163
- };
164
- Parser.prototype.checkInstance = function (instance) {
165
- if (!this.options.instance) {
175
+ }
176
+ checkInstance(instance) {
177
+ if (!this.instances) {
166
178
  return true;
167
179
  }
168
- for (var i = 0; i < this.instances.length; ++i) {
180
+ for (let i = 0; i < this.instances.length; ++i) {
169
181
  if (this.checkPath(this.instances[i], instance)) {
170
182
  return true;
171
183
  }
172
184
  }
173
185
  return false;
174
- };
175
- Parser.prototype.extractArguments = function (path) {
176
- var method;
186
+ }
187
+ extractArguments(path) {
188
+ let method;
177
189
  if (t.isIdentifier(path.node.callee) && !this.instances) {
178
190
  method = path.node.callee.name;
179
191
  }
180
192
  else if (t.isMemberExpression(path.node.callee)) {
181
- var instance = new Array();
193
+ const instance = new Array();
182
194
  method = this.methodFromMemberExpression(path.node.callee, instance);
183
195
  if (method === null) {
184
196
  return;
@@ -194,7 +206,8 @@ var Parser = (function () {
194
206
  if (!Object.prototype.hasOwnProperty.call(this.keywords, method)) {
195
207
  return;
196
208
  }
197
- var keywordSpec = this.keywords[method];
209
+ // Enough arguments?
210
+ const keywordSpec = this.keywords[method];
198
211
  if (keywordSpec.totalArgs &&
199
212
  path.node.arguments.length !== keywordSpec.totalArgs) {
200
213
  return;
@@ -209,27 +222,33 @@ var Parser = (function () {
209
222
  keywordSpec.context > path.node.arguments.length) {
210
223
  return;
211
224
  }
212
- var msgid = this.extractArgument(path.node.arguments[keywordSpec.singular - 1]);
225
+ const msgid = this.extractArgument(path.node.arguments[keywordSpec.singular - 1]);
213
226
  if (msgid === null) {
214
227
  return;
215
228
  }
216
- var msgidPlural;
229
+ let msgidPlural;
217
230
  if (keywordSpec.plural) {
218
231
  msgidPlural = this.extractArgument(path.node.arguments[keywordSpec.plural - 1]);
219
232
  if (msgidPlural === null) {
220
233
  return;
221
234
  }
222
235
  }
223
- var msgctxt;
236
+ let msgctxt;
224
237
  if (keywordSpec.context) {
225
238
  msgctxt = this.extractArgument(path.node.arguments[keywordSpec.context - 1]);
226
239
  if (msgctxt === null) {
227
240
  return;
228
241
  }
229
242
  }
230
- this.addEntry({ msgid: msgid, loc: path.node.loc, method: method, msgidPlural: msgidPlural, msgctxt: msgctxt });
231
- };
232
- Parser.prototype.extractArgument = function (argument) {
243
+ this.addEntry({
244
+ msgid: msgid,
245
+ loc: path.node.loc,
246
+ method,
247
+ msgidPlural,
248
+ msgctxt,
249
+ });
250
+ }
251
+ extractArgument(argument) {
233
252
  if (t.isStringLiteral(argument)) {
234
253
  return argument.value;
235
254
  }
@@ -240,19 +259,19 @@ var Parser = (function () {
240
259
  return this.extractTemplateLiteral(argument);
241
260
  }
242
261
  return null;
243
- };
244
- Parser.prototype.extractBinaryExpression = function (exp) {
245
- var left = this.extractArgument(exp.left);
262
+ }
263
+ extractBinaryExpression(exp) {
264
+ const left = this.extractArgument(exp.left);
246
265
  if (left === null) {
247
266
  return null;
248
267
  }
249
- var right = this.extractArgument(exp.right);
268
+ const right = this.extractArgument(exp.right);
250
269
  if (right === null) {
251
270
  return null;
252
271
  }
253
272
  return left + right;
254
- };
255
- Parser.prototype.extractTemplateLiteral = function (literal) {
273
+ }
274
+ extractTemplateLiteral(literal) {
256
275
  if (literal.expressions.length === 0 &&
257
276
  literal.quasis.length === 1 &&
258
277
  t.isTemplateElement(literal.quasis[0])) {
@@ -262,39 +281,40 @@ var Parser = (function () {
262
281
  ' allowed as arguments to gettext functions because they' +
263
282
  ' are not constant'), literal.loc);
264
283
  return null;
265
- };
266
- Parser.prototype.concatStrings = function (path) {
284
+ }
285
+ concatStrings(path) {
267
286
  if (!t.isBinaryExpression(path.parent)) {
268
287
  return;
269
288
  }
270
- var parentPath = path.parentPath;
289
+ const parentPath = path.parentPath;
271
290
  if (parentPath.node.operator === '+' &&
272
291
  t.isStringLiteral(parentPath.node.left) &&
273
292
  t.isStringLiteral(parentPath.node.right)) {
274
- parentPath.replaceWithSourceString("\"".concat(parentPath.node.left.value).concat(parentPath.node.right.value, "\""));
293
+ const node = t.stringLiteral(parentPath.node.left.value + parentPath.node.right.value);
294
+ node.loc = parentPath.node.loc;
295
+ parentPath.replaceInline(node);
275
296
  }
276
- };
277
- Parser.prototype.addEntry = function (props) {
297
+ }
298
+ addEntry(props) {
278
299
  props.flag = new Array();
279
- console.log(props);
280
- var dict = props.loc;
281
- var references = [];
282
- if (dict != null) {
283
- var filename = '-' === dict.filename ? '[stdin]' : dict.filename;
284
- references = ["".concat(filename, ":").concat(props.loc.start.line)];
285
- }
286
- var commentBlocks = this.findPrecedingComments(props.loc);
287
- var extractedComments = commentBlocks.map(function (block) { return block.value.trim(); });
300
+ const dict = props.loc;
301
+ let references = [];
302
+ if (dict !== null && typeof dict !== 'undefined') {
303
+ const filename = '-' === dict.filename ? '[stdin]' : dict.filename;
304
+ references = [`${filename}:${props.loc.start.line}`];
305
+ }
306
+ const commentBlocks = this.findPrecedingComments(props.loc);
307
+ let extractedComments = commentBlocks.map(block => block.value.trim());
288
308
  if (typeof props.method !== 'undefined') {
289
309
  extractedComments = extractedComments
290
- .map(function (comment) {
291
- var match = /xgettext:(.*)/.exec(comment);
310
+ .map(comment => {
311
+ const match = /xgettext:(.*)/.exec(comment);
292
312
  if (match) {
293
- var str = match[1];
294
- var validTokens_1 = ['fuzzy', 'wrap', 'no-wrap'];
295
- var tokens = str.split(/[ \x09-\x0d]+/);
296
- tokens.forEach(function (token) {
297
- if (validTokens_1.includes(token) ||
313
+ const str = match[1];
314
+ const validTokens = ['fuzzy', 'wrap', 'no-wrap'];
315
+ const tokens = str.split(/[ \x09-\x0d]+/);
316
+ tokens.forEach(token => {
317
+ if (validTokens.includes(token) ||
298
318
  /^(?:[a-z]+-)+(?:format|check)$/.exec(token)) {
299
319
  props.flag.push(token);
300
320
  }
@@ -303,7 +323,7 @@ var Parser = (function () {
303
323
  }
304
324
  return comment;
305
325
  })
306
- .filter(function (comment) { return comment !== ''; });
326
+ .filter(comment => comment !== '');
307
327
  }
308
328
  if (!props.flag.includes('no-perl-brace-format') &&
309
329
  /\{[_a-zA-Z][_a-zA-Z0-9]*\}/.exec(props.msgid)) {
@@ -314,12 +334,11 @@ var Parser = (function () {
314
334
  msgidPlural: props.msgidPlural,
315
335
  msgctxt: props.msgctxt,
316
336
  flags: props.flag,
317
- references: references,
318
- extractedComments: extractedComments,
337
+ references,
338
+ extractedComments,
319
339
  }));
320
- };
321
- Parser.prototype.methodFromMemberExpression = function (me, instance) {
322
- if (instance === void 0) { instance = []; }
340
+ }
341
+ methodFromMemberExpression(me, instance = []) {
323
342
  if (t.isIdentifier(me.object)) {
324
343
  if (t.isLiteral(me.property) && me.computed) {
325
344
  instance.push(this.literalValue(me.property));
@@ -338,20 +357,22 @@ var Parser = (function () {
338
357
  else if (t.isMemberExpression(me.object) &&
339
358
  t.isIdentifier(me.property) &&
340
359
  !me.computed) {
360
+ // Recurse.
341
361
  instance.push(me.property.name);
342
362
  return this.methodFromMemberExpression(me.object, instance);
343
363
  }
344
364
  else if (t.isMemberExpression(me.object) &&
345
365
  t.isLiteral(me.property) &&
346
366
  me.computed) {
367
+ // Recurse.
347
368
  instance.push(this.literalValue(me.property));
348
369
  return this.methodFromMemberExpression(me.object, instance);
349
370
  }
350
371
  else {
351
372
  return null;
352
373
  }
353
- };
354
- Parser.prototype.literalValue = function (node) {
374
+ }
375
+ literalValue(node) {
355
376
  if (t.isStringLiteral(node)) {
356
377
  return node.value;
357
378
  }
@@ -364,11 +385,13 @@ var Parser = (function () {
364
385
  else {
365
386
  return null;
366
387
  }
367
- };
368
- Parser.prototype.findPrecedingComments = function (loc) {
369
- var last;
388
+ }
389
+ findPrecedingComments(loc) {
390
+ let last;
391
+ // Find the last relevant comment, which is the first one that
392
+ // immediately precedes the location.
370
393
  for (last = 0; last < this.comments.length; ++last) {
371
- var commentLocation = this.comments[last].loc;
394
+ const commentLocation = this.comments[last].loc;
372
395
  if (commentLocation.end.line === loc.start.line ||
373
396
  commentLocation.end.line === loc.start.line - 1) {
374
397
  break;
@@ -382,35 +405,37 @@ var Parser = (function () {
382
405
  this.comments.splice(0, this.comments.length);
383
406
  return [];
384
407
  }
385
- var ptr = this.comments[last].loc;
386
- var preceding = this.comments.splice(0, last + 1);
408
+ // Now go back and find all adjacent comments.
409
+ let ptr = this.comments[last].loc;
410
+ const preceding = this.comments.splice(0, last + 1);
387
411
  if (!last) {
388
412
  return preceding;
389
413
  }
390
- var first;
414
+ let first;
391
415
  for (first = preceding.length - 2; first >= 0; --first) {
392
- var commentLocation = preceding[first].loc;
393
- if (commentLocation.end.line < ptr.start.line - 1) {
416
+ const commentLocation = preceding[first].loc;
417
+ if (commentLocation &&
418
+ commentLocation.end.line < ptr.start.line - 1) {
394
419
  break;
395
420
  }
396
421
  ptr = commentLocation;
397
422
  }
398
423
  return preceding.slice(first + 1);
399
- };
400
- Parser.prototype.filterComments = function (comments) {
424
+ }
425
+ filterComments(comments) {
401
426
  if (this.options.addAllComments) {
402
427
  return comments;
403
428
  }
404
- var markers = this.options.addComments
429
+ const markers = this.options.addComments
405
430
  ? this.options.addComments
406
431
  : new Array();
407
- return comments.filter(function (block) {
432
+ return comments.filter(block => {
408
433
  if (block.value.includes('xgettext:')) {
409
434
  return true;
410
435
  }
411
436
  else {
412
- for (var i = 0; i < markers.length; ++i) {
413
- var marker = markers[i].trim();
437
+ for (let i = 0; i < markers.length; ++i) {
438
+ const marker = markers[i].trim();
414
439
  if (marker === block.value.trim().substr(0, marker.length)) {
415
440
  return true;
416
441
  }
@@ -418,11 +443,11 @@ var Parser = (function () {
418
443
  }
419
444
  return false;
420
445
  });
421
- };
422
- Parser.cookedDefaultKeywords = function () {
423
- return Parser.defaultKeywords().map(function (keyword) { return keyword_1.Keyword.from(keyword); });
424
- };
425
- Parser.defaultKeywords = function () {
446
+ }
447
+ static cookedDefaultKeywords() {
448
+ return Parser.defaultKeywords().map(keyword => keyword_1.Keyword.from(keyword));
449
+ }
450
+ static defaultKeywords() {
426
451
  return [
427
452
  '_',
428
453
  '_x',
@@ -449,27 +474,28 @@ var Parser = (function () {
449
474
  'N_lp:2c,3',
450
475
  'N_lpx:2c,3',
451
476
  ];
452
- };
453
- Parser.prototype.warn = function (msg, loc) {
454
- var start = "".concat(loc.start.line, ":").concat(loc.start.column);
455
- var end = loc.end ? "-".concat(loc.end.line, ":").concat(loc.end.column) : '';
456
- var filename = '-' === this.filename ? gtx._('[standard input]') : this.filename;
457
- var location = "".concat(filename, ":").concat(start).concat(end);
458
- console.warn(gtx._x('{location}: warning: {msg}', { location: location, msg: msg }));
459
- };
460
- Parser.prototype.error = function (msg, loc) {
477
+ }
478
+ warn(msg, loc) {
479
+ const start = `${loc.start.line}:${loc.start.column}`;
480
+ const end = loc.end.line > 0 ? `-${loc.end.line}:${loc.end.column}` : '';
481
+ const filename = '-' === this.filename ? gtx._('[standard input]') : this.filename;
482
+ const location = `${filename}:${start}${end}`;
483
+ console.warn(gtx._x('{location}: warning: {msg}', { location, msg }));
484
+ }
485
+ error(msg, loc) {
461
486
  ++this.errors;
462
- var start = "".concat(loc.start.line, ":").concat(loc.start.column);
463
- var end = loc.end ? "-".concat(loc.end.line, ":").concat(loc.end.column) : '';
464
- var filename = '-' === this.filename ? gtx._('[standard input]') : this.filename;
465
- var location = "".concat(filename, ":").concat(start).concat(end);
466
- console.error(gtx._x('{location}: error: {msg}', { location: location, msg: msg }));
467
- };
468
- Parser.prototype.canonicalizeEncoding = function (encoding) {
469
- var canonical = ('' + encoding)
487
+ const start = `${loc.start.line}:${loc.start.column}`;
488
+ const end = loc.end && loc.end.line > 0 ? `-${loc.end.line}:${loc.end.column}` : '';
489
+ const filename = '-' === this.filename ? gtx._('[standard input]') : this.filename;
490
+ const location = `${filename}:${start}${end}`;
491
+ console.error(gtx._x('{location}: Error: {msg}', { location, msg }));
492
+ }
493
+ canonicalizeEncoding(encoding) {
494
+ // This is taken from iconv-lite.
495
+ const canonical = ('' + encoding)
470
496
  .toLowerCase()
471
497
  .replace(/:[0-9]{4}$|[^0-9a-z]/g, '');
472
- var asciiAliases = [
498
+ const asciiAliases = [
473
499
  'ascii8bit',
474
500
  'usascii',
475
501
  'ansix34',
@@ -489,18 +515,22 @@ var Parser = (function () {
489
515
  else {
490
516
  return canonical;
491
517
  }
492
- };
493
- Parser.prototype.findNonUtf8 = function (buf) {
494
- var i = 0;
495
- var loc = {
518
+ }
519
+ findNonUtf8(buf) {
520
+ let i = 0;
521
+ const loc = {
496
522
  start: {
497
523
  line: 1,
498
524
  column: 1,
499
- index: 0
525
+ index: 0,
526
+ },
527
+ end: {
528
+ line: 0,
529
+ column: 0,
530
+ index: 0,
500
531
  },
501
- end: null,
502
532
  filename: '',
503
- identifierName: ''
533
+ identifierName: '',
504
534
  };
505
535
  while (i < buf.length) {
506
536
  if (buf[i] === 0x09 ||
@@ -575,8 +605,6 @@ var Parser = (function () {
575
605
  return loc;
576
606
  }
577
607
  return null;
578
- };
579
- return Parser;
580
- }());
608
+ }
609
+ }
581
610
  exports.Parser = Parser;
582
- //# sourceMappingURL=parser.js.map