File tree 4 files changed +27
-12
lines changed
4 files changed +27
-12
lines changed Original file line number Diff line number Diff line change @@ -96,18 +96,22 @@ export function fromJs(value, options) {
96
96
onComment : comments
97
97
} )
98
98
} catch ( error ) {
99
- const exception = /** @type {AcornError } */ ( error )
100
- exception . message = exception . message . replace ( / \( ( \d + ) : ( \d + ) \) $ / , '' )
99
+ const cause = /** @type {AcornError } */ ( error )
101
100
102
- throw new VFileMessage (
103
- exception ,
104
- {
105
- line : exception . loc . line ,
106
- column : exception . loc . column + 1 ,
107
- offset : exception . pos
101
+ const message = new VFileMessage ( 'Could not parse JavaScript with Acorn' , {
102
+ cause ,
103
+ place : {
104
+ line : cause . loc . line ,
105
+ column : cause . loc . column + 1 ,
106
+ offset : cause . pos
108
107
} ,
109
- 'esast-util-from-js:acorn'
110
- )
108
+ ruleId : 'acorn' ,
109
+ source : 'esast-util-from-js'
110
+ } )
111
+
112
+ message . url = 'https://github.com/syntax-tree/esast-util-from-js#throws'
113
+
114
+ throw message
111
115
}
112
116
113
117
tree . comments = comments
Original file line number Diff line number Diff line change 39
39
"@types/estree-jsx" : " ^1.0.0" ,
40
40
"acorn" : " ^8.0.0" ,
41
41
"esast-util-from-estree" : " ^1.0.0" ,
42
- "vfile-message" : " ^3 .0.0"
42
+ "vfile-message" : " ^4 .0.0"
43
43
},
44
44
"devDependencies" : {
45
45
"@types/node" : " ^20.0.0" ,
Original file line number Diff line number Diff line change @@ -135,6 +135,15 @@ Parse JavaScript to an esast.
135
135
136
136
Tree ([ ` Node ` ] [ node ] ).
137
137
138
+ ###### Throws
139
+
140
+ When the JavaScript cannot be parsed with ` acorn ` , a
141
+ [ ` VFileMessage ` ] [ vfile-message ] is thrown.
142
+
143
+ This can for example happen when passing modern syntax (you could maybe use a
144
+ newer ` version ` , or it might be that the syntax is not yet supported), or just
145
+ otherwise invalid JavaScript (you might need a plugin).
146
+
138
147
### ` Options `
139
148
140
149
Configuration (TypeScript type).
@@ -309,6 +318,8 @@ abide by its terms.
309
318
310
319
[estree-util-to-js]: https://github.com/syntax-tree/estree-util-to-js
311
320
321
+ [vfile-message]: https://github.com/vfile/vfile-message
322
+
312
323
[fromjs]: #fromjsvalue-options
313
324
314
325
[options]: #options
Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ test('fromJs', () => {
69
69
function ( ) {
70
70
fromJs ( 'import "a"' )
71
71
} ,
72
- / ' i m p o r t ' a n d ' e x p o r t ' m a y a p p e a r o n l y w i t h ' s o u r c e T y p e : m o d u l e ' / ,
72
+ / C o u l d n o t p a r s e J a v a S c r i p t w i t h A c o r n / ,
73
73
'should fail on an import w/o `module: true`'
74
74
)
75
75
You can’t perform that action at this time.
0 commit comments