]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: useWith -> prefixIdentifiers
authorEvan You <yyx990803@gmail.com>
Mon, 23 Sep 2019 17:29:41 +0000 (13:29 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 23 Sep 2019 17:29:52 +0000 (13:29 -0400)
packages/compiler-core/__tests__/transforms/expression.spec.ts
packages/compiler-core/src/codegen.ts
packages/compiler-core/src/errors.ts
packages/compiler-core/src/index.ts
packages/compiler-core/src/transform.ts
packages/compiler-core/src/transforms/vFor.ts
packages/compiler-core/src/transforms/vIf.ts

index 33b077440befa4f6c70be010b552a09c00461307..a54c9b23b475e6f1ae9d3f838d5116cc72783aa3 100644 (file)
@@ -9,7 +9,7 @@ test(`should work`, async () => {
     <p>{{ i }}</p>
     `,
     {
-      useWith: false
+      prefixIdentifiers: true
     }
   )
   console.log(code)
index 1a32b0e215476d319e944e1fa6ffa5033933f1ef..f62bf69b786e98875d4c8d82b53d9e85547ef995 100644 (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(`}`)
   }
index 14e89429aff903339bd2b56b80c694c584935232..2f28f5f797fe44d2ce90a6c5ebae85d869d231f8 100644 (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.`
 }
index 0307f23ae973204310a4b64068811c57820db6e2..7b0a95decb916f6e328a014b729e3fe1d4f349fb 100644 (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
     ],
index 862fce6558f3129d04b9edb9285eb8c2e869972e..00caba19408b59ffec2d2aae41cf5cb856ac3c34 100644 (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,
index 9771e85903f31a3206d18a7ad2f358f90aded920..d46005e484e26eebbe8a9d2fab32dfa71d17cbd3 100644 (file)
@@ -87,7 +87,7 @@ function parseForExpression(
       RHS.trim(),
       source.indexOf(RHS, LHS.length),
       context,
-      !context.useWith
+      context.prefixIdentifiers
     ),
     value: undefined,
     key: undefined,
index 3bfa2341f04bb7b799676a4114683deaf05f1471..f0f5830c610b77938c320550ed215c3063a7d7b0 100644 (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') {