import HelderCore
import Testing
@Suite struct JavaScriptGrammarTests {
@Test("keywords, strings, numbers and comments")
func basics() {
let text = "const a = 'x' + \"y\" // note\nlet b = 1.5e3"
#expect(paintedRuns(text, .javascript) == [
"const:keyword", "=:keyword", "'x':string", "+:keyword", "\"y\":string", "// note:comment",
"let:keyword", "=:keyword", "1.5e3:number",
])
}
@Test("a template literal spans lines and highlights its interpolation as code")
func templateLiteral() {
let text = "const s = `a ${fn(1)} b\nstill string ${x}`\nconst after = 2"
#expect(role("`a ", in: text, .javascript) == .string)
#expect(role("1", in: text, .javascript) == .number)
#expect(role("still string ", in: text, .javascript) == .string)
#expect(role("after", in: text, .javascript) == .plain)
#expect(role("2", in: text, .javascript) == .number)
}
@Test("an interpolation holding braces returns to the template at its own brace")
func nestedBraces() {
let text = "`${ {a: 1}.a } tail`"
#expect(role(" tail`", in: text, .javascript) == .string)
}
@Test("a regex literal after an operator is a string, a slash after a value divides")
func regexVersusDivision() {
#expect(role("/ab+c/gi", in: "const r = /ab+c/gi", .javascript) == .string)
#expect(role("/", in: "const q = a / b / c", .javascript) == .keyword)
}
@Test("block comments carry over lines")
func blockComment() {
let text = "/**\n * Docs\n */\nfunction f() {}"
#expect(role(" * Docs", in: text, .javascript) == .comment)
#expect(role("function", in: text, .javascript) == .keyword)
}
@Test("class names follow class, extends and new")
func classNames() {
let text = "class A extends B { m() { return new C() } }"
#expect(role("A", in: text, .javascript) == .variable)
#expect(role("B", in: text, .javascript) == .variable)
#expect(role("C", in: text, .javascript) == .variable)
#expect(role("m", in: text, .javascript) == .plain)
}
@Test("constants and booleans read in the number colour")
func constants() {
let text = "if (x === undefined || y === true) MAX_SIZE"
#expect(role("undefined", in: text, .javascript) == .number)
#expect(role("true", in: text, .javascript) == .number)
#expect(role("MAX_SIZE", in: text, .javascript) == .number)
}
@Test("TypeScript adds its keywords and builtin types")
func typescript() {
let text = "interface A { readonly n: number }\ntype B = keyof A"
#expect(role("readonly", in: text, .typescript) == .keyword)
#expect(role("number", in: text, .typescript) == .number)
#expect(role("keyof", in: text, .typescript) == .keyword)
#expect(role("type", in: text, .javascript) == .plain)
}
@Test("JSX tags, attributes and expressions", arguments: [Language.jsx, .tsx])
func jsx(language: Language) {
let text = "return