@@ -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 ( / e x p e c t e d a n i d e n t i f i e r / i. test ( reason ) ) {
42+ return `${ prefix } a value or variable name is missing before the symbol here.` ;
43+ }
44+ if ( / m i s s i n g s e m i c o l o n / i. test ( reason ) ) {
45+ return `${ prefix } a ';' might be missing at the end of this statement.` ;
46+ }
47+ if ( / u n c l o s e d ( s t r i n g | r e g u l a r e x p r e s s i o n ) / i. test ( reason ) ) {
48+ return `${ prefix } a string or regular expression is not closed with a matching quote or delimiter.` ;
49+ }
50+ if ( / m i s s i n g ' \) ' / i. test ( reason ) ) {
51+ return `${ prefix } a closing ')' is missing. check for unbalanced parentheses.` ;
52+ }
53+ if ( / m i s s i n g ' \} ' / i. test ( reason ) ) {
54+ return `${ prefix } a closing '}' is missing. check for unbalanced braces.` ;
55+ }
56+ if ( / u n m a t c h e d ' / i. test ( reason ) ) {
57+ return `${ prefix } a bracket, brace, or parenthesis on this line does not have a matching partner.` ;
58+ }
59+ if ( / u n e x p e c t e d e a r l y e n d / i. test ( reason ) ) {
60+ return `${ prefix } the code ended while a block was still open. a '}' or ')' may be missing.` ;
61+ }
62+ if (
63+ / u n e x p e c t e d ' ? ; ' ? / i. test ( reason ) ||
64+ / u n e x p e c t e d t o k e n ' ; ' / i. test ( reason )
65+ ) {
66+ return `${ prefix } there is an extra or misplaced ';' on this line.` ;
67+ }
68+ if (
69+ / u s e o f c o n s t b e f o r e i t w a s d e f i n e d | ' [ a - z _ $ ] [ \w $ ] * ' w a s u s e d b e f o r e i t w a s d e f i n e d / 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+
3878if ( 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