Skip to content

Commit bf12b8b

Browse files
committed
print original source when failed to parse arg
1 parent 92273a0 commit bf12b8b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

index.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import wp = require("webpack");
22
import fs = require("fs");
33
import path = require("path");
44
import util = require('util');
5+
import readline = require('readline');
56
import _ = require("lodash");
67
import i18next = require('i18next');
78
import Backend = require('i18next-node-fs-backend');
9+
import { ReadableStreamBuffer } from 'stream-buffers';
810
import { SourceMapConsumer } from 'source-map';
911
const VirtualModulePlugin = require('virtual-module-webpack-plugin');
1012

@@ -26,7 +28,7 @@ async function exists(path: fs.PathLike) {
2628
}
2729
}
2830

29-
function extractArgs(arg: any, warning?: (msg: string) => void) {
31+
function extractArgs(arg: any, warning?: (node: any) => void) {
3032
switch (arg.type) {
3133
case 'Literal':
3234
return arg.value;
@@ -40,7 +42,7 @@ function extractArgs(arg: any, warning?: (msg: string) => void) {
4042
return res;
4143
default:
4244
if (warning) {
43-
warning(`unable to parse arg ${arg}`);
45+
warning(arg);
4446
}
4547
return null;
4648
}
@@ -279,12 +281,45 @@ export default class I18nextPlugin {
279281
}
280282

281283
protected static onTranslateFunctionCall(this: wp.Parser, plugin: I18nextPlugin, expr: wp.Expression) {
282-
const args = expr.arguments.map((arg: any) => extractArgs(arg, plugin.warningOnCompilation.bind(plugin)));
283284
const resource = this.state.current.resource;
284285
if (plugin.sourceMaps[resource] === undefined) {
285286
plugin.sourceMaps[resource] = new SourceMapConsumer(this.state.current._source._sourceMap);
286287
}
287288
const sourceMap = plugin.sourceMaps[resource];
289+
const args = expr.arguments.map((arg: any) => extractArgs(arg, (arg) => {
290+
const beginPos = sourceMap.originalPositionFor(arg.loc.start);
291+
const endPos = sourceMap.originalPositionFor(arg.loc.end);
292+
if (beginPos.source !== null) {
293+
const originalSource = sourceMap.sourceContentFor(beginPos.source);
294+
const sourceLines: string[] = [];
295+
if (originalSource !== null) {
296+
const buffer = new ReadableStreamBuffer();
297+
buffer.put(originalSource);
298+
let lineIdx = 0;
299+
const lineInterface = readline.createInterface(buffer).on("line", (line: string) => {
300+
lineIdx++;
301+
let beginCol = 0, endCol = line.length;
302+
if (lineIdx === beginPos.line) {
303+
beginCol = beginPos.column as number;
304+
}
305+
if (lineIdx === endPos.line) {
306+
endCol = endPos.column as number;
307+
}
308+
if (lineIdx >= (beginPos.line as number) && lineIdx <= (endPos.line as number)) {
309+
sourceLines.push(line.substring(beginCol, endCol));
310+
}
311+
312+
if (lineIdx === endPos.line) {
313+
lineInterface.close();
314+
}
315+
}).on("close", () => {
316+
plugin.warningOnCompilation(`unable to parse arg ${sourceLines.join("\n")} at ${resource}:(${beginPos.line}, ${beginPos.column})`);
317+
});
318+
return;
319+
}
320+
}
321+
plugin.warningOnCompilation(`unable to parse node at ${resource}:(${beginPos.line}, ${beginPos.column})`);
322+
}));
288323
const startPos = sourceMap.originalPositionFor(expr.loc.start);
289324
const pos = [resource, startPos.line, startPos.column];
290325

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@types/lodash": "^4.14.74",
3030
"@types/node": "^8.0.26",
3131
"@types/source-map": "^0.5.1",
32+
"@types/stream-buffers": "^3.0.2",
3233
"@types/webpack": "^3.0.10",
3334
"typescript": "^2.4.2"
3435
},
@@ -37,6 +38,7 @@
3738
"i18next-node-fs-backend": "^1.0.0",
3839
"lodash": "^4.17.4",
3940
"source-map": "^0.5.7",
41+
"stream-buffers": "^3.0.1",
4042
"virtual-module-webpack-plugin": "^0.3.0"
4143
}
4244
}

0 commit comments

Comments
 (0)