Skip to content

Commit

Permalink
importNodeRules -- fix import rule -- only both nodes are imported
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Sep 1, 2023
1 parent 3866c06 commit 656b884
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rename lookup to find

# articles

[docs] 反应网编程 -- 加入到 演算场 的连接
Expand Down
3 changes: 3 additions & 0 deletions src/lang/mod/RuleEntry.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { NodeWithoutId } from "../node"
import { Word } from "../word"
import { Mod } from "./Mod"

export type RuleEntry = {
name: string
first: NodeWithoutId
second: NodeWithoutId
mod: Mod
words: Array<Word>
}
14 changes: 13 additions & 1 deletion src/lang/mod/defineRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@ export function defineRule(
const key = `${firstKey} ${secondKey}`
const name = `${firstName} ${secondName}`

mod.ruleEntries.set(key, { name, mod, words })
mod.ruleEntries.set(key, {
name,
first: {
url: firstDefinition.mod.url,
name: firstDefinition.name,
},
second: {
url: secondDefinition.mod.url,
name: secondDefinition.name,
},
mod,
words,
})
}
6 changes: 5 additions & 1 deletion src/lang/mod/findNodeRuleEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NodeWithoutId } from "../node"
import { nodeKeyWithoutId } from "../node/nodeKeyWithoutId"
import { Mod } from "./Mod"
import { RuleEntry } from "./RuleEntry"
import { hasNodeDefinition } from "./hasNodeDefinition"

export function findNodeRuleEntries(
mod: Mod,
Expand All @@ -11,7 +12,10 @@ export function findNodeRuleEntries(
const entries = []
for (const [key, entry] of mod.ruleEntries) {
const [firstKey, secondKey] = key.split(" ")
if (firstKey === nodeKey || secondKey === nodeKey) {
if (
(firstKey === nodeKey && hasNodeDefinition(mod, entry.second)) ||
(secondKey === nodeKey && hasNodeDefinition(mod, entry.first))
) {
entries.push(entry)
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/lang/mod/hasNodeDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NodeWithoutId } from "../node"
import { Mod } from "./Mod"
import { lookupDefinition } from "./lookupDefinition"

export function hasNodeDefinition(mod: Mod, node: NodeWithoutId): boolean {
const definition = lookupDefinition(mod, node.name)
if (definition === undefined) {
return false
}

if (definition["@kind"] !== "NodeDefinition") {
return false
}

if (definition.mod.url.href !== mod.url.href) {
return false
}

return true
}

0 comments on commit 656b884

Please sign in to comment.