Skip to content

Commit 145623c

Browse files
authored
Add shorthand property to AST Node (#136)
1 parent 8b551ad commit 145623c

File tree

79 files changed

+233
-96
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+233
-96
lines changed

src/ast.ts

+2
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,12 @@ export interface SvelteAnimationDirective extends BaseSvelteDirective {
559559
}
560560
export interface SvelteBindingDirective extends BaseSvelteDirective {
561561
kind: "Binding"
562+
shorthand: boolean
562563
expression: null | ESTree.Expression
563564
}
564565
export interface SvelteClassDirective extends BaseSvelteDirective {
565566
kind: "Class"
567+
shorthand: boolean
566568
expression: null | ESTree.Expression
567569
}
568570
export interface SvelteEventHandlerDirective extends BaseSvelteDirective {

src/parser/converts/attr.ts

+95-96
Original file line numberDiff line numberDiff line change
@@ -228,31 +228,35 @@ function convertBindingDirective(
228228
type: "SvelteDirective",
229229
kind: "Binding",
230230
key: null as any,
231+
shorthand: false,
231232
expression: null,
232233
parent,
233234
...ctx.getConvertLocation(node),
234235
}
235-
processDirective(node, directive, ctx, (expression) => {
236-
return ctx.scriptLet.addExpression(
237-
expression,
238-
directive,
239-
null,
240-
(es, { getInnermostScope }) => {
241-
directive.expression = es
242-
const scope = getInnermostScope(es)
243-
const reference = scope.references.find(
244-
(ref) => ref.identifier === es,
245-
)
246-
if (reference) {
247-
// The bind directive does read and write.
248-
reference.isWrite = () => true
249-
reference.isWriteOnly = () => false
250-
reference.isReadWrite = () => true
251-
reference.isReadOnly = () => false
252-
reference.isRead = () => true
253-
}
254-
},
255-
)
236+
processDirective(node, directive, ctx, {
237+
processExpression(expression, shorthand) {
238+
directive.shorthand = shorthand
239+
return ctx.scriptLet.addExpression(
240+
expression,
241+
directive,
242+
null,
243+
(es, { getInnermostScope }) => {
244+
directive.expression = es
245+
const scope = getInnermostScope(es)
246+
const reference = scope.references.find(
247+
(ref) => ref.identifier === es,
248+
)
249+
if (reference) {
250+
// The bind directive does read and write.
251+
reference.isWrite = () => true
252+
reference.isWriteOnly = () => false
253+
reference.isReadWrite = () => true
254+
reference.isReadOnly = () => false
255+
reference.isRead = () => true
256+
}
257+
},
258+
)
259+
},
256260
})
257261
return directive
258262
}
@@ -274,18 +278,15 @@ function convertEventHandlerDirective(
274278
const isCustomEvent =
275279
parent.parent.type === "SvelteElement" &&
276280
(parent.parent.kind === "component" || parent.parent.kind === "special")
277-
processDirective(
278-
node,
279-
directive,
280-
ctx,
281-
buildProcessExpressionForExpression(
281+
processDirective(node, directive, ctx, {
282+
processExpression: buildProcessExpressionForExpression(
282283
directive,
283284
ctx,
284285
isCustomEvent
285286
? "(e:CustomEvent<any>)=>void"
286287
: `(e:'${node.name}' extends keyof HTMLElementEventMap?HTMLElementEventMap['${node.name}']:CustomEvent<any>)=>void`,
287288
),
288-
)
289+
})
289290
return directive
290291
}
291292

@@ -299,16 +300,17 @@ function convertClassDirective(
299300
type: "SvelteDirective",
300301
kind: "Class",
301302
key: null as any,
303+
shorthand: false,
302304
expression: null,
303305
parent,
304306
...ctx.getConvertLocation(node),
305307
}
306-
processDirective(
307-
node,
308-
directive,
309-
ctx,
310-
buildProcessExpressionForExpression(directive, ctx, null),
311-
)
308+
processDirective(node, directive, ctx, {
309+
processExpression(expression, shorthand) {
310+
directive.shorthand = shorthand
311+
return ctx.scriptLet.addExpression(expression, directive)
312+
},
313+
})
312314
return directive
313315
}
314316

@@ -368,18 +370,17 @@ function convertOldStyleDirective(
368370
}
369371
processDirectiveKey(node, directive, ctx)
370372
if (processStyleDirectiveValue(node, ctx)) {
371-
processDirectiveExpression(node, directive, ctx, (expression) => {
372-
directive.value.push(
373-
convertTemplateLiteralToLiteral(expression, directive, ctx),
374-
)
375-
return []
373+
processDirectiveExpression(node, directive, ctx, {
374+
processExpression(expression) {
375+
directive.value.push(
376+
convertTemplateLiteralToLiteral(expression, directive, ctx),
377+
)
378+
return []
379+
},
376380
})
377381
} else {
378-
processDirectiveExpression(
379-
node,
380-
directive,
381-
ctx,
382-
(expression, shorthand) => {
382+
processDirectiveExpression(node, directive, ctx, {
383+
processExpression(expression, shorthand) {
383384
;(directive as any).shorthand = shorthand
384385
return ctx.scriptLet.addExpression(
385386
expression,
@@ -400,7 +401,7 @@ function convertOldStyleDirective(
400401
},
401402
)
402403
},
403-
)
404+
})
404405
}
405406

406407
return directive
@@ -463,13 +464,14 @@ function convertTransitionDirective(
463464
parent,
464465
...ctx.getConvertLocation(node),
465466
}
466-
processDirective(
467-
node,
468-
directive,
469-
ctx,
470-
buildProcessExpressionForExpression(directive, ctx, null),
471-
(name) => ctx.scriptLet.addExpression(name, directive.key),
472-
)
467+
processDirective(node, directive, ctx, {
468+
processExpression: buildProcessExpressionForExpression(
469+
directive,
470+
ctx,
471+
null,
472+
),
473+
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
474+
})
473475
return directive
474476
}
475477

@@ -487,13 +489,14 @@ function convertAnimationDirective(
487489
parent,
488490
...ctx.getConvertLocation(node),
489491
}
490-
processDirective(
491-
node,
492-
directive,
493-
ctx,
494-
buildProcessExpressionForExpression(directive, ctx, null),
495-
(name) => ctx.scriptLet.addExpression(name, directive.key),
496-
)
492+
processDirective(node, directive, ctx, {
493+
processExpression: buildProcessExpressionForExpression(
494+
directive,
495+
ctx,
496+
null,
497+
),
498+
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
499+
})
497500
return directive
498501
}
499502

@@ -511,13 +514,14 @@ function convertActionDirective(
511514
parent,
512515
...ctx.getConvertLocation(node),
513516
}
514-
processDirective(
515-
node,
516-
directive,
517-
ctx,
518-
buildProcessExpressionForExpression(directive, ctx, null),
519-
(name) => ctx.scriptLet.addExpression(name, directive.key),
520-
)
517+
processDirective(node, directive, ctx, {
518+
processExpression: buildProcessExpressionForExpression(
519+
directive,
520+
ctx,
521+
null,
522+
),
523+
processName: (name) => ctx.scriptLet.addExpression(name, directive.key),
524+
})
521525
return directive
522526
}
523527

@@ -535,16 +539,13 @@ function convertLetDirective(
535539
parent,
536540
...ctx.getConvertLocation(node),
537541
}
538-
processDirective(
539-
node,
540-
directive,
541-
ctx,
542-
(pattern) => {
542+
processDirective(node, directive, ctx, {
543+
processExpression(pattern) {
543544
return ctx.letDirCollections
544545
.getCollection()
545546
.addPattern(pattern, directive, "any")
546547
},
547-
node.expression
548+
processName: node.expression
548549
? undefined
549550
: (name) => {
550551
// shorthand
@@ -555,7 +556,7 @@ function convertLetDirective(
555556
})
556557
return []
557558
},
558-
)
559+
})
559560
return directive
560561
}
561562

@@ -568,22 +569,18 @@ function processDirective<
568569
node: D & { expression: null | E },
569570
directive: S,
570571
ctx: Context,
571-
processExpression: (
572-
expression: E,
573-
shorthand: boolean,
574-
) => ScriptLetCallback<NonNullable<E>>[],
575-
processName?: (
576-
expression: SvelteName,
577-
) => ScriptLetCallback<ESTree.Identifier>[],
572+
processors: {
573+
processExpression: (
574+
expression: E,
575+
shorthand: boolean,
576+
) => ScriptLetCallback<NonNullable<E>>[]
577+
processName?: (
578+
expression: SvelteName,
579+
) => ScriptLetCallback<ESTree.Identifier>[]
580+
},
578581
) {
579582
processDirectiveKey(node, directive, ctx)
580-
processDirectiveExpression<D, S, E>(
581-
node,
582-
directive,
583-
ctx,
584-
processExpression,
585-
processName,
586-
)
583+
processDirectiveExpression<D, S, E>(node, directive, ctx, processors)
587584
}
588585

589586
/** Common process for directive key */
@@ -657,13 +654,15 @@ function processDirectiveExpression<
657654
node: D & { expression: null | E },
658655
directive: S,
659656
ctx: Context,
660-
processExpression: (
661-
expression: E,
662-
shorthand: boolean,
663-
) => ScriptLetCallback<NonNullable<E>>[],
664-
processName?: (
665-
expression: SvelteName,
666-
) => ScriptLetCallback<ESTree.Identifier>[],
657+
processors: {
658+
processExpression: (
659+
expression: E,
660+
shorthand: boolean,
661+
) => ScriptLetCallback<NonNullable<E>>[]
662+
processName?: (
663+
expression: SvelteName,
664+
) => ScriptLetCallback<ESTree.Identifier>[]
665+
},
667666
) {
668667
const key = directive.key
669668
const keyName = key.name as SvelteName
@@ -679,15 +678,15 @@ function processDirectiveExpression<
679678
// e.g. bind:value=""
680679
getWithLoc(node.expression).end = keyName.range[1]
681680
}
682-
processExpression(node.expression, shorthand).push((es) => {
681+
processors.processExpression(node.expression, shorthand).push((es) => {
683682
if (directive.type === "SvelteDirective") {
684683
directive.expression = es
685684
}
686685
})
687686
}
688687
if (!shorthand) {
689-
if (processName) {
690-
processName(keyName).push((es) => {
688+
if (processors.processName) {
689+
processors.processName(keyName).push((es) => {
691690
key.name = es
692691
})
693692
} else {

tests/fixtures/parser/ast/blog/write-less-code01-output.json

+2
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
}
378378
}
379379
},
380+
"shorthand": false,
380381
"range": [
381382
65,
382383
79
@@ -581,6 +582,7 @@
581582
}
582583
}
583584
},
585+
"shorthand": false,
584586
"range": [
585587
102,
586588
116

tests/fixtures/parser/ast/class-directive01-output.json

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
}
322322
}
323323
},
324+
"shorthand": false,
324325
"range": [
325326
60,
326327
75
@@ -485,6 +486,7 @@
485486
}
486487
}
487488
},
489+
"shorthand": true,
488490
"range": [
489491
88,
490492
97

tests/fixtures/parser/ast/docs/template-syntax/04-comments/02-output.json

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
}
119119
}
120120
},
121+
"shorthand": false,
121122
"range": [
122123
45,
123124
62

tests/fixtures/parser/ast/docs/template-syntax/11-element-directives/02-bind-property/01-output.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
}
8383
}
8484
},
85+
"shorthand": false,
8586
"range": [
8687
5,
8788
29

tests/fixtures/parser/ast/docs/template-syntax/11-element-directives/02-bind-property/02-output.json

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
}
8383
}
8484
},
85+
"shorthand": false,
8586
"range": [
8687
7,
8788
24
@@ -230,6 +231,7 @@
230231
}
231232
}
232233
},
234+
"shorthand": false,
233235
"range": [
234236
36,
235237
53
@@ -450,6 +452,7 @@
450452
}
451453
}
452454
},
455+
"shorthand": false,
453456
"range": [
454457
90,
455458
108

0 commit comments

Comments
 (0)