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 go(1)}>\n Hi\n" #expect(role("Card", in: text, language) == .number) #expect(role("title", in: text, language) == .variable) #expect(role("\"x\"", in: text, language) == .string) #expect(role("=>", in: text, language) == .keyword) #expect(role("1", in: text, language) == .number) #expect(role("Card", in: text, language, occurrence: 1) == .number) } @Test("a JSX tag may wrap its attributes over lines") func multiLineTag() { let text = "const a = \nlet b" #expect(role("className", in: text, .jsx) == .variable) #expect(role("let", in: text, .jsx) == .keyword) } @Test("a less-than between values stays an operator in JSX") func lessThan() { #expect(role("<", in: "if (a {items.map((item) => (
  • {item.label}
  • ))} ) let after = 1 """ #expect(role("ul", in: text, .jsx) == .number) #expect(role("=>", in: text, .jsx) == .keyword) #expect(role("li", in: text, .jsx, occurrence: 1) == .number) #expect(role("key", in: text, .jsx) == .variable) #expect(role("ul", in: text, .jsx, occurrence: 1) == .number) #expect(role("let", in: text, .jsx) == .keyword) } @Test("a fragment opens and closes like an element") func fragments() { let text = "return <>it's x\nlet y" #expect(role("it's ", in: text, .tsx) == .plain) #expect(role("let", in: text, .tsx) == .keyword) } @Test("a self-closing element at the top leaves code mode at once") func selfClosing() { #expect(role("let", in: "x =
    ; let z", .jsx) == .keyword) } }