@@ -31,7 +31,7 @@ class Location {
31
31
readonly #indices: Record < Offset , Readonly < Point > > & Record < string , Offset >
32
32
33
33
/**
34
- * Point before first character in source file.
34
+ * Point before first character in file.
35
35
*
36
36
* @see {@linkcode Point }
37
37
*
@@ -53,34 +53,39 @@ class Location {
53
53
* @see {@linkcode VFile }
54
54
* @see {@linkcode Value }
55
55
*
56
- * @param {Value | VFile } file - File to index
57
- * @param {( Point | null)? } [start] - Point before first character in `file`
56
+ * @param {Value | VFile | null | undefined } [ file] - File to index
57
+ * @param {Point | null | undefined } [start] - Point before first character
58
58
*/
59
- constructor ( file : Value | VFile , start ?: Point | null ) {
59
+ constructor (
60
+ file ?: Value | VFile | null | undefined ,
61
+ start ?: Point | null | undefined
62
+ ) {
60
63
this . #indices = { }
61
64
this . start = Object . assign ( { } , start ?? { column : 1 , line : 1 , offset : 0 } )
62
65
this . start = Object . freeze ( this . start )
63
66
64
- /**
65
- * Iteration point.
66
- *
67
- * @const {Point} point
68
- */
69
- const point : Point = { ...this . start }
70
-
71
67
// index file
72
- for ( const char of String ( file ) + '\n' ) {
73
- this . #indices[ point . offset ] = { ...point }
74
- this . #indices[ `${ point . line } :${ point . column } ` ] = point . offset
68
+ if ( file !== null && file !== undefined ) {
69
+ /**
70
+ * Iteration point.
71
+ *
72
+ * @const {Point} point
73
+ */
74
+ const point : Point = { ...this . start }
75
+
76
+ for ( const char of String ( file ) + '\n' ) {
77
+ this . #indices[ point . offset ] = { ...point }
78
+ this . #indices[ `${ point . line } :${ point . column } ` ] = point . offset
75
79
76
- // advance point
77
- if ( / [ \n \r ] / . test ( char ) ) {
78
- point . column = 1
79
- point . line ++
80
- point . offset ++
81
- } else {
82
- point . column ++
83
- point . offset ++
80
+ // advance point
81
+ if ( / [ \n \r ] / . test ( char ) ) {
82
+ point . column = 1
83
+ point . line ++
84
+ point . offset ++
85
+ } else {
86
+ point . column ++
87
+ point . offset ++
88
+ }
84
89
}
85
90
}
86
91
}
@@ -97,10 +102,10 @@ class Location {
97
102
* @public
98
103
* @instance
99
104
*
100
- * @param {( unist.Point | null)? } [point] - Place in source file
101
- * @return {Offset } Index of character in source file or `-1`
105
+ * @param {unist.Point | null | undefined } [point] - Place in file
106
+ * @return {Offset } Index of character in file or `-1`
102
107
*/
103
- public offset ( point ?: unist . Point | null ) : Offset {
108
+ public offset ( point ?: unist . Point | null | undefined ) : Offset {
104
109
return this . #indices[ `${ point ?. line } :${ point ?. column } ` ] ?? - 1
105
110
}
106
111
@@ -116,10 +121,10 @@ class Location {
116
121
* @public
117
122
* @instance
118
123
*
119
- * @param {( Offset | null)? } [offset] - Index of character in source file
120
- * @return {Point } Place in source file
124
+ * @param {Offset | null | undefined } [offset] - Index of character in file
125
+ * @return {Point } Place in file
121
126
*/
122
- public point ( offset ?: Offset | null ) : Point {
127
+ public point ( offset ?: Offset | null | undefined ) : Point {
123
128
return this . #indices[ offset ?? Number . NaN ] ?? {
124
129
column : - 1 ,
125
130
line : - 1 ,
0 commit comments