@nordcraft/search 1.0.38 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fixProject.js +32 -0
- package/dist/fixProject.js.map +1 -0
- package/dist/problems.worker.js +45 -8
- package/dist/problems.worker.js.map +1 -1
- package/dist/rules/actions/legacyActionRule.js +144 -2
- package/dist/rules/actions/legacyActionRule.js.map +1 -1
- package/dist/rules/actions/legacyActionRule.test.js +235 -1
- package/dist/rules/actions/legacyActionRule.test.js.map +1 -1
- package/dist/rules/components/noReferenceComponentRule.js +31 -17
- package/dist/rules/components/noReferenceComponentRule.js.map +1 -1
- package/dist/rules/components/noReferenceComponentRule.test.js +86 -1
- package/dist/rules/components/noReferenceComponentRule.test.js.map +1 -1
- package/dist/rules/formulas/legacyFormulaRule.js +602 -6
- package/dist/rules/formulas/legacyFormulaRule.js.map +1 -1
- package/dist/rules/formulas/legacyFormulaRule.test.js +232 -1
- package/dist/rules/formulas/legacyFormulaRule.test.js.map +1 -1
- package/dist/rules/formulas/noReferenceProjectFormulaRule.js +73 -58
- package/dist/rules/formulas/noReferenceProjectFormulaRule.js.map +1 -1
- package/dist/rules/formulas/noReferenceProjectFormulaRule.test.js +34 -1
- package/dist/rules/formulas/noReferenceProjectFormulaRule.test.js.map +1 -1
- package/dist/searchProject.js +338 -217
- package/dist/searchProject.js.map +1 -1
- package/dist/util/helpers.js +31 -5
- package/dist/util/helpers.js.map +1 -1
- package/dist/util/helpers.test.js +58 -0
- package/dist/util/helpers.test.js.map +1 -0
- package/package.json +3 -2
- package/src/fixProject.ts +47 -0
- package/src/problems.worker.ts +90 -12
- package/src/rules/actions/legacyActionRule.test.ts +245 -1
- package/src/rules/actions/legacyActionRule.ts +166 -4
- package/src/rules/components/noReferenceComponentRule.test.ts +87 -1
- package/src/rules/components/noReferenceComponentRule.ts +38 -23
- package/src/rules/formulas/legacyFormulaRule.test.ts +242 -1
- package/src/rules/formulas/legacyFormulaRule.ts +697 -10
- package/src/rules/formulas/noReferenceProjectFormulaRule.test.ts +36 -1
- package/src/rules/formulas/noReferenceProjectFormulaRule.ts +78 -64
- package/src/searchProject.ts +217 -98
- package/src/types.d.ts +20 -3
- package/src/util/helpers.test.ts +80 -0
- package/src/util/helpers.ts +61 -9
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { ProjectFiles } from '@nordcraft/ssr/dist/ssr.types'
|
|
1
2
|
import { describe, expect, test } from 'bun:test'
|
|
3
|
+
import { fixProject } from '../../fixProject'
|
|
2
4
|
import { searchProject } from '../../searchProject'
|
|
3
5
|
import { noReferenceProjectFormulaRule } from './noReferenceProjectFormulaRule'
|
|
4
6
|
|
|
5
|
-
describe('noReferenceFormulaRule', () => {
|
|
7
|
+
describe('find noReferenceFormulaRule', () => {
|
|
6
8
|
test('should detect unused global formulas', () => {
|
|
7
9
|
const problems = Array.from(
|
|
8
10
|
searchProject({
|
|
@@ -289,3 +291,36 @@ describe('noReferenceFormulaRule', () => {
|
|
|
289
291
|
expect(problems).toHaveLength(0)
|
|
290
292
|
})
|
|
291
293
|
})
|
|
294
|
+
|
|
295
|
+
describe('fix noReferenceFormulaRule', () => {
|
|
296
|
+
test('should remove unused global formulas', () => {
|
|
297
|
+
const projectFiles: ProjectFiles = {
|
|
298
|
+
formulas: {
|
|
299
|
+
'my-formula': {
|
|
300
|
+
name: 'my-formula',
|
|
301
|
+
arguments: [],
|
|
302
|
+
formula: {
|
|
303
|
+
type: 'value',
|
|
304
|
+
value: 'value',
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
components: {
|
|
309
|
+
test: {
|
|
310
|
+
name: 'test',
|
|
311
|
+
nodes: {},
|
|
312
|
+
formulas: {},
|
|
313
|
+
apis: {},
|
|
314
|
+
attributes: {},
|
|
315
|
+
variables: {},
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
}
|
|
319
|
+
const fixedProject = fixProject({
|
|
320
|
+
files: projectFiles,
|
|
321
|
+
rule: noReferenceProjectFormulaRule,
|
|
322
|
+
fixType: 'delete-project-formula',
|
|
323
|
+
})
|
|
324
|
+
expect(fixedProject.formulas).toEqual({})
|
|
325
|
+
})
|
|
326
|
+
})
|
|
@@ -1,92 +1,106 @@
|
|
|
1
1
|
import { ToddleComponent } from '@nordcraft/core/dist/component/ToddleComponent'
|
|
2
2
|
import type { Formula } from '@nordcraft/core/dist/formula/formula'
|
|
3
3
|
import { isToddleFormula } from '@nordcraft/core/dist/formula/formulaTypes'
|
|
4
|
+
import { omit } from '@nordcraft/core/dist/utils/collections'
|
|
4
5
|
import { ToddleApiService } from '@nordcraft/ssr/dist/ToddleApiService'
|
|
5
6
|
import { ToddleRoute } from '@nordcraft/ssr/dist/ToddleRoute'
|
|
6
|
-
import type { Rule } from '../../types'
|
|
7
|
+
import type { NodeType, Rule } from '../../types'
|
|
7
8
|
|
|
8
9
|
export const noReferenceProjectFormulaRule: Rule<void> = {
|
|
9
10
|
code: 'no-reference project formula',
|
|
10
11
|
level: 'warning',
|
|
11
12
|
category: 'No References',
|
|
12
|
-
visit: (report,
|
|
13
|
-
if (
|
|
13
|
+
visit: (report, details) => {
|
|
14
|
+
if (hasReferences(details)) {
|
|
14
15
|
return
|
|
15
16
|
}
|
|
17
|
+
report(details.path, undefined, ['delete-project-formula'])
|
|
18
|
+
},
|
|
19
|
+
fixes: {
|
|
20
|
+
'delete-project-formula': (data) => {
|
|
21
|
+
if (hasReferences(data)) {
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
return omit(data.files, data.path)
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
}
|
|
16
28
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
export type NoReferenceProjectFormulaRuleFix = 'delete-project-formula'
|
|
30
|
+
|
|
31
|
+
const hasReferences = ({ value, files, nodeType, memo }: NodeType) => {
|
|
32
|
+
if (nodeType !== 'project-formula' || value.exported === true) {
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Check in all API services first, since that should be quick
|
|
37
|
+
for (const apiService of Object.values(files.services ?? {})) {
|
|
38
|
+
const service = new ToddleApiService({
|
|
39
|
+
service: apiService,
|
|
40
|
+
globalFormulas: { formulas: files.formulas, packages: files.packages },
|
|
41
|
+
})
|
|
42
|
+
const formulas = service.formulasInService()
|
|
43
|
+
for (const { path: _formulaPath, formula } of formulas) {
|
|
44
|
+
// Check if the formula is used in the formula
|
|
45
|
+
if (checkFormula(formula, value.name)) {
|
|
46
|
+
return true
|
|
29
47
|
}
|
|
30
48
|
}
|
|
49
|
+
}
|
|
31
50
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
51
|
+
// Check routes before components, since they should be quicker
|
|
52
|
+
for (const projectRoute of Object.values(files.routes ?? {})) {
|
|
53
|
+
const route = new ToddleRoute({
|
|
54
|
+
route: projectRoute,
|
|
55
|
+
globalFormulas: { formulas: files.formulas, packages: files.packages },
|
|
56
|
+
})
|
|
57
|
+
const formulas = route.formulasInRoute()
|
|
58
|
+
for (const { path: _formulaPath, formula } of formulas) {
|
|
59
|
+
// Check if the formula is used in the formula
|
|
60
|
+
if (checkFormula(formula, value.name)) {
|
|
61
|
+
return true
|
|
44
62
|
}
|
|
45
63
|
}
|
|
64
|
+
}
|
|
46
65
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (formula.type === 'function') {
|
|
64
|
-
usedFormulas.add(formula.name)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
66
|
+
const componentFormulaReferences = memo('componentFormulaReferences', () => {
|
|
67
|
+
const usedFormulas = new Set<string>()
|
|
68
|
+
for (const component of Object.values(files.components)) {
|
|
69
|
+
const c = new ToddleComponent({
|
|
70
|
+
// Enforce that the component is not undefined since we're iterating
|
|
71
|
+
component: component!,
|
|
72
|
+
getComponent: (name) => files.components[name],
|
|
73
|
+
packageName: undefined,
|
|
74
|
+
globalFormulas: {
|
|
75
|
+
formulas: files.formulas,
|
|
76
|
+
packages: files.packages,
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
for (const { formula } of c.formulasInComponent()) {
|
|
80
|
+
if (formula.type === 'function') {
|
|
81
|
+
usedFormulas.add(formula.name)
|
|
67
82
|
}
|
|
68
|
-
|
|
69
|
-
},
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
if (componentFormulaReferences.has(value.name)) {
|
|
73
|
-
return
|
|
83
|
+
}
|
|
74
84
|
}
|
|
85
|
+
return usedFormulas
|
|
86
|
+
})
|
|
75
87
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
continue
|
|
80
|
-
}
|
|
88
|
+
if (componentFormulaReferences.has(value.name)) {
|
|
89
|
+
return true
|
|
90
|
+
}
|
|
81
91
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
// TODO: Memoize similar to above. We need have a helper class `ToddleFormula` with `ToddleFormula.normalizeFormulas()`
|
|
93
|
+
for (const f of Object.values(files.formulas ?? {})) {
|
|
94
|
+
if (f.name === value.name) {
|
|
95
|
+
continue
|
|
86
96
|
}
|
|
87
97
|
|
|
88
|
-
|
|
89
|
-
|
|
98
|
+
// Check if the formula is used in the formula
|
|
99
|
+
if (isToddleFormula(f) && checkFormula(f.formula, value.name)) {
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return false
|
|
90
104
|
}
|
|
91
105
|
|
|
92
106
|
const checkArguments = (
|