Skip to content

Commit 5b3c8b7

Browse files
authored
Merge branch 'main' into fix/transform-literal-html-head-body
2 parents c5a3f94 + 910b093 commit 5b3c8b7

File tree

5 files changed

+95
-5
lines changed

5 files changed

+95
-5
lines changed

Diff for: .changeset/orange-knives-smile.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
Adds warnings indicating that the `data-astro-rerun` attribute can not be used on an external module `<script>` and that `data-astro-reload` is only supported on `<a>`, `<area>` and `<form>` elements.

Diff for: .github/workflows/snapshot-release.yml

+14-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ jobs:
1515
if: ${{ github.repository_owner == 'withastro' && github.event.issue.pull_request && startsWith(github.event.comment.body, '!preview') }}
1616
runs-on: ubuntu-latest
1717
steps:
18-
- name: 'Check if user has admin access (only admins can publish snapshot releases).'
19-
uses: 'lannonbr/[email protected]'
18+
- name: Check if user has admin access (only admins can publish snapshot releases).
19+
id: checkAccess
20+
uses: actions-cool/check-user-permission@v2
2021
with:
21-
permission: 'admin'
22-
env:
23-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22+
require: admin
23+
username: ${{ github.triggering_actor }}
24+
25+
# if the user does not have the required permission, we should return exit code 1 to stop the workflow
26+
- name: Check user permission
27+
if: steps.checkAccess.outputs.require-result == 'false'
28+
run: |
29+
echo "${{ github.triggering_actor }} does not have permissions on this repo."
30+
echo "Current permission level is ${{ steps.checkAccess.outputs.user-permission }}"
31+
echo "Job originally triggered by ${{ github.actor }}"
32+
exit 1
2433
2534
- name: Extract the snapshot name from comment body
2635
id: getSnapshotName

Diff for: internal/loc/diagnostics.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
WARNING_CANNOT_DEFINE_VARS DiagnosticCode = 2007
2020
WARNING_INVALID_SPREAD DiagnosticCode = 2008
2121
WARNING_UNEXPECTED_CHARACTER DiagnosticCode = 2009
22+
WARNING_CANNOT_RERUN DiagnosticCode = 2010
2223
INFO DiagnosticCode = 3000
2324
HINT DiagnosticCode = 4000
2425
)

Diff for: internal/transform/transform.go

+52
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
const TRANSITION_ANIMATE = "transition:animate"
1717
const TRANSITION_NAME = "transition:name"
1818
const TRANSITION_PERSIST = "transition:persist"
19+
const DATA_ASTRO_RELOAD = "data-astro-reload"
1920
const TRANSITION_PERSIST_PROPS = "transition:persist-props"
2021

2122
type TransformOptions struct {
@@ -42,6 +43,8 @@ func Transform(doc *astro.Node, opts TransformOptions, h *handler.Handler) *astr
4243
i := 0
4344
walk(doc, func(n *astro.Node) {
4445
i++
46+
WarnAboutRerunOnExternalESMs(n, h)
47+
WarnAboutMisplacedReload(n, h)
4548
HintAboutImplicitInlineDirective(n, h)
4649
ExtractScript(doc, n, &opts, h)
4750
AddComponentProps(doc, n, &opts)
@@ -357,6 +360,55 @@ func collapseWhitespace(doc *astro.Node) {
357360
})
358361
}
359362

363+
func WarnAboutMisplacedReload(n *astro.Node, h *handler.Handler) {
364+
if HasAttr(n, DATA_ASTRO_RELOAD) {
365+
attr := &n.Attr[AttrIndex(n, DATA_ASTRO_RELOAD)]
366+
367+
/*
368+
* When set on <a>, <form> or <area>,
369+
* the data-astro-reload attribute replaces view transitions between pages with a full page loads.
370+
* The data-astro-reload attribute is not supported for other elements. It does not accept a value.
371+
*/
372+
373+
if n.Type != astro.ElementNode || n.Data != "a" && n.Data != "area" && n.Data != "form" {
374+
h.AppendWarning(&loc.ErrorWithRange{
375+
Code: loc.WARNING,
376+
Text: "The data-astro-reload attribute is only supported on <a>, <form> and <area> elements.",
377+
Range: loc.Range{Loc: attr.KeyLoc, Len: len(attr.Key)},
378+
})
379+
}
380+
if attr.Val != "" {
381+
h.AppendWarning(&loc.ErrorWithRange{
382+
Code: loc.WARNING_UNSUPPORTED_EXPRESSION,
383+
Text: "The data-astro-reload attribute does not accept a value",
384+
Range: loc.Range{Loc: attr.ValLoc, Len: len(attr.Val)},
385+
})
386+
return
387+
}
388+
}
389+
}
390+
391+
func WarnAboutRerunOnExternalESMs(n *astro.Node, h *handler.Handler) {
392+
if n.Data == "script" && HasAttr(n, "src") && HasAttr(n, "type") && HasAttr(n, "data-astro-rerun") {
393+
394+
/*
395+
* The browser caches external ECMAScript Modules. Even if such a script is included several times on a page,
396+
* it will only run once. This means that the data-astro-rerun attribute will not have any effect.
397+
*/
398+
src := &n.Attr[AttrIndex(n, "src")]
399+
typ := &n.Attr[AttrIndex(n, "type")]
400+
rerun := &n.Attr[AttrIndex(n, "data-astro-rerun")]
401+
if typ.Val == "module" && src.Val != "" {
402+
h.AppendWarning(&loc.ErrorWithRange{
403+
Code: loc.WARNING_CANNOT_RERUN,
404+
Text: "The data-astro-rerun attribute is not supported on an external module <script>",
405+
Hint: "Two out of three is OK: type=\"module\", src=\"...\", or data-astro-rerun",
406+
Range: loc.Range{Loc: rerun.KeyLoc, Len: len(rerun.Key)},
407+
})
408+
}
409+
}
410+
}
411+
360412
func ExtractScript(doc *astro.Node, n *astro.Node, opts *TransformOptions, h *handler.Handler) {
361413
if n.Type == astro.ElementNode && n.DataAtom == a.Script {
362414
if HasSetDirective(n) || HasInlineDirective(n) {

Diff for: packages/compiler/test/transition/data-astro.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { test } from 'uvu';
2+
import * as assert from 'uvu/assert';
3+
import { transform } from '@astrojs/compiler';
4+
5+
const FIXTURE = `
6+
<div data-astro-reload>
7+
<a href="/" data-astro-reload>/</a>
8+
<form data-astro-reload="x">.</form>
9+
<area data-astro-reload/>
10+
<svg xmlns="http://www.w3.org/2000/svg"><a data-astro-reload>.</a></svg>
11+
<script is:inline data-astro-rerun src="some.js" type="module" />
12+
<script is:inline data-astro-rerun>"Bar"</script>
13+
</div>`;
14+
15+
test('Issues warnings for data-astro-* attributes', async () => {
16+
const result = await transform(FIXTURE);
17+
assert.equal(result.diagnostics.length, 3);
18+
assert.equal(result.diagnostics[0].code, 2000);
19+
assert.equal(result.diagnostics[1].code, 2005);
20+
assert.equal(result.diagnostics[2].code, 2010);
21+
});
22+
23+
test.run();

0 commit comments

Comments
 (0)