Skip to content

Commit 875f1fb

Browse files
authored
Fix opts argument of Node#rangeBy() and Node#positionBy() cannot be omitted (#2031)
* 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
1 parent 25443a5 commit 875f1fb

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

lib/node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Node {
208208
return this.parent.nodes[index + 1]
209209
}
210210

211-
positionBy(opts) {
211+
positionBy(opts = {}) {
212212
let pos = this.source.start
213213
if (opts.index) {
214214
pos = this.positionInside(opts.index)
@@ -253,7 +253,7 @@ class Node {
253253
return this.parent.nodes[index - 1]
254254
}
255255

256-
rangeBy(opts) {
256+
rangeBy(opts = {}) {
257257
let start = {
258258
column: this.source.start.column,
259259
line: this.source.start.line
@@ -423,7 +423,7 @@ class Node {
423423
return result
424424
}
425425

426-
warn(result, text, opts) {
426+
warn(result, text, opts = {}) {
427427
let data = { node: this }
428428
for (let i in opts) data[i] = opts[i]
429429
return result.warn(text, data)

test/node.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,29 @@ test('positionInside() supports multi-root documents', () => {
453453
equal(a.positionInside(1), { column: 9, line: 1 })
454454
})
455455

456+
test('positionBy() returns position', () => {
457+
let css = parse('a { one: X }')
458+
let a = css.first as Rule
459+
let one = a.first as Declaration
460+
equal(one.positionBy(), { column: 6, line: 1, offset: 5 })
461+
equal(a.positionBy(), { column: 1, line: 1, offset: 0 })
462+
})
463+
464+
test('positionBy() returns position after AST mutations', () => {
465+
let css = parse('a {\n\tone: 1;\n\ttwo: 2;}')
466+
let a = css.first as Rule
467+
let one = a.first as Declaration
468+
let two = one.next() as Declaration
469+
470+
equal(a.positionBy(), { column: 1, line: 1, offset: 0 })
471+
equal(two.positionBy(), { column: 2, line: 3, offset: 14 })
472+
473+
one.remove()
474+
475+
equal(a.positionBy(), { column: 1, line: 1, offset: 0 })
476+
equal(two.positionBy(), { column: 2, line: 3, offset: 14 })
477+
})
478+
456479
test('positionBy() returns position for word', () => {
457480
let css = parse('a { one: X }')
458481
let a = css.first as Rule
@@ -527,6 +550,59 @@ test('positionBy() supports multi-root documents', () => {
527550
equal(a.positionBy({ word: 'a' }), { column: 8, line: 1 })
528551
})
529552

553+
test('rangeBy() returns range', () => {
554+
let css = parse('a { one: X }')
555+
let a = css.first as Rule
556+
let one = a.first as Declaration
557+
equal(one.rangeBy(), {
558+
end: { column: 12, line: 1 },
559+
start: { column: 6, line: 1 }
560+
})
561+
})
562+
563+
test('rangeBy() returns range when offsets are missing', () => {
564+
let css = parse('a { one: X }')
565+
let a = css.first as Rule
566+
let one = a.first as Declaration
567+
568+
// @ts-expect-error Testing non-standard AST
569+
if (one.source?.start) delete one.source.start.offset
570+
// @ts-expect-error Testing non-standard AST
571+
if (one.source?.end) delete one.source.end.offset
572+
573+
equal(one.rangeBy(), {
574+
end: { column: 12, line: 1 },
575+
start: { column: 6, line: 1 }
576+
})
577+
})
578+
579+
test('rangeBy() returns range for empty object even after AST mutations', () => {
580+
let css = parse('a {\n\tone: 1;\n\ttwo: 2;}')
581+
let a = css.first as Rule
582+
let one = a.first as Declaration
583+
let two = one.next() as Declaration
584+
585+
equal(a.rangeBy(), {
586+
end: { column: 10, line: 3 },
587+
start: { column: 1, line: 1 }
588+
})
589+
equal(two.rangeBy(), {
590+
end: { column: 9, line: 3 },
591+
start: { column: 2, line: 3 }
592+
})
593+
594+
one.remove()
595+
596+
equal(a.rangeBy(), {
597+
end: { column: 10, line: 3 },
598+
start: { column: 1, line: 1 }
599+
})
600+
equal(two.rangeBy(), {
601+
end: { column: 9, line: 3 },
602+
start: { column: 2, line: 3 }
603+
})
604+
})
605+
530606
test('rangeBy() returns range for word', () => {
531607
let css = parse('a { one: X }')
532608
let a = css.first as Rule

0 commit comments

Comments
 (0)