Skip to content

Commit d5778d9

Browse files
committed
Fix cannot import module use import {a as b} from 'c';
Show error stack in hover message of result; Rename some variable.
1 parent e9cb022 commit d5778d9

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

src/code.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@ import * as Path from 'path';
22
import * as Fs from 'fs';
33

44

5-
const importStatement = /import\s*(?:(\*\s+as\s)?([\w-_]+),?)?\s*(?:\{([^\}]+)\})?\s+from\s+["']([^"']+)["']/gi;
5+
const importStatement = /import\s+(?:(\*\s+as\s)?([\w-_]+),?)?\s*(?:\{([^\}]+)\})?\s+from\s+["']([^"']+)["']/gi;
66

77
export function rewriteImportToRequire(code: string): string {
88
return code.replace(importStatement, (str, wildcard: string, module: string, modules: string, from: string) => {
99
let rewrite = '';
1010

1111
if (module)
12-
rewrite += `${rewrite === '' ? '' : ', '}default: ${module} `;
12+
rewrite = `default: ${module}`;
1313

1414
if (modules)
15-
rewrite += `${rewrite === '' ? '' : ', '}${modules
16-
.split(',')
17-
.map(r => r.replace(/\s*([\w-_]+)(?:\s+as\s+([\w-_]))?\s*/gi, (str, moduleName: string, moduleNewName: string) => {
18-
return `${moduleNewName ? `${moduleNewName.trim()}: ` : ``}${moduleName.trim()}`;
19-
}))
20-
.join(', ')}`;
21-
22-
return `const ${wildcard ? module : `{ ${rewrite} }`} = require('${from}')`;
15+
rewrite += (rewrite && ', ') + modules.replace(/\sas\s/g, ': ');
16+
17+
return `const ${wildcard ? module : `{ ${rewrite} }`} = require('${from}');`;
2318
});
2419
}
2520

@@ -42,14 +37,14 @@ export function rewriteModulePathInRequire(code: string, basePath: string, fileP
4237
}
4338

4439

45-
const linBreak = /\r?\n/;
40+
const lineBreak = /\r?\n/;
4641
const consoleLogCall = /console\s*\.(log|debug|error)\(/g;
4742

4843
export function rewriteConsoleToAppendLineNumber(code: string): string {
4944
let num = 0,
5045
out = [];
5146

52-
for (let line of code.split(linBreak)) {
47+
for (let line of code.split(lineBreak)) {
5348
out.push(line.replace(consoleLogCall, `global['\`console\`'].$1(${num++}, `));
5449
}
5550

@@ -60,5 +55,5 @@ export function rewriteConsoleToAppendLineNumber(code: string): string {
6055
const lineBreakInChainCall = /([\n\s]+)\./gi;
6156

6257
export function rewriteChainCallInOneLine(code: string): string {
63-
return code.replace(lineBreakInChainCall, (str, whitespace) => `/*\`${whitespace.split(linBreak).length - 1}\`*/.`);
58+
return code.replace(lineBreakInChainCall, (str, whitespace) => `/*\`${whitespace.split(lineBreak).length - 1}\`*/.`);
6459
}

src/decorator.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const colorOfType = {
2323
'Console': '#457abb',
2424
'Error': 'red',
2525
}
26-
// TODO: if Node's Version of VSCode >=9.9, use option `compact`
27-
const inspectOptions = { maxArrayLength: null, depth: null };
26+
// TODO: if Node's Version of VSCode >=9.9, use default option
27+
const inspectOptions: NodeJS.InspectOptions = { depth: 20 };
2828

2929
type Data = { line: number, type: 'Expression' | 'Terminal', value: any };
30-
type Result = { line: number, type: 'Value of Expression' | 'Console' | 'Error', text: string, value: any };
30+
type Result = { line: number, type: 'Value of Expression' | 'Console' | 'Error', text: string, value: string };
3131

3232
export default class Decorator {
3333
private editor: TextEditor;
@@ -63,18 +63,15 @@ export default class Decorator {
6363
renderOptions: { before: { margin: '0 0 0 1em' } },
6464
range: new Range(pos, pos)
6565
};
66-
this.lineToDecorator.set(result.line, decorator);
66+
this.lineToDecorator.set(line, decorator);
6767
this.decorators.push(decorator);
6868
}
6969

7070
decorator.renderOptions.before.color = colorOfType[result.type];
7171
decorator.renderOptions.before.contentText = ` ${result.text}`;
7272

7373
decorator.hoverMessage = new MarkdownString(result.type);
74-
decorator.hoverMessage.appendCodeblock(
75-
result.type === 'Console' ? result.value.join('\n') : result.value || result.text,
76-
'javascript'
77-
);
74+
decorator.hoverMessage.appendCodeblock(result.value, result.type === 'Error' ? 'text' : 'javascript');
7875

7976
this.decorateAll();
8077
}
@@ -87,14 +84,14 @@ export default class Decorator {
8784
case 'object':
8885
if (result.constructor && result.constructor.name === 'Promise' && result.then) {
8986
try {
90-
let value = await Promise.resolve(result);
91-
return value ? this.formatExpressionValue(Object.assign(data, { value })) : null;
87+
data.value = await (<Promise<any>>result);
88+
return data.value ? this.formatExpressionValue(data) : null;
9289
} catch (error) {
9390
return {
9491
line: data.line,
9592
type: 'Error',
9693
text: `${error.name}: ${error.message}`,
97-
value: error,
94+
value: error.stack,
9895
}
9996
}
10097
}
@@ -103,7 +100,7 @@ export default class Decorator {
103100
return {
104101
line: data.line,
105102
type: 'Value of Expression',
106-
text: string,
103+
text: string.replace(/\n/g, ' '),
107104
value: string,
108105
}
109106

@@ -120,22 +117,22 @@ export default class Decorator {
120117
let out = data.value as string;
121118
let match: RegExpExecArray;
122119

123-
if ((match = /(\w*Error:\s.*)(?:\n\s*at\s)?/g.exec(out)) != null) {
124-
this.outputChannel.appendLine(` ${match[1]}\n\tat line ${data.line}`);
120+
if ((match = /^(\w*Error(?: \[[^\]]+\])?:\s.*)(?:\n\s*at\s)?/.exec(out)) != null) {
121+
this.outputChannel.appendLine(` ${out}`);
125122

126-
return { line: data.line, type: 'Error', text: match[1], value: match[1] };
123+
return { line: data.line, type: 'Error', text: match[1], value: out };
127124
}
128-
else if ((match = /^`\{(\d+)\}`([\s\S]*)$/g.exec(out)) != null) {
125+
else if ((match = /^`\{(\d+)\}`([\s\S]*)$/.exec(out)) != null) {
129126
let line = +match[1];
130127
let msg = match[2] || '';
131128

132129
let output = this.lineToOutput.get(line);
133130
if (output == null) {
134-
this.lineToOutput.set(+match[1], output = { line, type: 'Console', text: '', value: [] });
131+
this.lineToOutput.set(line, output = { line, type: 'Console', text: '', value: '' });
135132
}
136133

137-
output.text += (output.text === '' ? '' : ', ') + msg.replace(/\r?\n/g, ' ');
138-
output.value.push(msg);
134+
output.text += (output.text && ', ') + msg.replace(/\r?\n/g, ' ');
135+
output.value += (output.value && '\n') + msg;
139136

140137
this.outputChannel.appendLine(` ${msg}`);
141138
return output;

src/repl-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const server = Repl.start({
3030

3131

3232
const originEval = server.eval; // keep a backup of original eval
33-
const lineNumber = /\/\*`(\d+)`\*\//gi;
33+
const lineNumber = /\/\*`(\d+)`\*\//g;
3434

3535
// nice place to read the result in sequence and inject it in the code
3636
server.eval = (cmd, context, filename, callback) => {

0 commit comments

Comments
 (0)