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
Next Next commit
add lint for implicit is:inline on script tags
  • Loading branch information
lilnasy committed Feb 16, 2024
commit 253bf6d44cc715837942a52a3de41af2ba8b8334
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(doc, n, &opts, 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(doc *astro.Node, n *astro.Node, opts *TransformOptions, 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