Skip to content

feat(valid-scripts): add valid-scripts rule (#839) #903

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ic4l4s9c
Copy link

@ic4l4s9c ic4l4s9c commented Feb 19, 2025

This change adds a new valid-scripts rule, which is also included in the recommended config.

PR Checklist

Overview

This rule checks for "scripts" field to be an non-empty object.

This rule checks that each property in "scripts" field:

  • Is a string rather than any other data type
  • Is not empty

🖤

This change adds a new `valid-scripts` rule, which is also included in the `recommended` config.
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.83%. Comparing base (b2cebc2) to head (e7f830f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #903      +/-   ##
==========================================
+ Coverage   98.77%   98.83%   +0.06%     
==========================================
  Files          21       22       +1     
  Lines        1222     1287      +65     
  Branches      142      151       +9     
==========================================
+ Hits         1207     1272      +65     
  Misses         15       15              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ic4l4s9c ic4l4s9c marked this pull request as ready for review February 19, 2025 13:04
Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

😱 sorry for missing this for multiple weeks! This generally looks great to me - mostly small touchups. I think the only big point is whether we should validate that multiple keys aren't identical (IMO we should, but open to thoughts?).

🖤

emptyScript:
'values contained in "scripts" object should not be empty',
emptyScriptsField: '"scripts" field should not be empty.',
incorrectTypeScript:
Copy link
Owner

Choose a reason for hiding this comment

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

[Praise] Haha, "TypeScript". I thought this was cute. 🙂

messageId: "incorrectTypeScript",
node: node.value as unknown as ESTree.Node,
});
return;
Copy link
Owner

Choose a reason for hiding this comment

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

[Question] Why return;? If multiple scripts are incorrect, we'd want to get reports on all of them I think.

E.g. here I'd expect the values of both script1 and script3 to have complaints:

"scripts": {
    "script1": "",
    "script2": "...",
    "script3": "",
}

(here and the other return)

Comment on lines +40 to +43
{
code: `{ "scripts": { "test": undefined }}`,
errors: [{ messageId: "incorrectTypeScript" }],
},
Copy link
Owner

Choose a reason for hiding this comment

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

[Testing] No need, undefined isn't valid JSON.

Suggested change
{
code: `{ "scripts": { "test": undefined }}`,
errors: [{ messageId: "incorrectTypeScript" }],
},

I'm actually surprised this doesn't cause any "hey you have invalid stuff" complaint in the tester...

errors: [{ messageId: "incorrectTypeScript" }],
},
],
valid: [
Copy link
Owner

Choose a reason for hiding this comment

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

[Testing] Some more valid cases to think of:

  • No "scripts" key at all
  • Empty "scripts"
  • Just one value in "scripts"

import { ruleTester } from "./ruleTester.js";

ruleTester.run("valid-scripts", rule, {
invalid: [
Copy link
Owner

Choose a reason for hiding this comment

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

[Testing] Some more invalid cases to think of:

  • 1 incorrect value + 2 correct values
  • 2 incorrect values
  • duplicate values
  • 2 incorrect values + 1 correct value

Copy link
Owner

Choose a reason for hiding this comment

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

[Feature] Fun fact, it is technically valid JSON to have duplicate keys:

"scripts": {
  "this-is": "fine",
  "this-is": "fine"
}

cc @michaelfaith to check me here, but IMO this should be reported on by valid-scripts.


<!-- end auto-generated rule header -->

This rule applies two validations to each property in `"scripts"` field:
Copy link
Owner

Choose a reason for hiding this comment

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

[Docs] Just nitpicking a little bit for clarity, WDYT?

Suggested change
This rule applies two validations to each property in `"scripts"` field:
This rule checks that if a `"scripts"` property exists, it is a non-empty object.
Additionally, for each of its script properties:

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author Needs an action taken by the original poster label Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author Needs an action taken by the original poster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature: Bring in a valid-scripts rule
2 participants