Skip to content

Commit

Permalink
fix(syntax): Don't highlight attributes for HTML events that are not …
Browse files Browse the repository at this point in the history
…string with JS highlighting (#844)

* fix(syntax): Don't highlight attributes for HTML events that are not string with JS highlighting

* chore: changeset
  • Loading branch information
Princesseuh authored Mar 23, 2024
1 parent 1fe7503 commit 8e55c37
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-apes-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-vscode": patch
---

Fixes attributes for HTML events (onload, onclick etc) using the wrong highlighting for interpolated attributes
37 changes: 24 additions & 13 deletions packages/vscode/syntaxes/astro.tmLanguage.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "Astro",
"scopeName": "source.astro",
"fileTypes": ["astro"],
"fileTypes": [
"astro"
],
"injections": {
"L:(meta.script.astro) (meta.lang.json) - (meta source)": {
"patterns": [
Expand Down Expand Up @@ -347,6 +349,12 @@
},
"end": "(?<=[^\\s=])(?!\\s*=)|(?=/?>)",
"patterns": [
{
"include": "#interpolation"
},
{
"include": "#attribute-literal"
},
{
"begin": "(?=[^\\s=<>`/]|/(?!>))",
"end": "(?!\\G)",
Expand Down Expand Up @@ -523,6 +531,19 @@
}
]
},
"attribute-literal": {
"begin": "(`)",
"end": "\\1",
"name": "string.template.astro",
"patterns": [
{
"include": "source.tsx#template-substitution-element"
},
{
"include": "source.tsx#string-character-escape"
}
]
},
"attributes-value": {
"patterns": [
{
Expand All @@ -548,17 +569,7 @@
"name": "string.quoted.astro"
},
{
"begin": "(`)",
"end": "\\1",
"name": "string.template.astro",
"patterns": [
{
"include": "source.tsx#template-substitution-element"
},
{
"include": "source.tsx#string-character-escape"
}
]
"include": "#attribute-literal"
}
]
},
Expand Down Expand Up @@ -832,4 +843,4 @@
]
}
}
}
}
17 changes: 11 additions & 6 deletions packages/vscode/syntaxes/astro.tmLanguage.src.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ repository:
beginCaptures: { 0: { name: punctuation.separator.key-value.astro } }
end: (?<=[^\s=])(?!\s*=)|(?=/?>)
patterns:
- include: '#interpolation'
- include: '#attribute-literal'
- begin: (?=[^\s=<>`/]|/(?!>))
end: (?!\G)
name: meta.embedded.line.js
Expand Down Expand Up @@ -355,6 +357,14 @@ repository:
end: (?<=[^\s=])(?!\s*=)|(?=/?>)
patterns: [include: '#attributes-value']

attribute-literal:
begin: (`)
end: \1
name: string.template.astro
patterns:
- include: 'source.tsx#template-substitution-element'
- include: 'source.tsx#string-character-escape'

# The value part of attribute key-values. e.g. `"my-class"` in `class="my-class"`
attributes-value:
patterns:
Expand All @@ -370,12 +380,7 @@ repository:
endCaptures: { 0: { name: punctuation.definition.string.end.astro } }
name: string.quoted.astro
# Template literals
- begin: (`)
end: \1
name: string.template.astro
patterns:
- include: 'source.tsx#template-substitution-element'
- include: 'source.tsx#string-character-escape'
- include: '#attribute-literal'

# ------
# TAGS
Expand Down
6 changes: 5 additions & 1 deletion packages/vscode/test/grammar/fixtures/attributes.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@

<div onclick="console.log()"></div>

<div class=`\${not-a-variable}`></div>
<div onload={"console.log('')"}></div>

<div onload=`console.log('')`></div>

<div class=`\${not-a-variable}`></div>
31 changes: 30 additions & 1 deletion packages/vscode/test/grammar/fixtures/attributes.astro.snap
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro
>
><div onload={"console.log('')"}></div>
#^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro
# ^^^^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro entity.other.attribute-name.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro punctuation.separator.key-value.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro punctuation.section.embedded.begin.astro
# ^^^^^^^^^^^^^^^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro meta.embedded.expression.astro source.tsx
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro punctuation.section.embedded.end.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.end.astro
# ^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro
>
><div onload=`console.log('')`></div>
#^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro
# ^^^^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro entity.other.attribute-name.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro punctuation.separator.key-value.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.onload.astro
# ^^^^^^^^^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro meta.attribute.console.log.astro entity.other.attribute-name.astro
# ^^^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.end.astro
# ^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro
>
><div class=`\${not-a-variable}`></div>
#^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.start.astro entity.name.tag.astro
Expand All @@ -70,4 +98,5 @@
# ^ source.astro meta.scope.tag.div.astro meta.tag.start.astro punctuation.definition.tag.end.astro
# ^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.begin.astro
# ^^^ source.astro meta.scope.tag.div.astro meta.tag.end.astro entity.name.tag.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro
# ^ source.astro meta.scope.tag.div.astro meta.tag.end.astro punctuation.definition.tag.end.astro
>

0 comments on commit 8e55c37

Please sign in to comment.