@@ -12,34 +12,36 @@ import {
1212 skipSpaces ,
1313 walk ,
1414} from "../../astro"
15+ import type { Context } from "../../context"
16+ import { ParseError } from "../../errors"
1517
1618/**
1719 * Parse code by `@astrojs/compiler`
1820 */
19- export function parse ( code : string ) : ParseResult {
21+ export function parse ( code : string , ctx : Context ) : ParseResult {
2022 const ast = service . parse ( code , { position : true } ) . ast
21- fixLocations ( ast , code )
23+ fixLocations ( ast , ctx )
2224 return { ast }
2325}
2426
2527/**
2628 * Fix locations
2729 */
28- function fixLocations ( node : ParentNode , code : string ) : void {
30+ function fixLocations ( node : ParentNode , ctx : Context ) : void {
2931 // FIXME: Adjust because the parser does not return the correct location.
3032 let start = 0
3133 walk (
3234 node ,
33- code ,
35+ ctx . code ,
3436 ( node ) => {
3537 if ( node . type === "frontmatter" ) {
3638 start = node . position ! . start . offset = tokenIndex (
37- code ,
39+ ctx ,
3840 "---" ,
3941 start ,
4042 )
4143 start = node . position ! . end ! . offset =
42- tokenIndex ( code , "---" , start + 3 + node . value . length ) + 3
44+ tokenIndex ( ctx , "---" , start + 3 + node . value . length ) + 3
4345 } else if (
4446 node . type === "fragment" ||
4547 node . type === "element" ||
@@ -50,31 +52,31 @@ function fixLocations(node: ParentNode, code: string): void {
5052 node . position = { start : { } , end : { } } as any
5153 }
5254 start = node . position ! . start . offset = tokenIndex (
53- code ,
55+ ctx ,
5456 "<" ,
5557 start ,
5658 )
5759 start += 1
5860 start += node . name . length
5961 if ( ! node . attributes . length ) {
60- start = getStartTagEndOffset ( node , code )
62+ start = getStartTagEndOffset ( node , ctx )
6163 }
6264 } else if ( node . type === "attribute" ) {
63- fixLocationForAttr ( node , code , start )
64- start = getAttributeEndOffset ( node , code )
65+ fixLocationForAttr ( node , ctx , start )
66+ start = getAttributeEndOffset ( node , ctx )
6567 } else if ( node . type === "comment" ) {
66- node . position ! . start . offset = tokenIndex ( code , "<!--" , start )
67- start = getCommentEndOffset ( node , code )
68+ node . position ! . start . offset = tokenIndex ( ctx , "<!--" , start )
69+ start = getCommentEndOffset ( node , ctx )
6870 } else if ( node . type === "text" ) {
6971 start = node . position ! . start . offset = tokenIndex (
70- code ,
72+ ctx ,
7173 node . value ,
7274 start ,
7375 )
7476 start += node . value . length
7577 } else if ( node . type === "expression" ) {
7678 start = node . position ! . start . offset = tokenIndex (
77- code ,
79+ ctx ,
7880 "{" ,
7981 start ,
8082 )
@@ -87,13 +89,13 @@ function fixLocations(node: ParentNode, code: string): void {
8789 node . position ! . end = { } as any
8890 }
8991 start = node . position ! . start . offset = tokenIndex (
90- code ,
92+ ctx ,
9193 "<!" ,
9294 start ,
9395 )
9496 start += 2
9597 start = node . position ! . end ! . offset =
96- code . indexOf ( ">" , start ) + 1
98+ ctx . code . indexOf ( ">" , start ) + 1
9799 } else if ( node . type === "root" ) {
98100 // noop
99101 }
@@ -102,26 +104,26 @@ function fixLocations(node: ParentNode, code: string): void {
102104 if ( node . type === "attribute" ) {
103105 const attributes = ( parent as TagLikeNode ) . attributes
104106 if ( attributes [ attributes . length - 1 ] === node ) {
105- start = getStartTagEndOffset ( parent as TagLikeNode , code )
107+ start = getStartTagEndOffset ( parent as TagLikeNode , ctx )
106108 }
107109 return
108110 }
109111 if ( node . type === "expression" ) {
110- start = tokenIndex ( code , "}" , start ) + 1
112+ start = tokenIndex ( ctx , "}" , start ) + 1
111113 } else if (
112114 node . type === "fragment" ||
113115 node . type === "element" ||
114116 node . type === "component" ||
115117 node . type === "custom-element"
116118 ) {
117119 const closeTagStart = tokenIndexSafe (
118- code ,
120+ ctx . code ,
119121 `</${ node . name } ` ,
120122 start ,
121123 )
122124 if ( closeTagStart != null ) {
123125 start = closeTagStart + 2 + node . name . length
124- start = tokenIndex ( code , ">" , start ) + 1
126+ start = tokenIndex ( ctx , ">" , start ) + 1
125127 }
126128 } else {
127129 return
@@ -136,36 +138,42 @@ function fixLocations(node: ParentNode, code: string): void {
136138/**
137139 * Fix locations
138140 */
139- function fixLocationForAttr ( node : AttributeNode , code : string , start : number ) {
141+ function fixLocationForAttr ( node : AttributeNode , ctx : Context , start : number ) {
140142 if ( node . kind === "empty" ) {
141- node . position ! . start . offset = tokenIndex ( code , node . name , start )
143+ node . position ! . start . offset = tokenIndex ( ctx , node . name , start )
142144 } else if ( node . kind === "quoted" ) {
143- node . position ! . start . offset = tokenIndex ( code , node . name , start )
145+ node . position ! . start . offset = tokenIndex ( ctx , node . name , start )
144146 } else if ( node . kind === "expression" ) {
145- node . position ! . start . offset = tokenIndex ( code , node . name , start )
147+ node . position ! . start . offset = tokenIndex ( ctx , node . name , start )
146148 } else if ( node . kind === "shorthand" ) {
147- node . position ! . start . offset = tokenIndex ( code , "{" , start )
149+ node . position ! . start . offset = tokenIndex ( ctx , "{" , start )
148150 } else if ( node . kind === "spread" ) {
149- node . position ! . start . offset = tokenIndex ( code , "{" , start )
151+ node . position ! . start . offset = tokenIndex ( ctx , "{" , start )
150152 } else if ( node . kind === "template-literal" ) {
151- node . position ! . start . offset = tokenIndex ( code , node . name , start )
153+ node . position ! . start . offset = tokenIndex ( ctx , node . name , start )
152154 } else {
153- throw new Error ( `Unknown attr kind: ${ node . kind } ` )
155+ throw new ParseError (
156+ `Unknown attr kind: ${ node . kind } ` ,
157+ node . position ! . start . offset ,
158+ ctx ,
159+ )
154160 }
155161}
156162
157163/**
158164 * Get token index
159165 */
160- function tokenIndex ( string : string , token : string , position : number ) : number {
161- const index = tokenIndexSafe ( string , token , position )
166+ function tokenIndex ( ctx : Context , token : string , position : number ) : number {
167+ const index = tokenIndexSafe ( ctx . code , token , position )
162168 if ( index == null ) {
163169 const start =
164- token . trim ( ) === token ? skipSpaces ( string , position ) : position
165- throw new Error (
170+ token . trim ( ) === token ? skipSpaces ( ctx . code , position ) : position
171+ throw new ParseError (
166172 `Unknown token at ${ start } , expected: ${ JSON . stringify (
167173 token ,
168- ) } , actual: ${ JSON . stringify ( string . slice ( start , start + 10 ) ) } `,
174+ ) } , actual: ${ JSON . stringify ( ctx . code . slice ( start , start + 10 ) ) } `,
175+ start ,
176+ ctx ,
169177 )
170178 }
171179 return index
0 commit comments