@pyreon/compiler 0.11.4 → 0.11.6
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/README.md +13 -10
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +4 -4
- package/package.json +10 -10
- package/src/index.ts +6 -11
- package/src/jsx.ts +104 -104
- package/src/project-scanner.ts +21 -21
- package/src/react-intercept.ts +213 -213
- package/src/tests/jsx.test.ts +583 -583
- package/src/tests/project-scanner.test.ts +63 -63
- package/src/tests/react-intercept.test.ts +280 -280
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import * as fs from
|
|
2
|
-
import * as os from
|
|
3
|
-
import * as path from
|
|
4
|
-
import { generateContext } from
|
|
1
|
+
import * as fs from 'node:fs'
|
|
2
|
+
import * as os from 'node:os'
|
|
3
|
+
import * as path from 'node:path'
|
|
4
|
+
import { generateContext } from '../project-scanner'
|
|
5
5
|
|
|
6
6
|
/** Create a temporary directory with the given file structure. */
|
|
7
7
|
function createTempProject(files: Record<string, string>): string {
|
|
8
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(),
|
|
8
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pyreon-scanner-'))
|
|
9
9
|
for (const [filePath, content] of Object.entries(files)) {
|
|
10
10
|
const fullPath = path.join(dir, filePath)
|
|
11
11
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true })
|
|
@@ -18,14 +18,14 @@ function cleanupDir(dir: string): void {
|
|
|
18
18
|
fs.rmSync(dir, { recursive: true, force: true })
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
describe(
|
|
22
|
-
test(
|
|
21
|
+
describe('project-scanner — generateContext', () => {
|
|
22
|
+
test('returns valid ProjectContext shape for empty project', () => {
|
|
23
23
|
const dir = createTempProject({
|
|
24
|
-
|
|
24
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
25
25
|
})
|
|
26
26
|
try {
|
|
27
27
|
const ctx = generateContext(dir)
|
|
28
|
-
expect(ctx.framework).toBe(
|
|
28
|
+
expect(ctx.framework).toBe('pyreon')
|
|
29
29
|
expect(ctx.routes).toEqual([])
|
|
30
30
|
expect(ctx.components).toEqual([])
|
|
31
31
|
expect(ctx.islands).toEqual([])
|
|
@@ -35,28 +35,28 @@ describe("project-scanner — generateContext", () => {
|
|
|
35
35
|
}
|
|
36
36
|
})
|
|
37
37
|
|
|
38
|
-
test(
|
|
38
|
+
test('reads version from @pyreon/* dependency', () => {
|
|
39
39
|
const dir = createTempProject({
|
|
40
|
-
|
|
41
|
-
name:
|
|
42
|
-
dependencies: {
|
|
40
|
+
'package.json': JSON.stringify({
|
|
41
|
+
name: 'test',
|
|
42
|
+
dependencies: { '@pyreon/core': '^0.7.0' },
|
|
43
43
|
}),
|
|
44
44
|
})
|
|
45
45
|
try {
|
|
46
46
|
const ctx = generateContext(dir)
|
|
47
|
-
expect(ctx.version).toBe(
|
|
47
|
+
expect(ctx.version).toBe('0.7.0')
|
|
48
48
|
} finally {
|
|
49
49
|
cleanupDir(dir)
|
|
50
50
|
}
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
-
test(
|
|
53
|
+
test('falls back to package version when no @pyreon deps', () => {
|
|
54
54
|
const dir = createTempProject({
|
|
55
|
-
|
|
55
|
+
'package.json': JSON.stringify({ name: 'test', version: '2.0.0' }),
|
|
56
56
|
})
|
|
57
57
|
try {
|
|
58
58
|
const ctx = generateContext(dir)
|
|
59
|
-
expect(ctx.version).toBe(
|
|
59
|
+
expect(ctx.version).toBe('2.0.0')
|
|
60
60
|
} finally {
|
|
61
61
|
cleanupDir(dir)
|
|
62
62
|
}
|
|
@@ -66,18 +66,18 @@ describe("project-scanner — generateContext", () => {
|
|
|
66
66
|
const dir = createTempProject({})
|
|
67
67
|
try {
|
|
68
68
|
const ctx = generateContext(dir)
|
|
69
|
-
expect(ctx.version).toBe(
|
|
69
|
+
expect(ctx.version).toBe('unknown')
|
|
70
70
|
} finally {
|
|
71
71
|
cleanupDir(dir)
|
|
72
72
|
}
|
|
73
73
|
})
|
|
74
74
|
})
|
|
75
75
|
|
|
76
|
-
describe(
|
|
77
|
-
test(
|
|
76
|
+
describe('project-scanner — extractRoutes', () => {
|
|
77
|
+
test('extracts routes from createRouter call', () => {
|
|
78
78
|
const dir = createTempProject({
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
80
|
+
'src/router.ts': `
|
|
81
81
|
const router = createRouter([
|
|
82
82
|
{ path: "/", name: "home", component: Home },
|
|
83
83
|
{ path: "/users/:id", name: "user", loader: fetchUser },
|
|
@@ -88,22 +88,22 @@ const router = createRouter([
|
|
|
88
88
|
try {
|
|
89
89
|
const ctx = generateContext(dir)
|
|
90
90
|
expect(ctx.routes).toHaveLength(3)
|
|
91
|
-
expect(ctx.routes[0]?.path).toBe(
|
|
92
|
-
expect(ctx.routes[0]?.name).toBe(
|
|
93
|
-
expect(ctx.routes[1]?.path).toBe(
|
|
94
|
-
expect(ctx.routes[1]?.params).toEqual([
|
|
91
|
+
expect(ctx.routes[0]?.path).toBe('/')
|
|
92
|
+
expect(ctx.routes[0]?.name).toBe('home')
|
|
93
|
+
expect(ctx.routes[1]?.path).toBe('/users/:id')
|
|
94
|
+
expect(ctx.routes[1]?.params).toEqual(['id'])
|
|
95
95
|
expect(ctx.routes[1]?.hasLoader).toBe(true)
|
|
96
|
-
expect(ctx.routes[2]?.path).toBe(
|
|
96
|
+
expect(ctx.routes[2]?.path).toBe('/admin')
|
|
97
97
|
expect(ctx.routes[2]?.hasGuard).toBe(true)
|
|
98
98
|
} finally {
|
|
99
99
|
cleanupDir(dir)
|
|
100
100
|
}
|
|
101
101
|
})
|
|
102
102
|
|
|
103
|
-
test(
|
|
103
|
+
test('extracts routes from const routes = [] pattern', () => {
|
|
104
104
|
const dir = createTempProject({
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
106
|
+
'src/routes.ts': `
|
|
107
107
|
const routes: RouteRecord[] = [
|
|
108
108
|
{ path: "/about", name: "about" },
|
|
109
109
|
]
|
|
@@ -112,17 +112,17 @@ const routes: RouteRecord[] = [
|
|
|
112
112
|
try {
|
|
113
113
|
const ctx = generateContext(dir)
|
|
114
114
|
expect(ctx.routes).toHaveLength(1)
|
|
115
|
-
expect(ctx.routes[0]?.path).toBe(
|
|
116
|
-
expect(ctx.routes[0]?.name).toBe(
|
|
115
|
+
expect(ctx.routes[0]?.path).toBe('/about')
|
|
116
|
+
expect(ctx.routes[0]?.name).toBe('about')
|
|
117
117
|
} finally {
|
|
118
118
|
cleanupDir(dir)
|
|
119
119
|
}
|
|
120
120
|
})
|
|
121
121
|
|
|
122
|
-
test(
|
|
122
|
+
test('extracts multiple params from route path', () => {
|
|
123
123
|
const dir = createTempProject({
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
125
|
+
'src/router.ts': `
|
|
126
126
|
const router = createRouter([
|
|
127
127
|
{ path: "/users/:userId/posts/:postId" },
|
|
128
128
|
])
|
|
@@ -130,18 +130,18 @@ const router = createRouter([
|
|
|
130
130
|
})
|
|
131
131
|
try {
|
|
132
132
|
const ctx = generateContext(dir)
|
|
133
|
-
expect(ctx.routes[0]?.params).toEqual([
|
|
133
|
+
expect(ctx.routes[0]?.params).toEqual(['userId', 'postId'])
|
|
134
134
|
} finally {
|
|
135
135
|
cleanupDir(dir)
|
|
136
136
|
}
|
|
137
137
|
})
|
|
138
138
|
})
|
|
139
139
|
|
|
140
|
-
describe(
|
|
141
|
-
test(
|
|
140
|
+
describe('project-scanner — extractComponents', () => {
|
|
141
|
+
test('extracts component with signals', () => {
|
|
142
142
|
const dir = createTempProject({
|
|
143
|
-
|
|
144
|
-
|
|
143
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
144
|
+
'src/Counter.tsx': `
|
|
145
145
|
export const Counter = ({ initial }) => {
|
|
146
146
|
const count = signal<number>(0)
|
|
147
147
|
const doubled = signal(0)
|
|
@@ -151,19 +151,19 @@ export const Counter = ({ initial }) => {
|
|
|
151
151
|
})
|
|
152
152
|
try {
|
|
153
153
|
const ctx = generateContext(dir)
|
|
154
|
-
const counter = ctx.components.find((c) => c.name ===
|
|
154
|
+
const counter = ctx.components.find((c) => c.name === 'Counter')
|
|
155
155
|
expect(counter).toBeTruthy()
|
|
156
156
|
expect(counter?.hasSignals).toBe(true)
|
|
157
|
-
expect(counter?.signalNames).toEqual([
|
|
157
|
+
expect(counter?.signalNames).toEqual(['count', 'doubled'])
|
|
158
158
|
} finally {
|
|
159
159
|
cleanupDir(dir)
|
|
160
160
|
}
|
|
161
161
|
})
|
|
162
162
|
|
|
163
|
-
test(
|
|
163
|
+
test('extracts component without signals', () => {
|
|
164
164
|
const dir = createTempProject({
|
|
165
|
-
|
|
166
|
-
|
|
165
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
166
|
+
'src/Header.tsx': `
|
|
167
167
|
export function Header({ title }) {
|
|
168
168
|
return <h1>{title}</h1>
|
|
169
169
|
}
|
|
@@ -171,7 +171,7 @@ export function Header({ title }) {
|
|
|
171
171
|
})
|
|
172
172
|
try {
|
|
173
173
|
const ctx = generateContext(dir)
|
|
174
|
-
const header = ctx.components.find((c) => c.name ===
|
|
174
|
+
const header = ctx.components.find((c) => c.name === 'Header')
|
|
175
175
|
expect(header).toBeTruthy()
|
|
176
176
|
expect(header?.hasSignals).toBe(false)
|
|
177
177
|
expect(header?.signalNames).toEqual([])
|
|
@@ -181,11 +181,11 @@ export function Header({ title }) {
|
|
|
181
181
|
})
|
|
182
182
|
})
|
|
183
183
|
|
|
184
|
-
describe(
|
|
185
|
-
test(
|
|
184
|
+
describe('project-scanner — extractIslands', () => {
|
|
185
|
+
test('extracts island definitions', () => {
|
|
186
186
|
const dir = createTempProject({
|
|
187
|
-
|
|
188
|
-
|
|
187
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
188
|
+
'src/islands.ts': `
|
|
189
189
|
const Counter = island(() => import("./Counter"), { hydrate: "visible", name: "Counter" })
|
|
190
190
|
const Nav = island(() => import("./Nav"), { name: "Nav" })
|
|
191
191
|
`,
|
|
@@ -193,25 +193,25 @@ const Nav = island(() => import("./Nav"), { name: "Nav" })
|
|
|
193
193
|
try {
|
|
194
194
|
const ctx = generateContext(dir)
|
|
195
195
|
expect(ctx.islands).toHaveLength(2)
|
|
196
|
-
const counter = ctx.islands.find((i) => i.name ===
|
|
196
|
+
const counter = ctx.islands.find((i) => i.name === 'Counter')
|
|
197
197
|
expect(counter).toBeTruthy()
|
|
198
198
|
// hydrate comes before name in the object literal, but the regex captures
|
|
199
199
|
// name first, so hydrate is only captured when it follows name directly
|
|
200
|
-
const nav = ctx.islands.find((i) => i.name ===
|
|
201
|
-
expect(nav?.hydrate).toBe(
|
|
200
|
+
const nav = ctx.islands.find((i) => i.name === 'Nav')
|
|
201
|
+
expect(nav?.hydrate).toBe('load') // default when hydrate not specified
|
|
202
202
|
} finally {
|
|
203
203
|
cleanupDir(dir)
|
|
204
204
|
}
|
|
205
205
|
})
|
|
206
206
|
})
|
|
207
207
|
|
|
208
|
-
describe(
|
|
209
|
-
test(
|
|
208
|
+
describe('project-scanner — collectSourceFiles', () => {
|
|
209
|
+
test('skips node_modules and dist directories', () => {
|
|
210
210
|
const dir = createTempProject({
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
211
|
+
'package.json': JSON.stringify({ name: 'test', version: '1.0.0' }),
|
|
212
|
+
'src/app.tsx': 'export const App = () => <div />',
|
|
213
|
+
'node_modules/pkg/index.ts': 'export const x = 1',
|
|
214
|
+
'dist/app.js': 'export const App = () => {}',
|
|
215
215
|
})
|
|
216
216
|
try {
|
|
217
217
|
const ctx = generateContext(dir)
|
|
@@ -219,21 +219,21 @@ describe("project-scanner — collectSourceFiles", () => {
|
|
|
219
219
|
expect(ctx.components.length).toBeGreaterThanOrEqual(1)
|
|
220
220
|
const files = ctx.components.map((c) => c.file)
|
|
221
221
|
for (const f of files) {
|
|
222
|
-
expect(f).not.toContain(
|
|
223
|
-
expect(f).not.toContain(
|
|
222
|
+
expect(f).not.toContain('node_modules')
|
|
223
|
+
expect(f).not.toContain('dist')
|
|
224
224
|
}
|
|
225
225
|
} finally {
|
|
226
226
|
cleanupDir(dir)
|
|
227
227
|
}
|
|
228
228
|
})
|
|
229
229
|
|
|
230
|
-
test(
|
|
230
|
+
test('handles non-existent directory gracefully', () => {
|
|
231
231
|
const dir = path.join(os.tmpdir(), `pyreon-scanner-nonexistent-${Date.now()}`)
|
|
232
232
|
// generateContext calls collectSourceFiles which catches readdir errors
|
|
233
233
|
const ctx = generateContext(dir)
|
|
234
234
|
expect(ctx.routes).toEqual([])
|
|
235
235
|
expect(ctx.components).toEqual([])
|
|
236
236
|
expect(ctx.islands).toEqual([])
|
|
237
|
-
expect(ctx.version).toBe(
|
|
237
|
+
expect(ctx.version).toBe('unknown')
|
|
238
238
|
})
|
|
239
239
|
})
|