vue3-yuanma/packages/compiler-core/src/utils.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

import { SourceLocation, Position } from './ast'
export function getInnerRange(
loc: SourceLocation,
offset: number,
length?: number
): SourceLocation {
__DEV__ && assert(offset <= loc.source.length)
const source = loc.source.substr(offset, length)
const newLoc: SourceLocation = {
source,
start: advancePositionWithClone(loc.start, loc.source, offset),
end: loc.end
}
if (length != null) {
__DEV__ && assert(offset + length <= loc.source.length)
newLoc.end = advancePositionWithClone(
loc.start,
loc.source,
offset + length
)
}
return newLoc
}
export function advancePositionWithClone(
pos: Position,
source: string,
numberOfCharacters: number
): Position {
return advancePositionWithMutation({ ...pos }, source, numberOfCharacters)
}
// advance by mutation without cloning (for performance reasons), since this
// gets called a lot in the parser
export function advancePositionWithMutation(
pos: Position,
source: string,
numberOfCharacters: number
): Position {
let linesCount = 0
let lastNewLinePos = -1
for (let i = 0; i < numberOfCharacters; i++) {
if (source.charCodeAt(i) === 10 /* newline char code */) {
linesCount++
lastNewLinePos = i
}
}
pos.offset += numberOfCharacters
pos.line += linesCount
pos.column =
lastNewLinePos === -1
? pos.column + numberOfCharacters
: Math.max(1, numberOfCharacters - lastNewLinePos - 1)
return pos
}
export function assert(condition: boolean, msg?: string) {
2019-09-25 04:35:01 +08:00
/* istanbul ignore if */
if (!condition) {
2019-09-23 04:50:57 +08:00
throw new Error(msg || `unexpected compiler condition`)
}
}