restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
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 <Card title=\"x\" onClick={() => go(1)}>\n Hi\n</Card>"
|
||||
#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 = <div\n className=\"x\"\n/>\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 <b) {}", .jsx) == .keyword)
|
||||
}
|
||||
|
||||
@Test("an unterminated string ends at the line end")
|
||||
func unterminatedString() {
|
||||
let text = "const a = 'open\nconst b = 1"
|
||||
#expect(role("const", in: text, .javascript, occurrence: 1) == .keyword)
|
||||
}
|
||||
|
||||
@Test("JSX children are plain text, so an apostrophe opens no string")
|
||||
func childrenText() {
|
||||
let text = "const a = <p>Don't {name} if</p>\nconst b = 'x'"
|
||||
#expect(role("Don't ", in: text, .jsx) == .plain)
|
||||
#expect(role("if", in: text, .jsx) == .plain)
|
||||
#expect(role("p", in: text, .jsx, occurrence: 1) == .number)
|
||||
#expect(role("'x'", in: text, .jsx) == .string)
|
||||
}
|
||||
|
||||
@Test("nested elements and expressions over several lines return to code at the end")
|
||||
func nestedChildren() {
|
||||
let text = """
|
||||
const list = (
|
||||
<ul className="x">
|
||||
{items.map((item) => (
|
||||
<li key={item.id}>{item.label}</li>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
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 <b>x</b></>\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 = <Br />; let z", .jsx) == .keyword)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user