From: Evan You Date: Mon, 23 Sep 2019 17:29:41 +0000 (-0400) Subject: refactor: useWith -> prefixIdentifiers X-Git-Tag: v3.0.0-alpha.0~718 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=88e5e96a3e1a0807798b59f128eb9a0a80006198;p=thirdparty%2Fvuejs%2Fcore.git refactor: useWith -> prefixIdentifiers --- diff --git a/packages/compiler-core/__tests__/transforms/expression.spec.ts b/packages/compiler-core/__tests__/transforms/expression.spec.ts index 33b077440b..a54c9b23b4 100644 --- a/packages/compiler-core/__tests__/transforms/expression.spec.ts +++ b/packages/compiler-core/__tests__/transforms/expression.spec.ts @@ -9,7 +9,7 @@ test(`should work`, async () => {

{{ i }}

`, { - useWith: false + prefixIdentifiers: true } ) console.log(code) diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index 1a32b0e215..f62bf69b78 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -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(`}`) } diff --git a/packages/compiler-core/src/errors.ts b/packages/compiler-core/src/errors.ts index 14e89429af..2f28f5f797 100644 --- a/packages/compiler-core/src/errors.ts +++ b/packages/compiler-core/src/errors.ts @@ -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.` } diff --git a/packages/compiler-core/src/index.ts b/packages/compiler-core/src/index.ts index 0307f23ae9..7b0a95decb 100644 --- a/packages/compiler-core/src/index.ts +++ b/packages/compiler-core/src/index.ts @@ -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 ], diff --git a/packages/compiler-core/src/transform.ts b/packages/compiler-core/src/transform.ts index 862fce6558..00caba1940 100644 --- a/packages/compiler-core/src/transform.ts +++ b/packages/compiler-core/src/transform.ts @@ -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 { 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, diff --git a/packages/compiler-core/src/transforms/vFor.ts b/packages/compiler-core/src/transforms/vFor.ts index 9771e85903..d46005e484 100644 --- a/packages/compiler-core/src/transforms/vFor.ts +++ b/packages/compiler-core/src/transforms/vFor.ts @@ -87,7 +87,7 @@ function parseForExpression( RHS.trim(), source.indexOf(RHS, LHS.length), context, - !context.useWith + context.prefixIdentifiers ), value: undefined, key: undefined, diff --git a/packages/compiler-core/src/transforms/vIf.ts b/packages/compiler-core/src/transforms/vIf.ts index 3bfa2341f0..f0f5830c61 100644 --- a/packages/compiler-core/src/transforms/vIf.ts +++ b/packages/compiler-core/src/transforms/vIf.ts @@ -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') {