Skip to content

Commit 6bbb338

Browse files
committed
render jshint errors inline with dynamic friendly hints
1 parent 3076ec8 commit 6bbb338

1 file changed

Lines changed: 74 additions & 20 deletions

File tree

client/utils/previewEntry.js

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,87 @@ setInterval(() => {
3535
}
3636
}, LOGWAIT);
3737

38+
function friendlyHintForJshint(err) {
39+
const prefix = `[${err.file}, line ${err.line}]`;
40+
const reason = err.reason || '';
41+
if (/expected an identifier/i.test(reason)) {
42+
return `${prefix} a value or variable name is missing before the symbol here.`;
43+
}
44+
if (/missing semicolon/i.test(reason)) {
45+
return `${prefix} a ';' might be missing at the end of this statement.`;
46+
}
47+
if (/unclosed (string|regular expression)/i.test(reason)) {
48+
return `${prefix} a string or regular expression is not closed with a matching quote or delimiter.`;
49+
}
50+
if (/missing '\)'/i.test(reason)) {
51+
return `${prefix} a closing ')' is missing. check for unbalanced parentheses.`;
52+
}
53+
if (/missing '\}'/i.test(reason)) {
54+
return `${prefix} a closing '}' is missing. check for unbalanced braces.`;
55+
}
56+
if (/unmatched '/i.test(reason)) {
57+
return `${prefix} a bracket, brace, or parenthesis on this line does not have a matching partner.`;
58+
}
59+
if (/unexpected early end/i.test(reason)) {
60+
return `${prefix} the code ended while a block was still open. a '}' or ')' may be missing.`;
61+
}
62+
if (
63+
/unexpected '?;'?/i.test(reason) ||
64+
/unexpected token ';'/i.test(reason)
65+
) {
66+
return `${prefix} there is an extra or misplaced ';' on this line.`;
67+
}
68+
if (
69+
/use of const before it was defined|'[a-z_$][\w$]*' was used before it was defined/i.test(
70+
reason
71+
)
72+
) {
73+
return `${prefix} a name is being used before it is declared.`;
74+
}
75+
return `${prefix} ${reason}`;
76+
}
77+
3878
if (Array.isArray(window.__jshintErrors) && window.__jshintErrors.length > 0) {
39-
const errorLogs = window.__jshintErrors.map((err) => {
79+
const messagesBatch = [];
80+
window.__jshintErrors.forEach((err) => {
4081
const location = `${err.file}:${err.line}:${err.character}`;
41-
const data = `SyntaxError: ${err.reason}\n at ${location}`;
42-
const log = {
43-
method: 'error',
44-
data: [data],
45-
id: `${Date.now()}-${err.file}-${err.line}-${err.character}`,
46-
meta: {
47-
name: 'SyntaxError',
48-
message: err.reason,
49-
stack: [
50-
{
51-
fileName: err.file,
52-
functionName: null,
53-
lineNumber: err.line,
54-
columnNumber: err.character
82+
const data = `SyntaxError: ${err.reason} at ${location}`;
83+
const id = `${Date.now()}-${err.file}-${err.line}-${err.character}`;
84+
messagesBatch.push({
85+
log: [
86+
{
87+
method: 'error',
88+
data: [data],
89+
id,
90+
meta: {
91+
name: 'SyntaxError',
92+
message: err.reason,
93+
stack: [
94+
{
95+
fileName: err.file,
96+
functionName: null,
97+
lineNumber: err.line,
98+
columnNumber: err.character
99+
}
100+
]
55101
}
56-
]
57-
}
58-
};
59-
return { log: [log] };
102+
}
103+
]
104+
});
105+
messagesBatch.push({
106+
log: [
107+
{
108+
method: 'log',
109+
data: [`🌸 p5.js says: ${friendlyHintForJshint(err)}`],
110+
id: `${id}-hint`
111+
}
112+
]
113+
});
60114
});
61115
editor.postMessage(
62116
{
63117
source: 'sketch',
64-
messages: errorLogs
118+
messages: messagesBatch
65119
},
66120
editorOrigin
67121
);

0 commit comments

Comments
 (0)