Skip to content

Commit

Permalink
Merge pull request #1848 from romainmenke/reduce-calls-to-node-toStri…
Browse files Browse the repository at this point in the history
…ng--ambitious-scorpion-82d381af30

reduce calls to `Node.prototype.toString()`
  • Loading branch information
ai committed Jul 6, 2023
2 parents 9359a73 + 492f474 commit 87e6dc8
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/node.js
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 87e6dc8

Please sign in to comment.