Skip to content

Commit 24be49c

Browse files
committed
do basic check for valid subscript syntax
1 parent 0b2f476 commit 24be49c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/processScript/preprocess.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import t from "@babel/types"
77
import type { LaxPartial } from "@samual/lib"
88
import { assert } from "@samual/lib/assert"
99
import { spliceString } from "@samual/lib/spliceString"
10+
import { tokenizer as tokenise, tokTypes as TokenTypes } from "acorn"
1011
import { resolve as resolveModule } from "import-meta-resolve"
1112

1213
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
1314
const { default: traverse } = babelTraverse as any as typeof import("@babel/traverse")
1415
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
1516
const { default: generate } = babelGenerator as any as typeof import("@babel/generator")
1617

18+
const SUBSCRIPT_PREFIXES = [ `s`, `fs`, `4s`, `hs`, `3s`, `ms`, `2s`, `ls`, `1s`, `ns`, `0s` ]
19+
1720
export type PreprocessOptions = LaxPartial<{ /** 11 a-z 0-9 characters */ uniqueId: string }>
1821

1922
/** @param code source code for preprocessing
@@ -22,6 +25,25 @@ export async function preprocess(code: string, { uniqueId = `00000000000` }: Pre
2225
: Promise<{ code: string }> {
2326
assert(/^\w{11}$/.test(uniqueId), HERE)
2427

28+
const tokensIterable = tokenise(code, { ecmaVersion: `latest` })
29+
30+
for (const token of tokensIterable) {
31+
assert(`value` in token, HERE)
32+
33+
if (token.type != TokenTypes.privateId)
34+
continue
35+
36+
assert(typeof token.value == `string`, HERE)
37+
38+
if (!SUBSCRIPT_PREFIXES.includes(token.value))
39+
continue
40+
41+
const nextToken = tokensIterable.getToken()
42+
43+
if (nextToken.type != TokenTypes._in && nextToken.type != TokenTypes.dot)
44+
throw SyntaxError(`Subscripts must be in the form of #fs.foo.bar`)
45+
}
46+
2547
const sourceCode = code
2648
let lengthBefore
2749

0 commit comments

Comments
 (0)