Skip to content

Commit

Permalink
feat(compiler-sfc): new script setup implementation
Browse files Browse the repository at this point in the history
- now exposes all top level bindings to template
- support `ref:` syntax sugar
  • Loading branch information
yyx990803 committed Oct 29, 2020
1 parent 0935c05 commit 556560f
Show file tree
Hide file tree
Showing 5 changed files with 758 additions and 557 deletions.
97 changes: 58 additions & 39 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export function processExpression(
if (!isDuplicate(node)) {
const needPrefix = shouldPrefix(node, parent)
if (!knownIds[node.name] && needPrefix) {
if (isPropertyShorthand(node, parent)) {
// property shorthand like { foo }, we need to add the key since we
// rewrite the value
if (isStaticProperty(parent) && parent.shorthand) {
// property shorthand like { foo }, we need to add the key since
// we rewrite the value
node.prefix = `${node.name}: `
}
node.name = prefix(node.name)
Expand Down Expand Up @@ -278,46 +278,65 @@ const isStaticProperty = (node: Node): node is ObjectProperty =>
(node.type === 'ObjectProperty' || node.type === 'ObjectMethod') &&
!node.computed

const isPropertyShorthand = (node: Node, parent: Node) => {
return (
isStaticProperty(parent) &&
parent.value === node &&
parent.key.type === 'Identifier' &&
parent.key.name === (node as Identifier).name &&
parent.key.start === node.start
)
}

const isStaticPropertyKey = (node: Node, parent: Node) =>
isStaticProperty(parent) && parent.key === node

function shouldPrefix(identifier: Identifier, parent: Node) {
function shouldPrefix(id: Identifier, parent: Node) {
// declaration id
if (
!(
isFunction(parent) &&
// not id of a FunctionDeclaration
((parent as any).id === identifier ||
// not a params of a function
parent.params.includes(identifier))
) &&
// not a key of Property
!isStaticPropertyKey(identifier, parent) &&
// not a property of a MemberExpression
!(
(parent.type === 'MemberExpression' ||
parent.type === 'OptionalMemberExpression') &&
parent.property === identifier &&
!parent.computed
) &&
// not in an Array destructure pattern
!(parent.type === 'ArrayPattern') &&
// skip whitelisted globals
!isGloballyWhitelisted(identifier.name) &&
// special case for webpack compilation
identifier.name !== `require` &&
// is a special keyword but parsed as identifier
identifier.name !== `arguments`
(parent.type === 'VariableDeclarator' ||
parent.type === 'ClassDeclaration') &&
parent.id === id
) {
return true
return false
}

if (isFunction(parent)) {
// function decalration/expression id
if ((parent as any).id === id) {
return false
}
// params list
if (parent.params.includes(id)) {
return false
}
}

// property key
// this also covers object destructure pattern
if (isStaticPropertyKey(id, parent)) {
return false
}

// array destructure pattern
if (parent.type === 'ArrayPattern') {
return false
}

// member expression property
if (
(parent.type === 'MemberExpression' ||
parent.type === 'OptionalMemberExpression') &&
parent.property === id &&
!parent.computed
) {
return false
}

// is a special keyword but parsed as identifier
if (id.name === 'arguments') {
return false
}

// skip whitelisted globals
if (isGloballyWhitelisted(id.name)) {
return false
}

// special case for webpack compilation
if (id.name === 'require') {
return false
}

return true
}
Loading

1 comment on commit 556560f

@ChanningHan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bravo!!!
ref-sugar: yes!!!

Please sign in to comment.