Skip to content

Commit

Permalink
Add lint rule for implicit is:inline on script tags. (#970)
Browse files Browse the repository at this point in the history
* add lint for implicit is:inline on script tags

* add changeset

* adjust exisitng test

* add test

* remove unused args

* Apply suggestions from code review

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* Apply suggestions from code review

Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>

* update test: `NPM` -> `npm`

* exclude external scripts from the diagnsotic

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* shorten the lint message

* comma

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
  • Loading branch information
4 people committed Feb 21, 2024
1 parent a90d99e commit 86221d6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
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 rule to display a message when attributes are added to a script tag, explaining that the script will be treated as `is:inline`.
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
14 changes: 14 additions & 0 deletions internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 @@ -408,6 +409,19 @@ 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) {
if len(n.Attr) == 1 && n.Attr[0].Key == "src" {
return
}
h.AppendHint(&loc.ErrorWithRange{
Code: loc.HINT,
Text: "This script will be treated as if it has the `is:inline` directive because it contains an attribute. Therefore, features that require processing (e.g. using TypeScript or npm packages in the script) are unavailable.\n\nSee docs for more details: https://docs.astro.build/en/guides/client-side-scripts/#script-processing.\n\nAdd the `is:inline` directive explicitly to silence this hint.",
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
17 changes: 17 additions & 0 deletions packages/compiler/test/scripts/isinline-hint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { transform } from '@astrojs/compiler';

test('reports a hint for adding attributes to a script tag without is:inline', async () => {
const result = await transform(`<script type="module"></script>`);
assert.equal(result.diagnostics[0].severity, 4);
assert.match(result.diagnostics[0].text, /\#script-processing/);
});

test('does not report a diagnostic for the src attribute', async () => {
const result = await transform(`<script src="/external.js"></script>`);
console.log(result.diagnostics)
assert.equal(result.diagnostics.length, 0);
});

test.run();

0 comments on commit 86221d6

Please sign in to comment.