forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
234 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/compiler-vapor/src/generators/objectExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
NewlineType, | ||
type ObjectExpression, | ||
type SimpleExpressionNode, | ||
type SourceLocation, | ||
isSimpleIdentifier, | ||
} from '@vue/compiler-dom' | ||
import type { CodegenContext } from '../generate' | ||
import { genExpression } from './expression' | ||
import type { IRExpression } from '@vue/compiler-vapor' | ||
import { isString } from '@vue/shared' | ||
|
||
export function genObjectExpression( | ||
node: ObjectExpression, | ||
context: CodegenContext, | ||
): void { | ||
const { push, withIndent, newline } = context | ||
const { properties, loc } = node | ||
if (!properties.length) { | ||
push(`{}`, NewlineType.None, loc) | ||
} | ||
const multilines = properties.length > 1 | ||
push(multilines ? `{` : `{ `) | ||
const genProperties = () => { | ||
for (let i = 0; i < properties.length; i++) { | ||
const { key, value, loc } = properties[i] | ||
// key | ||
genExpressionAsPropertyKey(key as SimpleExpressionNode, loc, context) | ||
push(': ') | ||
// value | ||
genExpression(value as SimpleExpressionNode, context) | ||
if (i < properties.length - 1) { | ||
// will only reach this if it's multilines | ||
push(',') | ||
newline() | ||
} | ||
} | ||
} | ||
multilines | ||
? withIndent(() => { | ||
newline() | ||
genProperties() | ||
newline() | ||
}) | ||
: genProperties() | ||
push(multilines ? '}' : ' }') | ||
} | ||
|
||
function genExpressionAsPropertyKey( | ||
node: IRExpression, | ||
loc: SourceLocation, | ||
context: CodegenContext, | ||
) { | ||
const { push, pushMulti } = context | ||
if (isString(node) || node.isStatic) { | ||
const keyName = isString(node) ? node : node.content | ||
// only quote keys if necessary | ||
const text = isSimpleIdentifier(keyName) ? keyName : JSON.stringify(keyName) | ||
push(text, NewlineType.None, loc) | ||
} else { | ||
pushMulti(['[', ']'], () => genExpression(node, context)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.