Skip to content

Commit 9e01170

Browse files
committed
support await
1 parent d7d0449 commit 9e01170

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

src/rules/valid-context-access.ts

+57-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRule } from "../utils"
22
import { extractContextReferences } from "./reference-helpers/svelte-context"
33
import { extractSvelteLifeCycleReferences } from "./reference-helpers/svelte-lifecycle"
4+
import type { AST } from "svelte-eslint-parser"
45
import type { TSESTree } from "@typescript-eslint/types"
56

67
export default createRule("valid-context-access", {
@@ -30,19 +31,20 @@ export default createRule("valid-context-access", {
3031
).map((r) => r.node)
3132

3233
// Extract <script> blocks that is not module=context.
33-
const scriptNotModuleElements = sourceCode.ast.body.filter((b) => {
34-
if (b.type !== "SvelteScriptElement") return false
35-
const isModule = b.startTag.attributes.some((a) => {
36-
return (
37-
a.type === "SvelteAttribute" &&
38-
a.key.name === "context" &&
39-
a.value.some(
40-
(v) => v.type === "SvelteLiteral" && v.value === "module",
34+
const scriptNotModuleElements: AST.SvelteScriptElement[] =
35+
sourceCode.ast.body.filter((b) => {
36+
if (b.type !== "SvelteScriptElement") return false
37+
const isModule = b.startTag.attributes.some((a) => {
38+
return (
39+
a.type === "SvelteAttribute" &&
40+
a.key.name === "context" &&
41+
a.value.some(
42+
(v) => v.type === "SvelteLiteral" && v.value === "module",
43+
)
4144
)
42-
)
43-
})
44-
return !isModule
45-
})
45+
})
46+
return !isModule
47+
}) as AST.SvelteScriptElement[]
4648

4749
const scopeManager = sourceCode.scopeManager
4850
const toplevelScope =
@@ -78,20 +80,44 @@ export default createRule("valid-context-access", {
7880
return []
7981
}
8082

83+
/** Return true if tnodeA is inside of nodeB. */
84+
function isInsideOfNodeB(
85+
nodeA: TSESTree.Node | AST.SvelteScriptElement,
86+
nodeB: TSESTree.Node,
87+
) {
88+
return (
89+
nodeA.range[0] <= nodeB.range[0] && nodeB.range[1] <= nodeA.range[1]
90+
)
91+
}
92+
8193
/** Return true if the node is there inside of <script> block that is not module=context. */
8294
function isInsideOfSvelteScriptElement(node: TSESTree.Node) {
8395
for (const script of scriptNotModuleElements) {
84-
if (
85-
node.range[0] >= script.range[0] &&
86-
node.range[1] <= script.range[1]
87-
) {
96+
if (isInsideOfNodeB(script, node)) {
8897
return true
8998
}
9099
}
91100
return false
92101
}
93102

94-
const awaitExpressions: TSESTree.AwaitExpression[] = []
103+
const awaitExpressions: {
104+
belongingFunction:
105+
| TSESTree.FunctionDeclaration
106+
| TSESTree.VariableDeclaration
107+
node: TSESTree.Node
108+
}[] = []
109+
110+
/** Return true if the given node is later than the await expression. */
111+
function isAfterAwait(node: TSESTree.CallExpression) {
112+
for (const awaitExpression of awaitExpressions) {
113+
const { belongingFunction, node: awaitNode } = awaitExpression
114+
if (isInsideOfNodeB(node, belongingFunction)) {
115+
continue
116+
}
117+
return awaitNode.range[0] <= node.range[0]
118+
}
119+
return false
120+
}
95121

96122
/** Let's lint! */
97123
function doLint(
@@ -105,6 +131,11 @@ export default createRule("valid-context-access", {
105131
return
106132
}
107133

134+
if (isAfterAwait(currentNode)) {
135+
report(contextCallExpression)
136+
return
137+
}
138+
108139
let { parent } = currentNode
109140
while (parent) {
110141
parent = parent.parent
@@ -151,7 +182,15 @@ export default createRule("valid-context-access", {
151182
}
152183
},
153184
AwaitExpression(node) {
154-
awaitExpressions.push(node)
185+
let parent: TSESTree.Node | undefined = node.parent
186+
while (parent) {
187+
if (parent.type === "FunctionDeclaration") {
188+
awaitExpressions.push({ belongingFunction: parent, node })
189+
} else if (parent.type === "VariableDeclaration") {
190+
awaitExpressions.push({ belongingFunction: parent, node })
191+
}
192+
parent = parent.parent
193+
}
155194
},
156195
}
157196
},

0 commit comments

Comments
 (0)