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

Add lint rule for implicit is:inline on script tags. #970

Merged
merged 16 commits into from
Feb 21, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/cuddly-pumas-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Adds a lint rules when attributes are added to a script tag, causing the script to be treated as `is:inline`.
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *Handler) AppendWarning(err error) {
func (h *Handler) AppendInfo(err error) {
h.infos = append(h.infos, err)
}

func (h *Handler) AppendHint(err error) {
h.hints = append(h.hints, err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Transform(doc *astro.Node, opts TransformOptions, h *handler.Handler) *astr
i := 0
walk(doc, func(n *astro.Node) {
i++
HintAboutImplicitInlineDirective(n, h)
ExtractScript(doc, n, &opts, h)
AddComponentProps(doc, n, &opts)
if shouldScope {
Expand Down Expand Up @@ -404,6 +405,17 @@ func ExtractScript(doc *astro.Node, n *astro.Node, opts *TransformOptions, h *ha
}
}

func HintAboutImplicitInlineDirective(n *astro.Node, h *handler.Handler) {
if n.Type == astro.ElementNode && n.DataAtom == a.Script && len(n.Attr) > 0 && !HasInlineDirective(n) {
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
h.AppendHint(&loc.ErrorWithRange{
Code: loc.HINT,
Text: "Astro processes your script tags to allow using TypeScript and NPM packages, and to optimize browser performance.\n\nAttributes cannot be used on Astro-processed script tags. Therefore, this script tag will be treated as if it has the `is:inline` directive, opting it out of the processing steps and its features.\n\nFor clarity, you might want to add the `is:inline` directive explicitly.\n\nSee docs for more details: https://docs.astro.build/en/guides/client-side-scripts/#script-processing.",
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
lilnasy marked this conversation as resolved.
Show resolved Hide resolved
Range: loc.Range{Loc: n.Attr[0].KeyLoc, Len: len(n.Attr[0].Key)},
})

}
}

func AddComponentProps(doc *astro.Node, n *astro.Node, opts *TransformOptions) {
if n.Type == astro.ElementNode && (n.Component || n.CustomElement) {
for _, attr := range n.Attr {
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/test/client-directive/warn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ test.before(async () => {
result = await transform(FIXTURE);
});

test('logs a warning for using a client directive', () => {
test('reports a warning for using a client directive', () => {
assert.ok(Array.isArray(result.diagnostics));
assert.is(result.diagnostics.length, 1);
assert.is(result.diagnostics.length, 2);
assert.equal(result.diagnostics[0].severity, 2);
assert.match(result.diagnostics[0].text, 'does not need the client:load directive');
});
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler/test/scripts/isinline-hint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { transform } from '@astrojs/compiler';

const FIXTURE = `
<script type="module"></script>
`;

let result;
test.before(async () => {
result = await transform(FIXTURE);
});

test('reports a hint for adding attributes to a script tag without is:inline', () => {
assert.equal(result.diagnostics[0].severity, 4);
assert.match(result.diagnostics[0].text, /Astro processes your script tags to allow using TypeScript and NPM packages/);
});

test.run();
Loading