Skip to content

Commit

Permalink
reduce calls to Node.prototype.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Jul 2, 2023
1 parent 9359a73 commit 492f474
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ class Node {
return fixed
}

positionInside(index) {
let string = this.toString()
positionInside(index, stringRepresentation) {
let string = stringRepresentation || this.toString()
let column = this.source.start.column
let line = this.source.start.line

Expand All @@ -246,13 +246,14 @@ class Node {
return { line, column }
}

positionBy(opts) {
positionBy(opts, stringRepresentation) {
let pos = this.source.start
if (opts.index) {
pos = this.positionInside(opts.index)
pos = this.positionInside(opts.index, stringRepresentation)
} else if (opts.word) {
let index = this.toString().indexOf(opts.word)
if (index !== -1) pos = this.positionInside(index)
stringRepresentation = this.toString()
let index = stringRepresentation.indexOf(opts.word)
if (index !== -1) pos = this.positionInside(index, stringRepresentation)
}
return pos
}
Expand All @@ -264,19 +265,20 @@ class Node {
}
let end = this.source.end
? {
line: this.source.end.line,
column: this.source.end.column + 1
}
line: this.source.end.line,
column: this.source.end.column + 1
}
: {
line: start.line,
column: start.column + 1
}
line: start.line,
column: start.column + 1
}

if (opts.word) {
let index = this.toString().indexOf(opts.word)
let stringRepresentation = this.toString()
let index = stringRepresentation.indexOf(opts.word)
if (index !== -1) {
start = this.positionInside(index)
end = this.positionInside(index + opts.word.length)
start = this.positionInside(index, stringRepresentation)
end = this.positionInside(index + opts.word.length, stringRepresentation)
}
} else {
if (opts.start) {
Expand Down

0 comments on commit 492f474

Please sign in to comment.