Skip to content

Commit e129df0

Browse files
committed
Fix errors a bit
1 parent c53d49e commit e129df0

7 files changed

Lines changed: 78 additions & 15 deletions

File tree

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,18 @@ codegen(['+', ['*', 'min', [,60]], [,'sec']])
138138
Bundle imports into a single file:
139139

140140
```js
141-
import { bundle } from 'subscript/util/bundle.js'
142-
143-
const code = await bundle('subscript/jessie.js')
144-
// → self-contained ES module
141+
// Node.js
142+
import { bundleFile } from 'subscript/util/bundle.js'
143+
console.log(await bundleFile('jessie.js'))
144+
145+
// Browser / custom sources
146+
import { bundle, fromSources } from 'subscript/util/bundle.js'
147+
const read = fromSources({
148+
'main.js': `import { x } from './lib.js'; export default x * 2`,
149+
'lib.js': `export const x = 21`
150+
})
151+
console.log(await bundle('main.js', read))
152+
// → "const x = 21;\nexport { x as default }"
145153
```
146154

147155

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"devDependencies": {
7777
"@playwright/test": "^1.57.0",
7878
"esbuild": "^0.27.2",
79-
"jsep": "^1.4.0",
8079
"terser": "^5.44.1",
8180
"tst": "^9.2.0"
8281
}

parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export let idx, cur,
1414
last = lines.pop(),
1515
before = cur.slice(Math.max(0, at - 40), at),
1616
ptr = '\u032D',
17-
chr = (cur[at] || '') + ptr,
17+
chr = (cur[at] || ' ') + ptr,
1818
after = cur.slice(at + 1, at + 20)
1919
) => {
20-
throw SyntaxError(`${msg} at ${lines.length + 1}:${last.length + 1}\n${(cur[at-41]!=='\n' ? '...' : '') +before}${chr}${after}`)
20+
throw SyntaxError(`${msg} at ${lines.length + 1}:${last.length + 1}\n${(cur[at-41]!=='\n' ? '' : '') +before}${chr}${after}`)
2121
},
2222

2323
// attach location to node (returns node for chaining)

test/errors-demo.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Demo: various parse error formats
2+
import t from 'tst'
3+
import { parse } from '../jessie.js'
4+
5+
t.demo('error formats', {only:true, bail:true}, () => {
6+
const cases = [
7+
// Unexpected token
8+
['1 * * 2', 'double multiply (no unary *)'],
9+
['a..b', 'double dot'],
10+
['(a +)', 'trailing operator in group'],
11+
12+
// Unclosed groups
13+
['(a + b', 'unclosed paren'],
14+
['[1, 2, 3', 'unclosed bracket'],
15+
['{a: 1', 'unclosed brace'],
16+
['`hello ${x', 'unclosed template'],
17+
18+
// Unexpected end
19+
['a +', 'trailing operator'],
20+
['let x =', 'incomplete assignment'],
21+
22+
// Invalid syntax
23+
['function ()', 'anonymous function without arrow'],
24+
['class { constructor }', 'incomplete class'],
25+
26+
// Mismatched
27+
['(a + b]', 'mismatched brackets'],
28+
['[1, 2)', 'mismatched parens'],
29+
30+
// Deep nesting error
31+
['((((a + b)))', 'unclosed deep nesting'],
32+
33+
// Long line error position
34+
['let veryLongVariableName = anotherLongName + yetAnotherOne * andOneMore / plusThis - ', 'long line trailing op'],
35+
]
36+
37+
for (const [code, desc] of cases) {
38+
console.log(`--- ${desc} ---`)
39+
console.log(`Input: ${JSON.stringify(code)}`)
40+
try {
41+
const ast = parse(code)
42+
console.error('Parsed:', JSON.stringify(ast))
43+
} catch (e) {
44+
console.log(e)
45+
}
46+
console.log()
47+
}
48+
})

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<script type="importmap">{
33
"imports": {
44
"tst": "../node_modules/tst/tst.js",
5+
"jsep": "../node_modules/jsep/dist/jsep.min.js",
56
"es-module-lexer": "../node_modules/es-module-lexer/dist/lexer.js",
67
"es-module-lexer": "https://unpkg.com/es-module-lexer?module"
78
}

test/perf.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const RUNS = 3e4
44
const src = c => `1 + (a * b / c % d) - 2.0 + -3e-3 * +4.4e4 / f.g[0] - i.j(+k == 1)(${c})`
55
const subscriptUrl = new URL('../subscript.js', import.meta.url).href
66
const justinUrl = new URL('../justin.js', import.meta.url).href
7+
// Browser uses CDN, Node uses npm package
8+
const jsepUrl = typeof process !== 'undefined' ? 'jsep' : 'https://esm.sh/jsep'
79

8-
test.fork('perf: expr < jsep', {data: {RUNS, src, subscriptUrl}}, async ({ ok }, {RUNS, src, subscriptUrl}) => {
10+
test.fork('perf: expr < jsep', {data: {RUNS, src, subscriptUrl, jsepUrl}}, async ({ ok }, {RUNS, src, subscriptUrl, jsepUrl}) => {
911
const bench = (fn) => {
1012
let best = Infinity
1113
for (let r = 0; r < 3; r++) {
@@ -17,7 +19,7 @@ test.fork('perf: expr < jsep', {data: {RUNS, src, subscriptUrl}}, async ({ ok },
1719
}
1820

1921
const { parse } = await import(subscriptUrl)
20-
const jsep = (await import('jsep')).default
22+
const jsep = (await import(jsepUrl)).default
2123

2224
for (let w = 0; w < RUNS; w++) { parse(src(w)); jsep(src(w)) }
2325

@@ -27,7 +29,7 @@ test.fork('perf: expr < jsep', {data: {RUNS, src, subscriptUrl}}, async ({ ok },
2729
ok(time < baseline, `expr (${time.toFixed(0)}ms) should be < jsep (${baseline.toFixed(0)}ms)`)
2830
})
2931

30-
test.fork('perf: justin <= jsep', {data: {RUNS, src, justinUrl}}, async ({ ok }, {RUNS, src, justinUrl}) => {
32+
test.fork('perf: justin <= jsep', {data: {RUNS, src, justinUrl, jsepUrl}}, async ({ ok }, {RUNS, src, justinUrl, jsepUrl}) => {
3133
const bench = (fn) => {
3234
let best = Infinity
3335
for (let r = 0; r < 3; r++) {
@@ -39,7 +41,7 @@ test.fork('perf: justin <= jsep', {data: {RUNS, src, justinUrl}}, async ({ ok },
3941
}
4042

4143
const { parse } = await import(justinUrl)
42-
const jsep = (await import('jsep')).default
44+
const jsep = (await import(jsepUrl)).default
4345

4446
for (let w = 0; w < RUNS; w++) { parse(src(w)); jsep(src(w)) }
4547

test/test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ import './feature/unit.js'
1919
import './feature/jessie.js'
2020
import './feature/async-class.js'
2121

22-
import './meta.js'
2322

2423
// Integration tests
2524
import './subscript.js'
2625

27-
// Bundler tests (dogfooding)
28-
import './bundle.js'
29-
import './perf.js'
26+
// Node.js only tests (bundler uses fs, perf needs stable timing)
27+
if (typeof process !== 'undefined') {
28+
await import('./meta.js')
29+
await import('./bundle.js')
30+
await import('./perf.js')
31+
}
32+
33+
// Error formatting demo
34+
// import './errors-demo.js'

0 commit comments

Comments
 (0)