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

New Rule: keys-patterns #55

Open
1 task done
dananaprey opened this issue Nov 10, 2024 · 5 comments
Open
1 task done

New Rule: keys-patterns #55

dananaprey opened this issue Nov 10, 2024 · 5 comments
Labels

Comments

@dananaprey
Copy link

dananaprey commented Nov 10, 2024

Rule details

Allows to set a keys patters to specific JSON files

What type of rule is this?

Suggests an alternate way of doing something

Example code

Real life example:

I have a JSON files for application localization

{
  "firstLocalizationKey_i18n": "Some translation 1",
  "secondLocalizationKey_i18n": "Some translation 2",
  "thirdLocalizationKey_i18n": "Some translation 3",
  ...
}

I want to prohibit adding keys to it that do not match a certain pattern (for this example /(^[a-zA-Z]+_i18n$)/)

I propose to add rule which add an ability to set patters to specific JSON files

Rule example:

{
  "keys-patterns": [
    "error",
    {
      "/(^[a-zA-Z]+_i18n$)/": ["./src/i18n/en.json", "./src/i18n/it.json"],
      "/(^[a-zA-Z]$)/": ["./src/some.json"],
    }
  ]
}

Different patterns can be assigned to different files. If you do not specify anything for file, there will be no pattern

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

In my example, there is a JSON file without nesting. If we have nesting maybe this rule will not be necessary, a schema is already needed there, but we can discuss it here

@fasttime fasttime added this to Triage Nov 12, 2024
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Nov 12, 2024
@fasttime fasttime removed the status in Triage Nov 12, 2024
@fasttime fasttime moved this to Needs Triage in Triage Nov 12, 2024
@nzakas nzakas moved this from Needs Triage to Triaging in Triage Nov 12, 2024
@nzakas nzakas moved this from Triaging to Feedback Needed in Triage Nov 12, 2024
@nzakas
Copy link
Member

nzakas commented Nov 12, 2024

Thanks for the suggestion. I understand what you're trying to do, but I'm not sure it's universal enough to include in this plugin. (Keep in mind, you can always just write the rule yourself -- it doesn't need to be in this plugin for it to exist.)

{
  "keys-patterns": [
    "error",
    {
      "/(^[a-zA-Z]+_i18n$)/": ["./src/i18n/en.json", "./src/i18n/it.json"],
      "/(^[a-zA-Z]$)/": ["./src/some.json"],
    }
  ]
}

I'm not sure what this is supposed to do. If you want different settings for different files, the canonical ESLint way of doing it would be:

export default [
  {
    files: ["./src/i18n/en.json", "./src/i18n/it.json"],
    rules: {
      "keys-patterns": [ "error", "/(^[a-zA-Z]+_i18n$)/"]
    }
  },
  {
    files: ["./src/some.json"],
    rules: {
      "keys-patterns": [ "error", "/(^[a-zA-Z]$)/"],
    }
  },
];

@eslint/eslint-team thoughts?

@JoshuaKGoldberg
Copy link

As with #54, agreed that this seems specific to one library's use cases. -1 from me on the specific rule.

On the other hand, I think there could be utility in adding an equivalent to no-restricted-syntax for JSON. Please forgive my forgetting/lacking familiarity with the new ESLint languages, is that something that can be done now?

  • If no-restricted-syntax already works with JSON, maybe this is more a docs issue on explaining how to use it?
  • If not, could it be?

@mdjermanovic
Copy link
Member

I agree this doesn't seem universal to include in this plugin.

  • If no-restricted-syntax already works with JSON, maybe this is more a docs issue on explaining how to use it?

Yes, the core no-restricted-syntax rule can be used with any language. I think we can make it official by updating the docs for this rule. @nzakas what do you think?

This config should work for the example from the original post:

import json from "@eslint/json";

export default [
    {
        files: ["**/*.json"],
        plugins: {
            json
        },
        language: "json/json"
    },
    {
        files: ["src/i18n/en.json", "src/i18n/it.json"],
        rules: {
            "no-restricted-syntax": [
                "error",
                {
                    selector: "Member > String[value!=/^[a-zA-Z]+_i18n$/].name",
                    message: "Object keys should match /^[a-zA-Z]+_i18n$/"
                }
            ]
        }
    },
    {
        files: ["src/some.json"],
        rules: {
            "no-restricted-syntax": [
                "error",
                {
                    selector: "Member > String[value!=/^[a-zA-Z]$/].name",
                    message: "Object keys should match /^[a-zA-Z]$/"
                }
            ]
        }
    }
];

@dananaprey
Copy link
Author

Thanks, I'll try to use the no-restricted-syntax rule to achieve my goal!

@nzakas
Copy link
Member

nzakas commented Nov 13, 2024

Yes, the core no-restricted-syntax rule can be used with any language. I think we can make it official by updating the docs for this rule. @nzakas what do you think?

Yes, we can do that. At some point, we probably want to split it out of the core into its own plugin, though, to avoid this ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Feedback Needed
Development

No branches or pull requests

4 participants