Skip to content

Change AST #59

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
Jun 29, 2021
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
3 changes: 2 additions & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export interface SvelteName extends BaseNode {
| SvelteStyleElement
| SvelteAttribute
| SvelteMemberExpressionName
| SvelteDirectiveKey
}

/** Nodes that may be used in component names. The component names separated by dots. */
Expand Down Expand Up @@ -414,7 +415,7 @@ export type SvelteDirective =
| SvelteTransitionDirective
export interface SvelteDirectiveKey extends BaseNode {
type: "SvelteDirectiveKey"
name: ESTree.Identifier
name: ESTree.Identifier | SvelteName
modifiers: string[]
parent: SvelteDirective
}
Expand Down
8 changes: 4 additions & 4 deletions src/context/let-directive-collection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { SvelteLetDirective, SvelteNode } from "../ast"
import type { SvelteLetDirective, SvelteName, SvelteNode } from "../ast"
import type * as ESTree from "estree"
import type { ScriptLetCallback, ScriptLetCallbackOption } from "./script-let"

/** A class that collects pattern nodes for Let directives. */
export class LetDirectiveCollection {
private readonly list: {
pattern: ESTree.Pattern
pattern: ESTree.Pattern | SvelteName
directive: SvelteLetDirective
typing: string
callbacks: ScriptLetCallback<ESTree.Pattern>[]
Expand All @@ -15,7 +15,7 @@ export class LetDirectiveCollection {
return this.list.length === 0
}

public getLetParams(): ESTree.Pattern[] {
public getLetParams(): (ESTree.Pattern | SvelteName)[] {
return this.list.map((d) => d.pattern)
}

Expand Down Expand Up @@ -43,7 +43,7 @@ export class LetDirectiveCollection {
}

public addPattern(
pattern: ESTree.Pattern,
pattern: ESTree.Pattern | SvelteName,
directive: SvelteLetDirective,
typing: string,
...callbacks: ScriptLetCallback<ESTree.Pattern>[]
Expand Down
12 changes: 9 additions & 3 deletions src/context/script-let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
Locations,
SvelteEachBlock,
SvelteIfBlock,
SvelteName,
SvelteNode,
Token,
} from "../ast"
Expand Down Expand Up @@ -50,6 +51,11 @@ function getNodeRange(
end: number
leadingComments?: Comment[]
trailingComments?: Comment[]
}
| {
range: [number, number]
leadingComments?: Comment[]
trailingComments?: Comment[]
},
): [number, number] {
let start = null
Expand Down Expand Up @@ -98,7 +104,7 @@ export class ScriptLetContext {
}

public addExpression<E extends ESTree.Expression>(
expression: E,
expression: E | SvelteName,
parent: SvelteNode,
typing?: string | null,
...callbacks: ScriptLetCallback<E>[]
Expand Down Expand Up @@ -300,7 +306,7 @@ export class ScriptLetContext {

public nestBlock(
block: SvelteNode,
params: ESTree.Pattern[],
params: (ESTree.Pattern | SvelteName)[],
parents: SvelteNode[],
callback: (
nodes: ESTree.Pattern[],
Expand All @@ -311,7 +317,7 @@ export class ScriptLetContext {

public nestBlock(
block: SvelteNode,
params?: ESTree.Pattern[],
params?: (ESTree.Pattern | SvelteName)[],
parents?: SvelteNode[],
callback?: (
nodes: ESTree.Pattern[],
Expand Down
29 changes: 20 additions & 9 deletions src/parser/converts/attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
SvelteSpreadAttribute,
SvelteTransitionDirective,
SvelteStartTag,
SvelteName,
} from "../../ast"
import type ESTree from "estree"
import type { Context } from "../../context"
Expand Down Expand Up @@ -399,9 +400,12 @@ function processDirective<
node: D & { expression: null | E },
directive: S,
ctx: Context,
processExpression: (expression: E) => ScriptLetCallback<NonNullable<E>>[],
processExpression: (
expression: E,
shorthand: boolean,
) => ScriptLetCallback<NonNullable<E>>[],
processName?: (
expression: ESTree.Identifier,
expression: SvelteName,
) => ScriptLetCallback<ESTree.Identifier>[],
) {
const colonIndex = ctx.code.indexOf(":", directive.range[0])
Expand Down Expand Up @@ -444,15 +448,22 @@ function processDirective<
keyEndIndex = nextEnd
}

let isShorthand = false
let isShorthandExpression = false

if (node.expression) {
isShorthand =
isShorthandExpression =
node.expression.type === "Identifier" &&
node.expression.name === node.name &&
getWithLoc(node.expression).start === nameRange.start &&
getWithLoc(node.expression).end === nameRange.end
processExpression(node.expression).push((es) => {
getWithLoc(node.expression).start === nameRange.start
if (
isShorthandExpression &&
getWithLoc(node.expression).end !== nameRange.end
) {
// The identifier location may be incorrect in some edge cases.
// e.g. bind:value=""
getWithLoc(node.expression).end = nameRange.end
}
processExpression(node.expression, isShorthandExpression).push((es) => {
directive.expression = es
})
}
Expand All @@ -467,12 +478,12 @@ function processDirective<

// put name
key.name = {
type: "Identifier",
type: "SvelteName",
name: node.name,
parent: key,
...ctx.getConvertLocation(nameRange),
}
if (!isShorthand) {
if (!isShorthandExpression) {
if (processName) {
processName(key.name).push((es) => {
key.name = es
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/parser/ast/blog/write-less-code01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
70,
Expand Down Expand Up @@ -530,7 +530,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
107,
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/parser/ast/class-directive01-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "bar",
"range": [
66,
Expand Down Expand Up @@ -434,7 +434,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "foo",
"range": [
94,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
154,
Expand Down Expand Up @@ -840,7 +840,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
208,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "eventname",
"range": [
8,
Expand Down Expand Up @@ -179,7 +179,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "eventname",
"range": [
39,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
96,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
11,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "submit",
"range": [
9,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
11,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
150,
Expand Down Expand Up @@ -584,7 +584,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "click",
"range": [
171,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "property",
"range": [
10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
12,
Expand Down Expand Up @@ -179,7 +179,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
41,
Expand Down Expand Up @@ -399,7 +399,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "checked",
"range": [
95,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
42,
Expand Down Expand Up @@ -215,7 +215,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
69,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
26,
Expand Down Expand Up @@ -291,7 +291,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "files",
"range": [
91,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"key": {
"type": "SvelteDirectiveKey",
"name": {
"type": "Identifier",
"type": "SvelteName",
"name": "value",
"range": [
22,
Expand Down
Loading