Skip to content

Commit 6f25131

Browse files
committed
refactor(app): rename useCode > useEvaluation
1 parent 5a08efa commit 6f25131

File tree

4 files changed

+20
-59
lines changed

4 files changed

+20
-59
lines changed

packages/app/modules/evaluation/composables/use-code.spec.ts renamed to packages/app/modules/evaluation/composables/use-evaluation.spec.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,13 @@
11
import { afterEach, describe, expect, it } from 'vitest'
2-
import { useCode } from './use-code'
2+
import { useEvaluation } from './use-evaluation'
33

4-
describe('useCode', () => {
5-
const composable = useCode()
4+
describe('useEvaluation', () => {
5+
const composable = useEvaluation()
66

77
afterEach(() => {
88
composable.setResolvers([])
99
})
1010

11-
it.each([
12-
[`import { ref } from "vue"`, `const { ref } = __INDEX_SAN_IMPORT("vue")`],
13-
[`import { ref } from 'vue'`, `const { ref } = __INDEX_SAN_IMPORT("vue")`],
14-
[`import lodash from "lodash"`, `const lodash = __INDEX_SAN_IMPORT("lodash")`],
15-
[`import lodash from 'lodash'`, `const lodash = __INDEX_SAN_IMPORT("lodash")`],
16-
[
17-
`
18-
import {
19-
camelCase,
20-
snakeCase
21-
} from 'lodash'
22-
`,
23-
`
24-
const {
25-
camelCase,
26-
snakeCase
27-
} = __INDEX_SAN_IMPORT("lodash")
28-
`,
29-
],
30-
])('should replace imports (%#)', async (source, expected) => {
31-
const result = await composable.mount(source)
32-
33-
expect(result.code).toBe(expected)
34-
})
35-
36-
it.each([
37-
[`export default { foo: 'bar' }`, `__INDEX_SAN_EXPORT({ default: { foo: 'bar' } })`],
38-
[
39-
`export default function (){ return {foo: 'bar'} }`,
40-
`__INDEX_SAN_EXPORT({ default: function (){ return {foo: 'bar'} } })`,
41-
],
42-
[`export const foo = 'bar'`, `__INDEX_SAN_EXPORT({ foo: 'bar' })`],
43-
])('should replace exports (%#)', async (source, expected) => {
44-
const result = await composable.mount(source)
45-
46-
expect(result.code).toBe(expected)
47-
})
48-
4911
it('should use resolver to handle imports', async () => {
5012
const lodash = { camelCase: () => '' }
5113

packages/app/modules/evaluation/composables/use-code.ts renamed to packages/app/modules/evaluation/composables/use-evaluation.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
import { defineExportProcessor } from '../processors/exports'
22
import { defineImportProcessor } from '../processors/imports'
3-
4-
interface ImportResolver {
5-
test: (id: string) => boolean
6-
order?: number
7-
resolve: (id: string) => Promise<any>
8-
}
9-
10-
interface Processor {
11-
order?: number
12-
process: (code: string) => string | undefined
13-
}
3+
import Processor from '../types/processor'
4+
import Resolver from '../types/resolver'
145

156
const prefix = '__INDEX_SAN'
167

17-
export function useCode() {
8+
export function useEvaluation() {
189
const state = {
19-
prefix: '__INDEX_SAN',
20-
resolvers: [] as ImportResolver[],
10+
resolvers: [] as Resolver[],
2111
processors: [] as Processor[],
2212
}
2313

24-
function addResolver(resolver: ImportResolver) {
14+
function addResolver(resolver: Resolver) {
2515
state.resolvers.push(resolver)
2616
}
2717

2818
function addProcessor(Processor: Processor) {
2919
state.processors.push(Processor)
3020
}
3121

32-
function setResolvers(resolvers: ImportResolver[]) {
22+
function setResolvers(resolvers: Resolver[]) {
3323
state.resolvers = resolvers
3424
}
3525

@@ -115,8 +105,8 @@ export function useCode() {
115105
const exported = {} as Record<string, any>
116106

117107
const context = {
118-
[`${state.prefix}_IMPORT`]: (moduleId: string) => spec.imports[moduleId],
119-
[`${state.prefix}_EXPORT`]: (newExported: Record<string, any>) => {
108+
[`${prefix}_IMPORT`]: (moduleId: string) => spec.imports[moduleId],
109+
[`${prefix}_EXPORT`]: (newExported: Record<string, any>) => {
120110
Object.assign(exported, newExported)
121111
},
122112
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default interface Processor {
2+
order?: number
3+
process: (code: string) => string | undefined
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default interface Resolver {
2+
test: (id: string) => boolean
3+
order?: number
4+
resolve: (id: string) => Promise<any>
5+
}

0 commit comments

Comments
 (0)