refactor: useWith -> prefixIdentifiers

This commit is contained in:
Evan You 2019-09-23 13:29:41 -04:00
parent e57cb51066
commit 88e5e96a3e
7 changed files with 19 additions and 19 deletions

View File

@ -9,7 +9,7 @@ test(`should work`, async () => {
<p>{{ i }}</p>
`,
{
useWith: false
prefixIdentifiers: true
}
)
console.log(code)

View File

@ -26,7 +26,7 @@ export interface CodegenOptions {
// runtime helpers; otherwise will grab the helpers from global `Vue`.
// default: false
mode?: 'module' | 'function'
useWith?: boolean
prefixIdentifiers?: boolean
// Filename for source map generation.
filename?: string
}
@ -54,13 +54,13 @@ function createCodegenContext(
ast: RootNode,
{
mode = 'function',
useWith = true,
prefixIdentifiers = false,
filename = `template.vue.html`
}: CodegenOptions
): CodegenContext {
const context: CodegenContext = {
mode,
useWith,
prefixIdentifiers,
filename,
source: ast.loc.source,
code: ``,
@ -119,7 +119,7 @@ export function generate(
options: CodegenOptions = {}
): CodegenResult {
const context = createCodegenContext(ast, options)
const { mode, push, useWith, indent, deindent, newline } = context
const { mode, push, prefixIdentifiers, indent, deindent, newline } = context
const imports = ast.imports.join(', ')
if (mode === 'function') {
// generate const declarations for helpers
@ -144,13 +144,13 @@ export function generate(
})
newline()
}
if (useWith) {
if (!prefixIdentifiers) {
push(`with (this) {`)
indent()
}
push(`return `)
genChildren(ast.children, context)
if (useWith) {
if (!prefixIdentifiers) {
deindent()
push(`}`)
}

View File

@ -69,7 +69,7 @@ export const enum ErrorCodes {
X_V_BIND_NO_EXPRESSION,
// generic errors
X_STRIP_WITH_NOT_SUPPORTED
X_PREFIX_ID_NOT_SUPPORTED
}
export const errorMessages: { [code: number]: string } = {
@ -135,5 +135,5 @@ export const errorMessages: { [code: number]: string } = {
[ErrorCodes.X_FOR_MALFORMED_EXPRESSION]: `v-for has invalid expression`,
// generic errors
[ErrorCodes.X_STRIP_WITH_NOT_SUPPORTED]: `useWith: false is not supported in this build of compiler because it is optimized for payload size.`
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler because it is optimized for payload size.`
}

View File

@ -18,21 +18,21 @@ export function compile(
options: CompilerOptions = {}
): CodegenResult {
const ast = isString(template) ? parse(template, options) : template
const useWith = __BROWSER__ || options.useWith !== false
const prefixIdentifiers = !__BROWSER__ && options.prefixIdentifiers === true
if (__BROWSER__ && options.useWith === false) {
if (__BROWSER__ && options.prefixIdentifiers === false) {
;(options.onError || defaultOnError)(
createCompilerError(ErrorCodes.X_STRIP_WITH_NOT_SUPPORTED)
createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED)
)
}
transform(ast, {
...options,
useWith,
prefixIdentifiers,
nodeTransforms: [
transformIf,
transformFor,
...(useWith ? [] : [expressionTransform]),
...(prefixIdentifiers ? [expressionTransform] : []),
prepareElementForCodegen,
...(options.nodeTransforms || []) // user transforms
],

View File

@ -43,7 +43,7 @@ export type StructuralDirectiveTransform = (
export interface TransformOptions {
nodeTransforms?: NodeTransform[]
directiveTransforms?: { [name: string]: DirectiveTransform }
useWith?: boolean
prefixIdentifiers?: boolean
onError?: (error: CompilerError) => void
}
@ -65,7 +65,7 @@ export interface TransformContext extends Required<TransformOptions> {
function createTransformContext(
root: RootNode,
{
useWith = true,
prefixIdentifiers = false,
nodeTransforms = [],
directiveTransforms = {},
onError = defaultOnError
@ -75,7 +75,7 @@ function createTransformContext(
imports: new Set(),
statements: [],
identifiers: {},
useWith,
prefixIdentifiers,
nodeTransforms,
directiveTransforms,
onError,

View File

@ -87,7 +87,7 @@ function parseForExpression(
RHS.trim(),
source.indexOf(RHS, LHS.length),
context,
!context.useWith
context.prefixIdentifiers
),
value: undefined,
key: undefined,

View File

@ -15,7 +15,7 @@ import { processExpression } from './expression'
export const transformIf = createStructuralDirectiveTransform(
/^(if|else|else-if)$/,
(node, dir, context) => {
if (!__BROWSER__ && !context.useWith && dir.exp) {
if (!__BROWSER__ && context.prefixIdentifiers && dir.exp) {
processExpression(dir.exp, context)
}
if (dir.name === 'if') {