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

Conversation

lilnasy
Copy link
Contributor

@lilnasy lilnasy commented Feb 16, 2024

Changes

Testing

Added scripts/isinline-hint.ts.

Docs

Would like docs review on the message.

Copy link

changeset-bot bot commented Feb 16, 2024

🦋 Changeset detected

Latest commit: b458421

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving the code change! Will leave decisions on the exact language to others, but this looks pretty good. Maybe a bit long for a lint error.

Looks like you have a test failure by the way!

internal/transform/transform.go Outdated Show resolved Hide resolved
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Happydev <81974850+MoustaphaDev@users.noreply.github.com>
lilnasy and others added 3 commits February 16, 2024 23:01
Copy link
Member

@MoustaphaDev MoustaphaDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! This will help reduce a lot of confusion (and support threads!!) about implicit inline scripts.
Thanks for tackling this!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great idea to help people understand the effect of attributes in their script tags! Some thoughts on the wordingt for you @lilnasy ! 🙌

}
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.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.",
Text: "This script contains an attribute (ex: `type="module"`, `type="text/partytown"`, `async`) or a directive (`define:vars`) and will not be processed and bundled for optimized browser performance.\n\nThis script will be treated as if it has the `is:inline` directive and 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.",

This felt maybe a little "life story" for a helpful hint message? If this is a warning message shown when an attribute exists (because people might not realize it's gonna be inline), then I'd frame the ideas in something like the order above:

  • why this message is being shown (we found an attribute/directive!)
  • what that means (no processing/optimizing for you!)
  • why it means that/how they can understand why this is happening (it's treated like inline!)
  • any other important information to know (no other features, too. See the docs)

I'm also not sure why the recommendation to add the is:inline directive explicitly. What's the benefit? What kind of clarity are you going for? Just to remind yourself that this one will be treated as inline? Unless there's a strong reason to suggest that I'm not seeing, I'd recommend removing.

Copy link
Member

@martrapp martrapp Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Sarah's suggestion:

This script contains an attribute (ex: type="module", type="text/partytown", async) or a directive (define:vars) and ...

Instead of giving examples from the original RFC that might confuse users, we could make this hint dynamic and include current attributes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to add the is:inline directive explicitly

I think this information is valuable as it shows you how to disable the hint (and remove the three dots in the editor view)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shows you how to disable the hint

That's exactly right. It was also part of the RFC that introduced is:inline.

I'm also not sure why the recommendation to add the is:inline directive explicitly.

I guess we wouldn't want to encourage is:inline, because it is almost always an accident. Is that what you're getting at?

Copy link
Member

@sarah11918 sarah11918 Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting at, I have no idea why this is a recommendation for this case, if it's already treated as inline. Why would you bother adding it?

Martin's response that adding it will silence the hint/message is a valid reason, so if it's helpful then something like:

Adding the is:inline explicitly will silence this hint.

BUT, that seems like maybe kind of terrible DX -- you have to go back and add this everywhere or you're going to get editor notation yelling at you for things that are just "how things work"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should clarify, the motivation behind this PR is to add friction to the cases where is:inline is added implicitly.

It is usually accidental, it makes the scripts behave drastically differently, and is only required in very specific use-cases.

Moreover, this "discouragement" was part of the original RFC from years ago.

.changeset/cuddly-pumas-call.md Outdated Show resolved Hide resolved
@martrapp
Copy link
Member

OliverSpeir mentioned this pull request 8 hours ago

#965 adds an additional warning when a <script> element is used with all three: src=..., type="module" and the new data-astro-rerun attribute from withastro/astro#9977.

lilnasy and others added 4 commits February 17, 2024 17:29
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs approves once you remove the extraneous comma! 🚀

internal/transform/transform.go Outdated Show resolved Hide resolved
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
@sarah11918
Copy link
Member

Just checking, because I don't know the required syntax here, but in some of these there are two spaces, and in another, only one. Just in case that matters!
image

@sasoria
Copy link

sasoria commented Feb 21, 2024

I'll add that @lilnasy did this pull request live with other community members. I had a lot of fun watching him do it and learned a lot.

@lilnasy
Copy link
Contributor Author

lilnasy commented Feb 21, 2024

@sarah11918 Good eye, I hadn't noticed!

Go has formatting built-in, it will take care of it on its own sooner or later. Doesn't affect behavior.

@lilnasy lilnasy merged commit 86221d6 into withastro:main Feb 21, 2024
5 checks passed
@lilnasy lilnasy deleted the lint branch February 21, 2024 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants