feat(compiler-core): add parser transform for v-for directive (#65)
* feat(compiler-core): add parser transform for v-for directive * fix: Include source location for expressions * chore: remove comment * refactor(compiler-core): extract hepler functions to utils
This commit is contained in:
50
packages/compiler-core/src/utils.ts
Normal file
50
packages/compiler-core/src/utils.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { SourceLocation, Position } from './ast'
|
||||
|
||||
export function getInnerRange(
|
||||
loc: SourceLocation,
|
||||
offset: number,
|
||||
length?: number
|
||||
): SourceLocation {
|
||||
const source = loc.source.substr(offset, length)
|
||||
const newLoc: SourceLocation = {
|
||||
source,
|
||||
start: advancePositionBy(loc.start, loc.source, offset),
|
||||
end: loc.end
|
||||
}
|
||||
|
||||
if (length != null) {
|
||||
newLoc.end = advancePositionBy(loc.start, loc.source, offset + length)
|
||||
}
|
||||
|
||||
return newLoc
|
||||
}
|
||||
|
||||
export function advancePositionBy(
|
||||
pos: Position,
|
||||
source: string,
|
||||
numberOfCharacters: number
|
||||
): Position {
|
||||
__DEV__ && assert(numberOfCharacters <= source.length)
|
||||
|
||||
const newPosition = {
|
||||
...pos
|
||||
}
|
||||
|
||||
const str = source.slice(0, numberOfCharacters)
|
||||
const lines = str.split(/\r?\n/)
|
||||
|
||||
newPosition.offset += numberOfCharacters
|
||||
newPosition.line += lines.length - 1
|
||||
newPosition.column =
|
||||
lines.length === 1
|
||||
? pos.column + numberOfCharacters
|
||||
: Math.max(1, lines.pop()!.length)
|
||||
|
||||
return newPosition
|
||||
}
|
||||
|
||||
export function assert(condition: boolean, msg?: string) {
|
||||
if (!condition) {
|
||||
throw new Error(msg || `unexpected parser condition`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user