Skip to content

Commit 7892a29

Browse files
mizdraai
andauthored
Fix missing offset in Node#positionInside, Node#positionBy and Node#rangeBy (#2033)
* Fix `opts` argument cannot be omitted * Assign default values to `opts` just in case - The `opts` argument to `Node#error` is currently omissible, but we'll assign a default value just in case. * Add test cases * Add test cases for `Node#rangeBy({start, end})` * Add test cases for offset * Fix missing offset in `Node#positionInside`, `Node#positionBy` and `Node#rangeBy` * Fix conflict resolution mistakes --------- Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>
1 parent 875f1fb commit 7892a29

File tree

3 files changed

+226
-82
lines changed

3 files changed

+226
-82
lines changed

lib/node.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ declare abstract class Node_ {
424424
* @return Range.
425425
*/
426426
rangeBy(
427-
opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
427+
opts?: Pick<WarningOptions, 'end' | 'endIndex' | 'index' | 'start' | 'word'>
428428
): Node.Range
429429

430430
/**

lib/node.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Node {
244244
}
245245
}
246246

247-
return { column, line }
247+
return { column, line, offset: end }
248248
}
249249

250250
prev() {
@@ -254,24 +254,33 @@ class Node {
254254
}
255255

256256
rangeBy(opts = {}) {
257+
let inputString = ('document' in this.source.input)
258+
? this.source.input.document
259+
: this.source.input.css
257260
let start = {
258261
column: this.source.start.column,
259-
line: this.source.start.line
262+
line: this.source.start.line,
263+
offset: sourceOffset(inputString, this.source.start)
260264
}
261265
let end = this.source.end
262266
? {
263267
column: this.source.end.column + 1,
264-
line: this.source.end.line
268+
line: this.source.end.line,
269+
offset: typeof this.source.end.offset === 'number'
270+
// `source.end.offset` is exclusive, so we don't need to add 1
271+
? this.source.end.offset
272+
// Since line/column in this.source.end is inclusive,
273+
// the `sourceOffset(... , this.source.end)` returns an inclusive offset.
274+
// So, we add 1 to convert it to exclusive.
275+
: sourceOffset(inputString, this.source.end) + 1
265276
}
266277
: {
267278
column: start.column + 1,
268-
line: start.line
279+
line: start.line,
280+
offset: start.offset + 1
269281
}
270282

271283
if (opts.word) {
272-
let inputString = ('document' in this.source.input)
273-
? this.source.input.document
274-
: this.source.input.css
275284
let stringRepresentation = inputString.slice(
276285
sourceOffset(inputString, this.source.start),
277286
sourceOffset(inputString, this.source.end)
@@ -287,7 +296,8 @@ class Node {
287296
if (opts.start) {
288297
start = {
289298
column: opts.start.column,
290-
line: opts.start.line
299+
line: opts.start.line,
300+
offset: sourceOffset(inputString, opts.start)
291301
}
292302
} else if (opts.index) {
293303
start = this.positionInside(opts.index)
@@ -296,7 +306,8 @@ class Node {
296306
if (opts.end) {
297307
end = {
298308
column: opts.end.column,
299-
line: opts.end.line
309+
line: opts.end.line,
310+
offset: sourceOffset(inputString, opts.end)
300311
}
301312
} else if (typeof opts.endIndex === 'number') {
302313
end = this.positionInside(opts.endIndex)
@@ -309,7 +320,11 @@ class Node {
309320
end.line < start.line ||
310321
(end.line === start.line && end.column <= start.column)
311322
) {
312-
end = { column: start.column + 1, line: start.line }
323+
end = {
324+
column: start.column + 1,
325+
line: start.line,
326+
offset: start.offset + 1
327+
}
313328
}
314329

315330
return { end, start }

0 commit comments

Comments
 (0)