Skip to content

Add shorthand property to AST Node #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,12 @@ export interface SvelteAnimationDirective extends BaseSvelteDirective {
}
export interface SvelteBindingDirective extends BaseSvelteDirective {
kind: "Binding"
shorthand: boolean
expression: null | ESTree.Expression
}
export interface SvelteClassDirective extends BaseSvelteDirective {
kind: "Class"
shorthand: boolean
expression: null | ESTree.Expression
}
export interface SvelteEventHandlerDirective extends BaseSvelteDirective {
Expand Down
191 changes: 95 additions & 96 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,35 @@ function convertBindingDirective(
type: "SvelteDirective",
kind: "Binding",
key: null as any,
shorthand: false,
expression: null,
parent,
...ctx.getConvertLocation(node),
}
processDirective(node, directive, ctx, (expression) => {
return ctx.scriptLet.addExpression(
expression,
directive,
null,
(es, { getInnermostScope }) => {
directive.expression = es
const scope = getInnermostScope(es)
const reference = scope.references.find(
(ref) => ref.identifier === es,
)
if (reference) {
// The bind directive does read and write.
reference.isWrite = () => true
reference.isWriteOnly = () => false
reference.isReadWrite = () => true
reference.isReadOnly = () => false
reference.isRead = () => true
}
},
)
processDirective(node, directive, ctx, {
processExpression(expression, shorthand) {
directive.shorthand = shorthand
return ctx.scriptLet.addExpression(
expression,
directive,
null,
(es, { getInnermostScope }) => {
directive.expression = es
const scope = getInnermostScope(es)
const reference = scope.references.find(
(ref) => ref.identifier === es,
)
if (reference) {
// The bind directive does read and write.
reference.isWrite = () => true
reference.isWriteOnly = () => false
reference.isReadWrite = () => true
reference.isReadOnly = () => false
reference.isRead = () => true
}
},
)
},
})
return directive
}
Expand All @@ -274,18 +278,15 @@ function convertEventHandlerDirective(
const isCustomEvent =
parent.parent.type === "SvelteElement" &&
(parent.parent.kind === "component" || parent.parent.kind === "special")
processDirective(
node,
directive,
ctx,
buildProcessExpressionForExpression(
processDirective(node, directive, ctx, {
processExpression: buildProcessExpressionForExpression(
directive,
ctx,
isCustomEvent
? "(e:CustomEvent<any>)=>void"
: `(e:'${node.name}' extends keyof HTMLElementEventMap?HTMLElementEventMap['${node.name}']:CustomEvent<any>)=>void`,
),
)
})
return directive
}

Expand All @@ -299,16 +300,17 @@ function convertClassDirective(
type: "SvelteDirective",
kind: "Class",
key: null as any,
shorthand: false,
expression: null,
parent,
...ctx.getConvertLocation(node),
}
processDirective(
node,
directive,
ctx,
buildProcessExpressionForExpression(directive, ctx, null),
)
processDirective(node, directive, ctx, {
processExpression(expression, shorthand) {
directive.shorthand = shorthand
return ctx.scriptLet.addExpression(expression, directive)
},
})
return directive
}

Expand Down Expand Up @@ -368,18 +370,17 @@ function convertOldStyleDirective(
}
processDirectiveKey(node, directive, ctx)
if (processStyleDirectiveValue(node, ctx)) {
processDirectiveExpression(node, directive, ctx, (expression) => {
directive.value.push(
convertTemplateLiteralToLiteral(expression, directive, ctx),
)
return []
processDirectiveExpression(node, directive, ctx, {
processExpression(expression) {
directive.value.push(
convertTemplateLiteralToLiteral(expression, directive, ctx),
)
return []
},
})
} else {
processDirectiveExpression(
node,
directive,
ctx,
(expression, shorthand) => {
processDirectiveExpression(node, directive, ctx, {
processExpression(expression, shorthand) {
;(directive as any).shorthand = shorthand
return ctx.scriptLet.addExpression(
expression,
Expand All @@ -400,7 +401,7 @@ function convertOldStyleDirective(
},
)
},
)
})
}

return directive
Expand Down Expand Up @@ -463,13 +464,14 @@ function convertTransitionDirective(
parent,
...ctx.getConvertLocation(node),
}
processDirective(
node,
directive,
ctx,
buildProcessExpressionForExpression(directive, ctx, null),
(name) => ctx.scriptLet.addExpression(name, directive.key),
)
processDirective(node, directive, ctx, {
processExpression: buildProcessExpressionForExpression(
directive,
ctx,
null,
),
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
})
return directive
}

Expand All @@ -487,13 +489,14 @@ function convertAnimationDirective(
parent,
...ctx.getConvertLocation(node),
}
processDirective(
node,
directive,
ctx,
buildProcessExpressionForExpression(directive, ctx, null),
(name) => ctx.scriptLet.addExpression(name, directive.key),
)
processDirective(node, directive, ctx, {
processExpression: buildProcessExpressionForExpression(
directive,
ctx,
null,
),
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
})
return directive
}

Expand All @@ -511,13 +514,14 @@ function convertActionDirective(
parent,
...ctx.getConvertLocation(node),
}
processDirective(
node,
directive,
ctx,
buildProcessExpressionForExpression(directive, ctx, null),
(name) => ctx.scriptLet.addExpression(name, directive.key),
)
processDirective(node, directive, ctx, {
processExpression: buildProcessExpressionForExpression(
directive,
ctx,
null,
),
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
})
return directive
}

Expand All @@ -535,16 +539,13 @@ function convertLetDirective(
parent,
...ctx.getConvertLocation(node),
}
processDirective(
node,
directive,
ctx,
(pattern) => {
processDirective(node, directive, ctx, {
processExpression(pattern) {
return ctx.letDirCollections
.getCollection()
.addPattern(pattern, directive, "any")
},
node.expression
processName: node.expression
? undefined
: (name) => {
// shorthand
Expand All @@ -555,7 +556,7 @@ function convertLetDirective(
})
return []
},
)
})
return directive
}

Expand All @@ -568,22 +569,18 @@ function processDirective<
node: D & { expression: null | E },
directive: S,
ctx: Context,
processExpression: (
expression: E,
shorthand: boolean,
) => ScriptLetCallback<NonNullable<E>>[],
processName?: (
expression: SvelteName,
) => ScriptLetCallback<ESTree.Identifier>[],
processors: {
processExpression: (
expression: E,
shorthand: boolean,
) => ScriptLetCallback<NonNullable<E>>[]
processName?: (
expression: SvelteName,
) => ScriptLetCallback<ESTree.Identifier>[]
},
) {
processDirectiveKey(node, directive, ctx)
processDirectiveExpression<D, S, E>(
node,
directive,
ctx,
processExpression,
processName,
)
processDirectiveExpression<D, S, E>(node, directive, ctx, processors)
}

/** Common process for directive key */
Expand Down Expand Up @@ -657,13 +654,15 @@ function processDirectiveExpression<
node: D & { expression: null | E },
directive: S,
ctx: Context,
processExpression: (
expression: E,
shorthand: boolean,
) => ScriptLetCallback<NonNullable<E>>[],
processName?: (
expression: SvelteName,
) => ScriptLetCallback<ESTree.Identifier>[],
processors: {
processExpression: (
expression: E,
shorthand: boolean,
) => ScriptLetCallback<NonNullable<E>>[]
processName?: (
expression: SvelteName,
) => ScriptLetCallback<ESTree.Identifier>[]
},
) {
const key = directive.key
const keyName = key.name as SvelteName
Expand All @@ -679,15 +678,15 @@ function processDirectiveExpression<
// e.g. bind:value=""
getWithLoc(node.expression).end = keyName.range[1]
}
processExpression(node.expression, shorthand).push((es) => {
processors.processExpression(node.expression, shorthand).push((es) => {
if (directive.type === "SvelteDirective") {
directive.expression = es
}
})
}
if (!shorthand) {
if (processName) {
processName(keyName).push((es) => {
if (processors.processName) {
processors.processName(keyName).push((es) => {
key.name = es
})
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/parser/ast/blog/write-less-code01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@
}
}
},
"shorthand": false,
"range": [
65,
79
Expand Down Expand Up @@ -581,6 +582,7 @@
}
}
},
"shorthand": false,
"range": [
102,
116
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/parser/ast/class-directive01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
}
}
},
"shorthand": false,
"range": [
60,
75
Expand Down Expand Up @@ -485,6 +486,7 @@
}
}
},
"shorthand": true,
"range": [
88,
97
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
}
}
},
"shorthand": false,
"range": [
45,
62
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
}
}
},
"shorthand": false,
"range": [
5,
29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
}
}
},
"shorthand": false,
"range": [
7,
24
Expand Down Expand Up @@ -230,6 +231,7 @@
}
}
},
"shorthand": false,
"range": [
36,
53
Expand Down Expand Up @@ -450,6 +452,7 @@
}
}
},
"shorthand": false,
"range": [
90,
108
Expand Down
Loading