1- import * as angular from '@angular/compiler' ;
1+ import { type AST } from '@angular/compiler' ;
22import type * as babel from '@babel/types' ;
33
4- import type { LocationInformation , NGNode , RawNGSpan } from './types.ts' ;
4+ import type { LocationInformation , NGNode , Range , StartEnd } from './types.ts' ;
55import { getCharacterIndex , sourceSpanToLocationInformation } from './utils.ts' ;
66
7+ export type RawLocationInformation = AST | StartEnd | Range ;
8+
79export class Source {
810 text ;
911
@@ -15,38 +17,51 @@ export class Source {
1517 return getCharacterIndex ( this . text , pattern , index ) ;
1618 }
1719
18- transformSpan ( span : RawNGSpan ) : LocationInformation {
20+ transformSpan ( span : StartEnd ) : LocationInformation {
1921 return sourceSpanToLocationInformation ( span ) ;
2022 }
2123
2224 createNode < T extends NGNode > (
2325 properties : Partial < T > & { type : T [ 'type' ] } ,
24- location : angular . AST | RawNGSpan | [ number , number ] ,
26+ location ?: RawLocationInformation ,
2527 ) {
26- let start : number ;
27- let end : number ;
28- let range : [ number , number ] ;
29- if ( Array . isArray ( location ) ) {
30- range = location ;
31- [ start , end ] = location ;
32- } else {
33- ( { start, end } =
34- location instanceof angular . AST ? location . sourceSpan : location ) ;
28+ let start : number | undefined | null = properties . start ;
29+ let end : number | undefined | null = properties . end ;
30+ let range : Range | undefined = properties . range ;
31+
32+ if ( location ) {
33+ if ( Array . isArray ( location ) ) {
34+ [ start , end ] = location ;
35+ range = location ;
36+ } else {
37+ ( { start, end } = ( location as AST ) . sourceSpan ?? location ) ;
38+ range = [ start , end ] ;
39+ }
40+ }
41+
42+ if ( range ) {
43+ [ start , end ] = range ;
44+ } else if ( typeof start === 'number' && typeof end === 'number' ) {
3545 range = [ start , end ] ;
3646 }
3747
48+ /* c8 ignore next 3 @preserve */
49+ if ( ! ( typeof start === 'number' && typeof end === 'number' && range ) ) {
50+ throw new Error ( 'Missing location information' ) ;
51+ }
52+
3853 const node = {
54+ ...properties ,
3955 start,
4056 end,
4157 range,
42- ...properties ,
4358 } as T & LocationInformation ;
4459
4560 switch ( node . type ) {
4661 case 'NumericLiteral' :
4762 case 'StringLiteral' :
4863 case 'RegExpLiteral' : {
49- const raw = this . text . slice ( node . start , node . end ) ;
64+ const raw = this . text . slice ( start , end ) ;
5065 const { value } = node as unknown as
5166 | babel . NumericLiteral
5267 | babel . StringLiteral ;
0 commit comments