Skip to content

Commit 18c8d37

Browse files
committed
feat: support syntax error
fix demo by removing async call. The runtime needs to wait for compile and copy commonjs first before running.
1 parent 75c742a commit 18c8d37

16 files changed

+707
-329
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"scripts": {
4-
"build": "turbo run build --filter=./packages/*",
4+
"build": "turbo run build --filter=./packages/* --force",
55
"clean": "turbo run clean",
66
"compile": "tester compile",
77
"node": "pnpm --filter node",

packages/tester/ts/logic/compile.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,35 @@ export function runCompile(ctx: {
1515
projectPath: string,
1616
}) {
1717
return {
18-
compileRaw: compileProject(ctx)
18+
compileRaw: compileProject(ctx)!
1919
}
2020
}
2121

2222
export type RunCompileContext = ReturnType<typeof runCompile>
2323

24-
async function compileProject(ctx: { project: string, projectPath: string }) {
25-
return new Promise<string>(a => {
26-
cp.exec(`pnpm ${ctx.project} build`, (_err, stdout) => {
27-
a(stdout)
28-
})
29-
})
30-
.then((raw) => {
31-
copyCommonJSPackageJson(ctx)
32-
return raw
33-
})
24+
function compileProject(ctx: { project: string, projectPath: string }) {
25+
try {
26+
const raw = cp.execSync(`pnpm ${ctx.project} build`).toString()
27+
copyCommonJSPackageJson(ctx)
28+
return raw
29+
} catch (e: any) {
30+
return e.stdout.toString()
31+
}
3432
}
3533

3634
export function processCompileResults({ compileRaw, subjects, moduleTypes }: {
3735
moduleTypes: string[]
3836
} & RunCompileContext & TestSubjectsContext) {
3937

38+
const results = extractCompileErrors(compileRaw)
39+
const compileErrors = results.flatMap(result => result.importType === 'all'
40+
? subjects
41+
.find(s => s.name === result.subject)!.files
42+
.map(f => ({ ...result, importType: f.importType }))
43+
: result)
44+
4045
return {
41-
compile: compileRaw
42-
.then(extractCompileErrors)
43-
.then(results => results.flatMap(result => result.importType === 'all'
44-
? subjects
45-
.find(s => s.name === result.subject)!.files
46-
.map(f => ({ ...result, importType: f.importType }))
47-
: result
48-
))
49-
.then(compileErrors => buildCompileResults({ compileErrors, moduleTypes, subjects }))
46+
compile: buildCompileResults({ compileErrors, moduleTypes, subjects })
5047
}
5148
}
5249

packages/tester/ts/testResults.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,15 @@ function extractRuntimeErrorKey(error: Error) {
133133
}
134134
return 'ref'
135135
}
136+
if (/SyntaxError: /.test(message)) {
137+
return 'syntax'
138+
}
136139
return error.name
137140
}
138141

139142
export function extractRuntimeErrorMessage(error?: Error) {
140143
if (!error) return ''
141-
const match = /\n\n([^\n]*)/.exec(error.message)
144+
const match = error.message.split('\n').map(line => /Error: (.*)$/.exec(line)).find(x => x)
142145
return match ? match[1] : error.message
143146
}
144147

tests/node-allow/test-result.4.9.4.md

Lines changed: 300 additions & 123 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as m } from 'named-cjs'
2+
3+
m.inc(1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as m } from 'named-es-cjs'
2+
3+
m.inc(1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as m } from 'named-esm-cjs'
2+
3+
m.inc(1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import m from 'named-esm-cjs'
2+
3+
m.inc(1)

tests/node-es/test-result.4.9.4.md

Lines changed: 297 additions & 120 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as m } from 'named-cjs'
2+
3+
m.inc(1)

0 commit comments

Comments
 (0)