feat(compiler): basic v-bind & v-on transforms

This commit is contained in:
Evan You
2019-09-22 22:19:42 -04:00
parent 3ab016e44f
commit 914087edea
14 changed files with 749 additions and 715 deletions

View File

@@ -123,10 +123,7 @@ function buildProps(
mergeArgs.push(prop.exp)
} else {
context.onError(
createCompilerError(
ErrorCodes.X_V_BIND_NO_EXPRESSION,
prop.loc.start
)
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, prop.loc)
)
}
continue

View File

@@ -1 +1,24 @@
// TODO
import { DirectiveTransform } from '../transform'
import { createObjectProperty, createExpression } from '../ast'
import { createCompilerError, ErrorCodes } from '../errors'
// v-bind without arg is handled directly in ./element.ts due to it affecting
// codegen for the entire props object. This transform here is only for v-bind
// *with* args.
export const transformBind: DirectiveTransform = (dir, context) => {
if (!dir.exp) {
context.onError(
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, dir.loc)
)
}
// TODO handle .prop modifier
// TODO handle .sync modifier
return {
props: createObjectProperty(
dir.arg!,
dir.exp || createExpression('', true, dir.loc),
dir.loc
),
needRuntime: false
}
}

View File

@@ -32,15 +32,12 @@ export const transformFor = createStructuralDirectiveTransform(
})
} else {
context.onError(
createCompilerError(
ErrorCodes.X_FOR_MALFORMED_EXPRESSION,
dir.loc.start
)
createCompilerError(ErrorCodes.X_FOR_MALFORMED_EXPRESSION, dir.loc)
)
}
} else {
context.onError(
createCompilerError(ErrorCodes.X_FOR_NO_EXPRESSION, dir.loc.start)
createCompilerError(ErrorCodes.X_FOR_NO_EXPRESSION, dir.loc)
)
}
}

View File

@@ -49,7 +49,7 @@ export const transformIf = createStructuralDirectiveTransform(
dir.name === 'else'
? ErrorCodes.X_ELSE_NO_ADJACENT_IF
: ErrorCodes.X_ELSE_IF_NO_ADJACENT_IF,
node.loc.start
node.loc
)
)
}

View File

@@ -1 +1,24 @@
// TODO
import { DirectiveTransform } from '../transform'
import { createObjectProperty, createExpression } from '../ast'
import { capitalize } from '@vue/shared'
// v-on without arg is handled directly in ./element.ts due to it affecting
// codegen for the entire props object. This transform here is only for v-on
// *with* args.
export const transformOn: DirectiveTransform = (dir, context) => {
const arg = dir.arg!
const eventName = arg.isStatic
? createExpression(`on${capitalize(arg.content)}`, true, arg.loc)
: // TODO inject capitalize helper
createExpression(`'on' + capitalize(${arg.content})`, false, arg.loc)
// TODO .once modifier handling since it is platform agnostic
// other modifiers are handled in compiler-dom
return {
props: createObjectProperty(
eventName,
dir.exp || createExpression(`() => {}`, false, dir.loc),
dir.loc
),
needRuntime: false
}
}