From 34c1aab9e25058532b046cd6a19b2b297b84facd Mon Sep 17 00:00:00 2001 From: zcf0508 Date: Fri, 12 Jul 2024 18:07:56 +0800 Subject: [PATCH] test: add cases --- fixtures/vue/options-base-2.vue | 120 ++++++ fixtures/vue/options-base-jsx.vue | 2 +- fixtures/vue/options-base-jsx2.vue | 60 +++ fixtures/vue/options-base.vue | 11 +- fixtures/vue/options-setup.vue | 34 +- fixtures/vue/setup-block.vue | 43 ++ test/fixtures.test.ts | 2 +- test/output/vue/options-base-2.vue.graph.txt | 138 ++++++ test/output/vue/options-base-2.vue.nodes.txt | 7 + .../output/vue/options-base-jsx.vue.graph.txt | 4 +- .../vue/options-base-jsx2.vue.graph.txt | 102 +++++ .../vue/options-base-jsx2.vue.nodes.txt | 4 + test/output/vue/options-base.vue.graph.txt | 29 +- .../vue/options-setup-render.vue.graph.txt | 195 +++++++++ .../vue/options-setup-render.vue.nodes.txt | 3 + test/output/vue/options-setup.vue.graph.txt | 216 ++++++++-- test/output/vue/setup-block.vue.graph.txt | 397 ++++++++++++++++-- test/output/vue/setup-block.vue.nodes.txt | 2 + test/suggest/filter.test.ts | 8 +- test/suggest/index.spec.ts | 360 ++++++++++++++++ 20 files changed, 1646 insertions(+), 91 deletions(-) create mode 100644 fixtures/vue/options-base-2.vue create mode 100644 fixtures/vue/options-base-jsx2.vue create mode 100644 test/output/vue/options-base-2.vue.graph.txt create mode 100644 test/output/vue/options-base-2.vue.nodes.txt create mode 100644 test/output/vue/options-base-jsx2.vue.graph.txt create mode 100644 test/output/vue/options-base-jsx2.vue.nodes.txt create mode 100644 test/output/vue/options-setup-render.vue.graph.txt create mode 100644 test/output/vue/options-setup-render.vue.nodes.txt create mode 100644 test/suggest/index.spec.ts diff --git a/fixtures/vue/options-base-2.vue b/fixtures/vue/options-base-2.vue new file mode 100644 index 0000000..32b42a5 --- /dev/null +++ b/fixtures/vue/options-base-2.vue @@ -0,0 +1,120 @@ + + + + + \ No newline at end of file diff --git a/fixtures/vue/options-base-jsx.vue b/fixtures/vue/options-base-jsx.vue index 3faedc1..acf208e 100644 --- a/fixtures/vue/options-base-jsx.vue +++ b/fixtures/vue/options-base-jsx.vue @@ -24,7 +24,7 @@ }, add() { counterStore.add(Number(this.number)); - this.add(); + this.plus(); } }, provide() { diff --git a/fixtures/vue/options-base-jsx2.vue b/fixtures/vue/options-base-jsx2.vue new file mode 100644 index 0000000..abc634c --- /dev/null +++ b/fixtures/vue/options-base-jsx2.vue @@ -0,0 +1,60 @@ + + + \ No newline at end of file diff --git a/fixtures/vue/options-base.vue b/fixtures/vue/options-base.vue index 6522464..18ac786 100644 --- a/fixtures/vue/options-base.vue +++ b/fixtures/vue/options-base.vue @@ -88,9 +88,16 @@ }, } }, + provide() { + return { + count: this.count, + number: this.number + } + }, created() { - console.log(this.count) - } + this.add() + console.log(this.number) + }, } diff --git a/fixtures/vue/options-setup.vue b/fixtures/vue/options-setup.vue index 96c4ca9..39492b5 100644 --- a/fixtures/vue/options-setup.vue +++ b/fixtures/vue/options-setup.vue @@ -64,35 +64,59 @@ export default { }, setup() { const data = reactive({ + ...{ + a: 1 + }, + a: 2, /** 这是注释 */number: 0, }); const count = computed(() => counterStore.count); + const dataNumber = computed(() => data.number); + + const number = count.value + const methods = { + ...{ + a: 1 + }, + a: 2, plus: ()=> { counterStore.increment(); methods.plus(); }, - add() { - counterStore.add(Number(data.number)); - methods.add(); - console.log(count) - } }; + function add() { + counterStore.add(Number(data.number)); + methods.plus(); + console.log(count) + return count; + } + + const a = {count}; const b = {count: count.value}; + + const [userinfo, setUserinfo] = useUserinfo(a, b.count, data.number) + + const { userinfo2 } = useUserinfo2(a, b.count, data.number) const ComponentD = () => (<>hello d); const msgRef = ref(); return { ...toRefs(data), + number, b, c: count, + dataNumber, ...methods, ComponentD, msgRef, + add, + userinfo, + userinfo2, } } diff --git a/fixtures/vue/setup-block.vue b/fixtures/vue/setup-block.vue index 365e1d0..5bc95ac 100644 --- a/fixtures/vue/setup-block.vue +++ b/fixtures/vue/setup-block.vue @@ -1,4 +1,6 @@ diff --git a/test/fixtures.test.ts b/test/fixtures.test.ts index fbf697f..20f8c2a 100644 --- a/test/fixtures.test.ts +++ b/test/fixtures.test.ts @@ -1,5 +1,6 @@ import { readFileSync } from 'node:fs'; import { basename } from 'node:path'; +import { inspect } from 'node:util'; import fg from 'fast-glob'; import { analyzeOptions, analyzeSetupScript, analyzeStyle, analyzeTemplate, analyzeTsx, parse } from '@/index'; @@ -13,7 +14,6 @@ describe('fixtures', async () => { const source = readFileSync(test, 'utf-8'); if (framework === 'vue') { const sfc = parse(source); - const nodesUsedInStyle = analyzeStyle(sfc.descriptor.styles || []); if (test.includes('tsx')) { diff --git a/test/output/vue/options-base-2.vue.graph.txt b/test/output/vue/options-base-2.vue.graph.txt new file mode 100644 index 0000000..5aff7be --- /dev/null +++ b/test/output/vue/options-base-2.vue.graph.txt @@ -0,0 +1,138 @@ +{ + "edges": Map { + { + "info": { + "column": 8, + "line": 65, + "used": Set { + "provide", + "created", + }, + }, + "label": "number", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 69, + "used": Set { + "provide", + "watch", + }, + }, + "label": "count", + "type": "var", + } => Set { + { + "info": { + "column": 8, + "line": 65, + "used": Set { + "provide", + "created", + }, + }, + "label": "number", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 74, + }, + "label": "plus", + "type": "fun", + } => Set { + { + "info": { + "column": 6, + "line": 74, + }, + "label": "plus", + "type": "fun", + }, + }, + { + "info": { + "column": 6, + "line": 78, + "used": Set { + "created", + }, + }, + "label": "add", + "type": "fun", + } => Set { + { + "info": { + "column": 8, + "line": 65, + "used": Set { + "provide", + "created", + }, + }, + "label": "number", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 78, + "used": Set { + "created", + }, + }, + "label": "add", + "type": "fun", + }, + }, + }, + "nodes": Set { + { + "info": { + "column": 8, + "line": 65, + "used": Set { + "provide", + "created", + }, + }, + "label": "number", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 69, + "used": Set { + "provide", + "watch", + }, + }, + "label": "count", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 74, + }, + "label": "plus", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 78, + "used": Set { + "created", + }, + }, + "label": "add", + "type": "fun", + }, + }, +} \ No newline at end of file diff --git a/test/output/vue/options-base-2.vue.nodes.txt b/test/output/vue/options-base-2.vue.nodes.txt new file mode 100644 index 0000000..9513b55 --- /dev/null +++ b/test/output/vue/options-base-2.vue.nodes.txt @@ -0,0 +1,7 @@ +Set { + "msg", + "plus", + "count", + "number", + "add", +} \ No newline at end of file diff --git a/test/output/vue/options-base-jsx.vue.graph.txt b/test/output/vue/options-base-jsx.vue.graph.txt index 4a59e2f..8d34d82 100644 --- a/test/output/vue/options-base-jsx.vue.graph.txt +++ b/test/output/vue/options-base-jsx.vue.graph.txt @@ -55,9 +55,9 @@ { "info": { "column": 6, - "line": 24, + "line": 20, }, - "label": "add", + "label": "plus", "type": "fun", }, }, diff --git a/test/output/vue/options-base-jsx2.vue.graph.txt b/test/output/vue/options-base-jsx2.vue.graph.txt new file mode 100644 index 0000000..4a59e2f --- /dev/null +++ b/test/output/vue/options-base-jsx2.vue.graph.txt @@ -0,0 +1,102 @@ +{ + "edges": Map { + { + "info": { + "column": 8, + "line": 11, + }, + "label": "number", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 15, + "used": Set { + "provide", + }, + }, + "label": "count", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 20, + }, + "label": "plus", + "type": "fun", + } => Set { + { + "info": { + "column": 6, + "line": 20, + }, + "label": "plus", + "type": "fun", + }, + }, + { + "info": { + "column": 6, + "line": 24, + }, + "label": "add", + "type": "fun", + } => Set { + { + "info": { + "column": 8, + "line": 11, + }, + "label": "number", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 24, + }, + "label": "add", + "type": "fun", + }, + }, + }, + "nodes": Set { + { + "info": { + "column": 8, + "line": 11, + }, + "label": "number", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 15, + "used": Set { + "provide", + }, + }, + "label": "count", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 20, + }, + "label": "plus", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 24, + }, + "label": "add", + "type": "fun", + }, + }, +} \ No newline at end of file diff --git a/test/output/vue/options-base-jsx2.vue.nodes.txt b/test/output/vue/options-base-jsx2.vue.nodes.txt new file mode 100644 index 0000000..2597686 --- /dev/null +++ b/test/output/vue/options-base-jsx2.vue.nodes.txt @@ -0,0 +1,4 @@ +Set { + "count", + "plus", +} \ No newline at end of file diff --git a/test/output/vue/options-base.vue.graph.txt b/test/output/vue/options-base.vue.graph.txt index 1b126db..5881cc3 100644 --- a/test/output/vue/options-base.vue.graph.txt +++ b/test/output/vue/options-base.vue.graph.txt @@ -4,6 +4,10 @@ "info": { "column": 8, "line": 65, + "used": Set { + "provide", + "created", + }, }, "label": "number", "type": "var", @@ -14,7 +18,7 @@ "line": 69, "used": Set { "watch", - "created", + "provide", }, }, "label": "count", @@ -24,6 +28,10 @@ "info": { "column": 8, "line": 65, + "used": Set { + "provide", + "created", + }, }, "label": "number", "type": "var", @@ -50,6 +58,9 @@ "info": { "column": 6, "line": 78, + "used": Set { + "created", + }, }, "label": "add", "type": "fun", @@ -58,6 +69,10 @@ "info": { "column": 8, "line": 65, + "used": Set { + "provide", + "created", + }, }, "label": "number", "type": "var", @@ -66,6 +81,9 @@ "info": { "column": 6, "line": 78, + "used": Set { + "created", + }, }, "label": "add", "type": "fun", @@ -77,6 +95,10 @@ "info": { "column": 8, "line": 65, + "used": Set { + "provide", + "created", + }, }, "label": "number", "type": "var", @@ -87,7 +109,7 @@ "line": 69, "used": Set { "watch", - "created", + "provide", }, }, "label": "count", @@ -105,6 +127,9 @@ "info": { "column": 6, "line": 78, + "used": Set { + "created", + }, }, "label": "add", "type": "fun", diff --git a/test/output/vue/options-setup-render.vue.graph.txt b/test/output/vue/options-setup-render.vue.graph.txt new file mode 100644 index 0000000..9f4074b --- /dev/null +++ b/test/output/vue/options-setup-render.vue.graph.txt @@ -0,0 +1,195 @@ +{ + "edges": Map { + { + "info": { + "column": 10, + "line": 10, + }, + "label": "data", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 13, + }, + "label": "number", + "type": "var", + }, + }, + { + "info": { + "column": 10, + "line": 13, + }, + "label": "number", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 10, + }, + "label": "data", + "type": "var", + }, + }, + { + "info": { + "column": 10, + "line": 14, + }, + "label": "count", + "type": "var", + } => Set {}, + { + "info": { + "column": 10, + "line": 16, + }, + "label": "methods", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 16, + }, + "label": "methods", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 10, + }, + "label": "data", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 14, + }, + "label": "count", + "type": "var", + }, + }, + { + "info": { + "column": 10, + "line": 29, + }, + "label": "a", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 14, + }, + "label": "count", + "type": "var", + }, + }, + { + "info": { + "column": 10, + "line": 30, + }, + "label": "b", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 14, + }, + "label": "count", + "type": "var", + }, + }, + { + "info": { + "column": 10, + "line": 31, + }, + "label": "ComponentD", + "type": "fun", + } => Set {}, + { + "info": { + "column": 10, + "line": 33, + }, + "label": "msgRef", + "type": "var", + } => Set {}, + }, + "nodes": Set { + { + "info": { + "column": 10, + "line": 10, + }, + "label": "data", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 13, + }, + "label": "number", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 14, + }, + "label": "count", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 16, + }, + "label": "methods", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 29, + }, + "label": "a", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 30, + }, + "label": "b", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 31, + }, + "label": "ComponentD", + "type": "fun", + }, + { + "info": { + "column": 10, + "line": 33, + }, + "label": "msgRef", + "type": "var", + }, + }, +} \ No newline at end of file diff --git a/test/output/vue/options-setup-render.vue.nodes.txt b/test/output/vue/options-setup-render.vue.nodes.txt new file mode 100644 index 0000000..c5f1270 --- /dev/null +++ b/test/output/vue/options-setup-render.vue.nodes.txt @@ -0,0 +1,3 @@ +Set { + "count", +} \ No newline at end of file diff --git a/test/output/vue/options-setup.vue.graph.txt b/test/output/vue/options-setup.vue.graph.txt index 1c09b18..3cd55b9 100644 --- a/test/output/vue/options-setup.vue.graph.txt +++ b/test/output/vue/options-setup.vue.graph.txt @@ -1,18 +1,44 @@ { "edges": Map { + { + "info": { + "column": 6, + "line": 69, + }, + "label": "a", + "type": "var", + } => Set { + { + "info": { + "column": 10, + "line": 72, + }, + "label": "count", + "type": "var", + }, + }, { "info": { "column": 17, "comment": "这是注释", - "line": 66, + "line": 70, }, "label": "number", "type": "var", - } => Set {}, + } => Set { + { + "info": { + "column": 10, + "line": 72, + }, + "label": "count", + "type": "var", + }, + }, { "info": { "column": 10, - "line": 83, + "line": 98, }, "label": "b", "type": "var", @@ -20,7 +46,7 @@ { "info": { "column": 10, - "line": 68, + "line": 72, }, "label": "count", "type": "var", @@ -29,7 +55,7 @@ { "info": { "column": 10, - "line": 68, + "line": 72, }, "label": "count", "type": "var", @@ -37,7 +63,7 @@ { "info": { "column": 6, - "line": 91, + "line": 111, }, "label": "c", "type": "var", @@ -45,16 +71,34 @@ { "info": { "column": 10, - "line": 68, + "line": 72, }, "label": "count", "type": "var", }, }, + { + "info": { + "column": 10, + "line": 74, + }, + "label": "dataNumber", + "type": "var", + } => Set { + { + "info": { + "column": 17, + "comment": "这是注释", + "line": 70, + }, + "label": "number", + "type": "var", + }, + }, { "info": { "column": 6, - "line": 71, + "line": 83, }, "label": "plus", "type": "fun", @@ -62,7 +106,7 @@ { "info": { "column": 6, - "line": 71, + "line": 83, }, "label": "plus", "type": "fun", @@ -70,8 +114,24 @@ }, { "info": { - "column": 6, - "line": 75, + "column": 10, + "line": 103, + }, + "label": "ComponentD", + "type": "fun", + } => Set {}, + { + "info": { + "column": 10, + "line": 105, + }, + "label": "msgRef", + "type": "var", + } => Set {}, + { + "info": { + "column": 13, + "line": 89, }, "label": "add", "type": "fun", @@ -80,7 +140,7 @@ "info": { "column": 17, "comment": "这是注释", - "line": 66, + "line": 70, }, "label": "number", "type": "var", @@ -88,15 +148,15 @@ { "info": { "column": 6, - "line": 75, + "line": 83, }, - "label": "add", + "label": "plus", "type": "fun", }, { "info": { "column": 10, - "line": 68, + "line": 72, }, "label": "count", "type": "var", @@ -104,27 +164,87 @@ }, { "info": { - "column": 10, - "line": 84, + "column": 11, + "line": 100, }, - "label": "ComponentD", - "type": "fun", - } => Set {}, + "label": "userinfo", + "type": "var", + } => Set { + { + "info": { + "column": 6, + "line": 69, + }, + "label": "a", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 98, + }, + "label": "b", + "type": "var", + }, + { + "info": { + "column": 17, + "comment": "这是注释", + "line": 70, + }, + "label": "number", + "type": "var", + }, + }, { "info": { - "column": 10, - "line": 86, + "column": 12, + "line": 102, }, - "label": "msgRef", + "label": "userinfo2", "type": "var", - } => Set {}, + } => Set { + { + "info": { + "column": 6, + "line": 69, + }, + "label": "a", + "type": "var", + }, + { + "info": { + "column": 10, + "line": 98, + }, + "label": "b", + "type": "var", + }, + { + "info": { + "column": 17, + "comment": "这是注释", + "line": 70, + }, + "label": "number", + "type": "var", + }, + }, }, "nodes": Set { + { + "info": { + "column": 6, + "line": 69, + }, + "label": "a", + "type": "var", + }, { "info": { "column": 17, "comment": "这是注释", - "line": 66, + "line": 70, }, "label": "number", "type": "var", @@ -132,7 +252,7 @@ { "info": { "column": 10, - "line": 83, + "line": 98, }, "label": "b", "type": "var", @@ -140,7 +260,7 @@ { "info": { "column": 10, - "line": 68, + "line": 72, }, "label": "count", "type": "var", @@ -148,31 +268,31 @@ { "info": { "column": 6, - "line": 91, + "line": 111, }, "label": "c", "type": "var", }, { "info": { - "column": 6, - "line": 71, + "column": 10, + "line": 74, }, - "label": "plus", - "type": "fun", + "label": "dataNumber", + "type": "var", }, { "info": { "column": 6, - "line": 75, + "line": 83, }, - "label": "add", + "label": "plus", "type": "fun", }, { "info": { "column": 10, - "line": 84, + "line": 103, }, "label": "ComponentD", "type": "fun", @@ -180,10 +300,34 @@ { "info": { "column": 10, - "line": 86, + "line": 105, }, "label": "msgRef", "type": "var", }, + { + "info": { + "column": 13, + "line": 89, + }, + "label": "add", + "type": "fun", + }, + { + "info": { + "column": 11, + "line": 100, + }, + "label": "userinfo", + "type": "var", + }, + { + "info": { + "column": 12, + "line": 102, + }, + "label": "userinfo2", + "type": "var", + }, }, } \ No newline at end of file diff --git a/test/output/vue/setup-block.vue.graph.txt b/test/output/vue/setup-block.vue.graph.txt index daa3f5d..ac608ad 100644 --- a/test/output/vue/setup-block.vue.graph.txt +++ b/test/output/vue/setup-block.vue.graph.txt @@ -4,7 +4,7 @@ "info": { "column": 6, "comment": "route", - "line": 7, + "line": 9, }, "label": "route", "type": "var", @@ -13,7 +13,7 @@ "info": { "column": 6, "comment": "path", - "line": 9, + "line": 11, "used": Set { "watch", }, @@ -25,7 +25,7 @@ "info": { "column": 6, "comment": "route", - "line": 7, + "line": 9, }, "label": "route", "type": "var", @@ -34,7 +34,7 @@ { "info": { "column": 6, - "line": 11, + "line": 13, "used": Set { "watch", }, @@ -46,7 +46,7 @@ "info": { "column": 6, "comment": "path", - "line": 9, + "line": 11, "used": Set { "watch", }, @@ -59,7 +59,7 @@ "info": { "column": 6, "comment": "这是注释11", - "line": 19, + "line": 21, }, "label": "data", "type": "var", @@ -67,7 +67,7 @@ { "info": { "column": 6, - "line": 23, + "line": 25, }, "label": "age", "type": "var", @@ -75,7 +75,7 @@ { "info": { "column": 10, - "line": 25, + "line": 27, }, "label": "restArr", "type": "var", @@ -83,7 +83,7 @@ { "info": { "column": 11, - "line": 26, + "line": 28, }, "label": "restObj", "type": "var", @@ -91,7 +91,7 @@ { "info": { "column": 9, - "line": 28, + "line": 30, }, "label": "addAge", "type": "fun", @@ -99,7 +99,7 @@ { "info": { "column": 6, - "line": 23, + "line": 25, }, "label": "age", "type": "var", @@ -108,7 +108,7 @@ { "info": { "column": 6, - "line": 32, + "line": 34, }, "label": "updateName", "type": "fun", @@ -117,7 +117,7 @@ "info": { "column": 6, "comment": "这是注释11", - "line": 19, + "line": 21, }, "label": "data", "type": "var", @@ -126,7 +126,7 @@ { "info": { "column": 9, - "line": 36, + "line": 38, }, "label": "funA", "type": "fun", @@ -134,7 +134,7 @@ { "info": { "column": 8, - "line": 43, + "line": 45, }, "label": "varB", "type": "var", @@ -142,17 +142,51 @@ { "info": { "column": 9, - "line": 36, + "line": 38, }, "label": "funA", "type": "fun", }, }, + { + "info": { + "column": 6, + "line": 47, + }, + "label": "varB2", + "type": "var", + } => Set { + { + "info": { + "column": 8, + "line": 45, + }, + "label": "varB", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 49, + }, + "label": "ddd", + "type": "var", + } => Set { + { + "info": { + "column": 8, + "line": 45, + }, + "label": "varB", + "type": "var", + }, + }, { "info": { "column": 9, "comment": "这是注释", - "line": 45, + "line": 54, "used": Set { "onMounted", "provide", @@ -165,7 +199,7 @@ "info": { "column": 9, "comment": "这是注释", - "line": 45, + "line": 54, "used": Set { "onMounted", "provide", @@ -178,7 +212,7 @@ { "info": { "column": 8, - "line": 52, + "line": 61, }, "label": "varD", "type": "var", @@ -187,7 +221,7 @@ "info": { "column": 9, "comment": "这是注释", - "line": 45, + "line": 54, "used": Set { "onMounted", "provide", @@ -199,7 +233,7 @@ { "info": { "column": 8, - "line": 43, + "line": 45, }, "label": "varB", "type": "var", @@ -208,7 +242,7 @@ { "info": { "column": 14, - "line": 52, + "line": 61, }, "label": "varE", "type": "var", @@ -217,7 +251,7 @@ "info": { "column": 9, "comment": "这是注释", - "line": 45, + "line": 54, "used": Set { "onMounted", "provide", @@ -229,19 +263,194 @@ { "info": { "column": 8, - "line": 43, + "line": 45, }, "label": "varB", "type": "var", }, }, + { + "info": { + "column": 7, + "line": 73, + }, + "label": "userinfo", + "type": "var", + } => Set {}, + { + "info": { + "column": 17, + "line": 73, + }, + "label": "setUserinfo", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 76, + }, + "label": "aaa1", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 78, + }, + "label": "bbb2", + "type": "var", + } => Set { + { + "info": { + "column": 6, + "line": 76, + }, + "label": "aaa1", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 80, + }, + "label": "cc333", + "type": "var", + } => Set { + { + "info": { + "column": 6, + "line": 78, + }, + "label": "bbb2", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 82, + }, + "label": "updateBBB", + "type": "fun", + } => Set { + { + "info": { + "column": 6, + "line": 80, + }, + "label": "cc333", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 76, + }, + "label": "aaa1", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 49, + }, + "label": "ddd", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 88, + }, + "label": "ddd4", + "type": "var", + } => Set {}, + { + "info": { + "column": 6, + "line": 89, + }, + "label": "eee5", + "type": "var", + } => Set { + { + "info": { + "column": 6, + "line": 88, + }, + "label": "ddd4", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 91, + }, + "label": "add1111", + "type": "var", + } => Set {}, + { + "info": { + "column": 9, + "line": 93, + }, + "label": "add2222", + "type": "fun", + } => Set { + { + "info": { + "column": 6, + "line": 91, + }, + "label": "add1111", + "type": "var", + }, + }, + { + "info": { + "column": 6, + "line": 97, + }, + "label": "add333", + "type": "fun", + } => Set { + { + "info": { + "column": 9, + "line": 93, + }, + "label": "add2222", + "type": "fun", + }, + }, + { + "info": { + "column": 6, + "line": 101, + }, + "label": "add4444", + "type": "fun", + } => Set { + { + "info": { + "column": 6, + "line": 97, + }, + "label": "add333", + "type": "fun", + }, + }, }, "nodes": Set { { "info": { "column": 6, "comment": "route", - "line": 7, + "line": 9, }, "label": "route", "type": "var", @@ -250,7 +459,7 @@ "info": { "column": 6, "comment": "path", - "line": 9, + "line": 11, "used": Set { "watch", }, @@ -261,7 +470,7 @@ { "info": { "column": 6, - "line": 11, + "line": 13, "used": Set { "watch", }, @@ -273,7 +482,7 @@ "info": { "column": 6, "comment": "这是注释11", - "line": 19, + "line": 21, }, "label": "data", "type": "var", @@ -281,7 +490,7 @@ { "info": { "column": 6, - "line": 23, + "line": 25, }, "label": "age", "type": "var", @@ -289,7 +498,7 @@ { "info": { "column": 10, - "line": 25, + "line": 27, }, "label": "restArr", "type": "var", @@ -297,7 +506,7 @@ { "info": { "column": 11, - "line": 26, + "line": 28, }, "label": "restObj", "type": "var", @@ -305,7 +514,7 @@ { "info": { "column": 9, - "line": 28, + "line": 30, }, "label": "addAge", "type": "fun", @@ -313,7 +522,7 @@ { "info": { "column": 6, - "line": 32, + "line": 34, }, "label": "updateName", "type": "fun", @@ -321,7 +530,7 @@ { "info": { "column": 9, - "line": 36, + "line": 38, }, "label": "funA", "type": "fun", @@ -329,16 +538,32 @@ { "info": { "column": 8, - "line": 43, + "line": 45, }, "label": "varB", "type": "var", }, + { + "info": { + "column": 6, + "line": 47, + }, + "label": "varB2", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 49, + }, + "label": "ddd", + "type": "var", + }, { "info": { "column": 9, "comment": "这是注释", - "line": 45, + "line": 54, "used": Set { "onMounted", "provide", @@ -350,7 +575,7 @@ { "info": { "column": 8, - "line": 52, + "line": 61, }, "label": "varD", "type": "var", @@ -358,10 +583,106 @@ { "info": { "column": 14, - "line": 52, + "line": 61, }, "label": "varE", "type": "var", }, + { + "info": { + "column": 7, + "line": 73, + }, + "label": "userinfo", + "type": "var", + }, + { + "info": { + "column": 17, + "line": 73, + }, + "label": "setUserinfo", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 76, + }, + "label": "aaa1", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 78, + }, + "label": "bbb2", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 80, + }, + "label": "cc333", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 82, + }, + "label": "updateBBB", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 88, + }, + "label": "ddd4", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 89, + }, + "label": "eee5", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 91, + }, + "label": "add1111", + "type": "var", + }, + { + "info": { + "column": 9, + "line": 93, + }, + "label": "add2222", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 97, + }, + "label": "add333", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 101, + }, + "label": "add4444", + "type": "fun", + }, }, } \ No newline at end of file diff --git a/test/output/vue/setup-block.vue.nodes.txt b/test/output/vue/setup-block.vue.nodes.txt index e1ba5db..c981d89 100644 --- a/test/output/vue/setup-block.vue.nodes.txt +++ b/test/output/vue/setup-block.vue.nodes.txt @@ -2,7 +2,9 @@ Set { "lmsg", "path", "age", + "userinfo", "data", "updateName", "addAge", + "add4444", } \ No newline at end of file diff --git a/test/suggest/filter.test.ts b/test/suggest/filter.test.ts index 678f5a1..707db76 100644 --- a/test/suggest/filter.test.ts +++ b/test/suggest/filter.test.ts @@ -176,15 +176,15 @@ describe('suggest tests', () => { }; const node2: TypedNode = { label: 'node2', - type: NodeType.fun, + type: NodeType.var, }; const node3: TypedNode = { label: 'node3', - type: NodeType.fun, + type: NodeType.var, }; const node4: TypedNode = { label: 'node4', - type: NodeType.fun, + type: NodeType.var, }; const node5: TypedNode = { label: 'node4', @@ -197,7 +197,7 @@ describe('suggest tests', () => { graph.set(node5, new Set([node5])); expect(findLinearPaths(graph)).toEqual([ - [node2, node3], + [node1, node2, node3], ]); }); it('graph linear paths 2', () => { diff --git a/test/suggest/index.spec.ts b/test/suggest/index.spec.ts new file mode 100644 index 0000000..0e3210c --- /dev/null +++ b/test/suggest/index.spec.ts @@ -0,0 +1,360 @@ +import { readFileSync } from 'node:fs'; +import { analyzeSetupScript, analyzeStyle, analyzeTemplate, gen, parse } from '@/index'; + +const testFile = './fixtures/vue/setup-block.vue'; + +describe('suggest gen', () => { + const source = readFileSync(testFile, 'utf-8'); + const sfc = parse(source); + it('base', () => { + const graph = analyzeSetupScript( + sfc.descriptor.scriptSetup?.content || '', + (sfc.descriptor.scriptSetup?.loc.start.line || 1) - 1, + ); + + const nodesUsedInStyle = analyzeStyle(sfc.descriptor.styles || []); + + const nodesUsedInTemplate = sfc.descriptor.template?.content + ? analyzeTemplate(sfc.descriptor.template!.content) + : new Set(); + + expect(gen(graph, nodesUsedInTemplate, nodesUsedInStyle)).toMatchInlineSnapshot(` + [ + { + "message": "Nodes [eee5,ddd4] are not used, perhaps you can remove them.", + "nodeInfo": [ + { + "info": { + "column": 6, + "line": 89, + }, + "label": "eee5", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 88, + }, + "label": "ddd4", + "type": "var", + }, + ], + "type": "info", + }, + { + "message": "Nodes [route,path,lmsg] are isolated, perhaps you can refactor them to an isolated file.", + "nodeInfo": [ + { + "info": { + "column": 6, + "comment": "route", + "line": 9, + }, + "label": "route", + "type": "var", + }, + { + "info": { + "column": 6, + "comment": "path", + "line": 11, + "used": Set { + "watch", + }, + }, + "label": "path", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 13, + "used": Set { + "watch", + }, + }, + "label": "lmsg", + "type": "var", + }, + ], + "type": "info", + }, + { + "message": "Nodes [add2222,add1111,add333,add4444] are isolated, perhaps you can refactor them to an isolated file.", + "nodeInfo": [ + { + "info": { + "column": 9, + "line": 93, + }, + "label": "add2222", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 91, + }, + "label": "add1111", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 97, + }, + "label": "add333", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 101, + }, + "label": "add4444", + "type": "fun", + }, + ], + "type": "info", + }, + { + "message": "Nodes [varB2,varB,funA,ddd,varE,funC,bbb2,aaa1,cc333,updateBBB...(11)] are isolated, perhaps you can refactor them to an isolated file.", + "nodeInfo": [ + { + "info": { + "column": 6, + "line": 47, + }, + "label": "varB2", + "type": "var", + }, + { + "info": { + "column": 8, + "line": 45, + }, + "label": "varB", + "type": "var", + }, + { + "info": { + "column": 9, + "line": 38, + }, + "label": "funA", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 49, + }, + "label": "ddd", + "type": "var", + }, + { + "info": { + "column": 14, + "line": 61, + }, + "label": "varE", + "type": "var", + }, + { + "info": { + "column": 9, + "comment": "这是注释", + "line": 54, + "used": Set { + "onMounted", + "provide", + }, + }, + "label": "funC", + "type": "fun", + }, + { + "info": { + "column": 6, + "line": 78, + }, + "label": "bbb2", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 76, + }, + "label": "aaa1", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 80, + }, + "label": "cc333", + "type": "var", + }, + { + "info": { + "column": 6, + "line": 82, + }, + "label": "updateBBB", + "type": "fun", + }, + { + "info": { + "column": 8, + "line": 61, + }, + "label": "varD", + "type": "var", + }, + ], + "type": "info", + }, + { + "message": "There is a loop call in nodes [funC], perhaps you can refactor it.", + "nodeInfo": [ + { + "info": { + "column": 9, + "comment": "这是注释", + "line": 54, + "used": Set { + "onMounted", + "provide", + }, + }, + "label": "funC", + "type": "fun", + }, + ], + "type": "error", + }, + { + "message": "Nodes [varB2,varB] are have function chain calls, perhaps you can refactor it.", + "nodeInfo": [ + { + "info": { + "column": 6, + "line": 47, + }, + "label": "varB2", + "type": "var", + }, + { + "info": { + "column": 8, + "line": 45, + }, + "label": "varB", + "type": "var", + }, + ], + "type": "warning", + }, + { + "message": "Node [restArr] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 10, + "line": 27, + }, + "label": "restArr", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [restObj] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 11, + "line": 28, + }, + "label": "restObj", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [varB2] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 6, + "line": 47, + }, + "label": "varB2", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [varD] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 8, + "line": 61, + }, + "label": "varD", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [varE] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 14, + "line": 61, + }, + "label": "varE", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [setUserinfo] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 17, + "line": 73, + }, + "label": "setUserinfo", + "type": "var", + }, + "type": "info", + }, + { + "message": "Node [updateBBB] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 6, + "line": 82, + }, + "label": "updateBBB", + "type": "fun", + }, + "type": "info", + }, + { + "message": "Node [eee5] is not used, perhaps you can remove it.", + "nodeInfo": { + "info": { + "column": 6, + "line": 89, + }, + "label": "eee5", + "type": "var", + }, + "type": "info", + }, + ] + `); + }); +});