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

177 lines
4.4 KiB
TypeScript
Raw Normal View History

import {
SourceLocation,
Position,
ElementNode,
NodeTypes,
CallExpression,
SequenceExpression,
createSequenceExpression,
createCallExpression,
ExpressionNode,
CompoundExpressionNode,
createCompoundExpression,
DirectiveNode
} from './ast'
2019-10-02 23:05:56 +08:00
import { parse } from 'acorn'
import { walk } from 'estree-walker'
import { TransformContext } from './transform'
import { OPEN_BLOCK, CREATE_BLOCK } from './runtimeConstants'
import { isString } from '@vue/shared'
// cache node requires
// lazy require dependencies so that they don't end up in rollup's dep graph
// and thus can be tree-shaken in browser builds.
2019-10-02 23:05:56 +08:00
let _parse: typeof parse
let _walk: typeof walk
2019-10-02 23:05:56 +08:00
export const parseJS: typeof parse = (code: string, options: any) => {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
2019-10-02 23:05:56 +08:00
const parse = _parse || (_parse = require('acorn').parse)
return parse(code, options)
}
export const walkJS: typeof walk = (ast, walker) => {
assert(
!__BROWSER__,
`Expression AST analysis can only be performed in non-browser builds.`
)
const walk = _walk || (_walk = require('estree-walker').walk)
return walk(ast, walker)
}
2019-09-25 10:39:20 +08:00
export const isSimpleIdentifier = (name: string): boolean =>
!/^\d|[^\w]/.test(name)
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 = source.length
): 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 = source.length
): 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)
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`)
}
}
2019-10-03 06:03:42 +08:00
export function findDir(
node: ElementNode,
2019-10-03 06:03:42 +08:00
name: string | RegExp,
allowEmpty: boolean = false
): DirectiveNode | undefined {
for (let i = 0; i < node.props.length; i++) {
const p = node.props[i]
if (
p.type === NodeTypes.DIRECTIVE &&
2019-10-03 06:03:42 +08:00
(allowEmpty || p.exp) &&
p.name.match(name)
) {
return p
}
}
}
export function findProp(
node: ElementNode,
name: string
): ElementNode['props'][0] | undefined {
for (let i = 0; i < node.props.length; i++) {
const p = node.props[i]
if (p.type === NodeTypes.ATTRIBUTE) {
if (p.name === name && p.value && !p.value.isEmpty) {
return p
}
} else if (
p.arg &&
p.arg.type === NodeTypes.SIMPLE_EXPRESSION &&
p.arg.isStatic &&
p.arg.content === name &&
p.exp
) {
return p
}
}
}
export function createBlockExpression(
args: CallExpression['arguments'],
context: TransformContext
): SequenceExpression {
return createSequenceExpression([
createCallExpression(context.helper(OPEN_BLOCK)),
createCallExpression(context.helper(CREATE_BLOCK), args)
])
}
export function mergeExpressions(
...args: (string | ExpressionNode)[]
): CompoundExpressionNode {
const children: CompoundExpressionNode['children'] = []
for (let i = 0; i < args.length; i++) {
const exp = args[i]
if (isString(exp) || exp.type === NodeTypes.SIMPLE_EXPRESSION) {
children.push(exp)
} else {
children.push(...exp.children)
}
}
return createCompoundExpression(children)
}