Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:"symbol name" symbol shorthand with quotes #1522

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions civet.dev/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ magicSymbol := :magic
iteratorSymbol := :iterator
</Playground>

Symbol names that aren't valid identifiers can be wrapped in quotes:

<Playground>
magicSymbol := :"magic-symbol"
</Playground>

## Operators

### All JavaScript/TypeScript Operators
Expand Down
34 changes: 25 additions & 9 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -2838,24 +2838,40 @@ CoffeeScriptBooleanLiteral

# NOTE: Added :symbol shorthand for Symbol.symbol or Symbol.for("symbol")
SymbolLiteral
Colon:colon IdentifierName:id ->
const { name, children: [ token ] } = id
Colon:colon ( IdentifierName / StringLiteral ):id ->
let name, token
if (id.type === "Identifier") {
({ name, children: [ token ] } = id)
} else {
name = literalValue({
type: "Literal",
subtype: "StringLiteral",
raw: id.token,
children: [id],
})
token = id
}
if (config.symbols.includes(name)) { // well-known symbol
return {
type: "SymbolLiteral",
children: [
{ ...colon, token: "Symbol." },
token,
],
children:
id.type === "Identifier" ? [
{ ...colon, token: "Symbol." },
token,
] : [
{ ...colon, token: "Symbol[" },
token,
"]",
],
name,
}
} else { // use global symbol registry
return {
type: "SymbolLiteral",
children: [
{ ...colon, token: 'Symbol.for("' },
token,
'")'
{ ...colon, token: 'Symbol.for(' },
id.type === "Identifier" ? [ '"', token, '"' ] : token,
')'
],
name,
}
Expand Down
10 changes: 10 additions & 0 deletions test/symbol.civet
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe ":symbol literals", ->
() => Symbol.for("foo")
"""

testCase """
string
---
=> :"foo-bar"
=> :"iterator"
---
() => Symbol.for("foo-bar");
() => Symbol["iterator"]
"""

testCase """
well-known
---
Expand Down
Loading