@@ -69,13 +69,12 @@ export function getErrorOrigin(loadedModules, lineNumber) {
69
69
let currentLine = 0
70
70
71
71
for ( let i = 0 ; i < loadedModules . length ; i ++ ) {
72
- // get module line count
73
- const lineCount = ( loadedModules [ i ] . content . match ( / \r ? \n / g) ?. length || 0 ) + 1
74
-
72
+ // get module line count, add 2 for '\n\n' offset
73
+ const lineCount = ( loadedModules [ i ] . content . match ( / \r ? \n / g) ?. length || 0 ) + 1 + 2
75
74
if ( currentLine + lineCount >= lineNumber ) {
76
75
return {
77
76
file : loadedModules [ i ] . path ,
78
- line : lineNumber - currentLine - i * 2
77
+ line : lineNumber - currentLine - ( i + 1 ) * 2
79
78
}
80
79
}
81
80
@@ -92,18 +91,28 @@ export function getErrorOrigin(loadedModules, lineNumber) {
92
91
* @param {ErrorOrigin|undefined } origin
93
92
*/
94
93
export function outputError ( line , error , origin ) {
95
- const lineNumber = origin ?. line || error . lineNumber
94
+ // Subtract 2 lines as the line does not includes the '\n\n' offset
95
+ const lineNumber = ( origin ?. line || error . lineNumber - 2 )
96
96
const lineNumberPlaceholder = ' ' . repeat ( lineNumber . toString ( ) . length )
97
97
98
- console . log (
99
- '\n' +
100
- chalk . bold ( error . errorMessage ) +
101
- '\n' +
102
- ( origin ? chalk . dim ( ` in ${ origin . file } \n` ) : "" ) +
103
- chalk . blue ( ` ${ lineNumberPlaceholder } |\n ${ lineNumber } | ` ) +
104
- line . split ( '\n' ) [ error . lineNumber - 1 ] +
105
- '\n' +
106
- chalk . blue ( ` ${ lineNumberPlaceholder } |\n` ) +
107
- chalk . dim ( 'This error occurred while aos was evaluating the submitted code.' )
108
- )
98
+ if ( origin ) {
99
+ console . log (
100
+ '\n' +
101
+ chalk . bold ( error . errorMessage ) +
102
+ '\n' +
103
+ ( origin ? chalk . dim ( ` in ${ origin . file } \n` ) : "" ) +
104
+ chalk . blue ( ` ${ lineNumberPlaceholder } |\n ${ lineNumber } | ` ) +
105
+ line . split ( '\n' ) [ lineNumber + 1 ] +
106
+ '\n' +
107
+ chalk . blue ( ` ${ lineNumberPlaceholder } |\n` ) +
108
+ chalk . dim ( 'This error occurred while aos was evaluating the submitted code.' )
109
+ )
110
+ } else {
111
+ console . log (
112
+ '\n' +
113
+ chalk . bold ( `Error on line ${ lineNumber } : ${ error . errorMessage } ` ) +
114
+ '\n' +
115
+ chalk . dim ( 'This error occurred while aos was evaluating the submitted code.' )
116
+ )
117
+ }
109
118
}
0 commit comments