Mayil-AI-Sandbox / eslint-v911

Clone of eslint v9.1.1 for debugging
MIT License
0 stars 0 forks source link

Rule Change: `no-restricted-exports` to accept Regex patterns #9

Open btmills opened 1 month ago

btmills commented 1 month ago

What rule do you want to change?

no-restricted-exports

What change do you want to make?

Generate more warnings

How do you think the change should be implemented?

Other

Example code

/*eslint no-restricted-exports: ["error", {
    "restrictedNamedExports": ["/^(?!foo)(?!bar).+$/g"]
}]*/

// Success
export const foo = 1;
export function bar() {return 2;}

// Error
const a = {};
export { a };

What does the rule currently do for this code?

The rule currently accepts only hardcoded strings as restrictions

What will the rule do after it's changed?

The rule will accept also regex patterns as name restrictions

Participation

Additional comments

Remix framework recommends not exporting anything other than their main route functions in route files, otherwise HMR won't work. Sadly, there is no way to enforce that with ESLint currently.

mayil-ai[bot] commented 1 month ago

To enhance the no-restricted-exports rule to accept regular expression patterns for named export restrictions, follow these steps:

  1. Modify the schema in no-restricted-exports.js to accept objects with a pattern property for regex patterns:
schema: [{
    anyOf: [
        {
            type: "object",
            properties: {
                restrictedNamedExports: {
                    type: "array",
                    items: {
                        anyOf: [
                            {
                                type: "string"
                            },
                            {
                                type: "object",
                                properties: {
                                    pattern: {
                                        type: "string"
                                    }
                                },
                                required: ["pattern"],
                                additionalProperties: false
                            }
                        ]
                    },
                    uniqueItems: true
                }
            },
            additionalProperties: false
        },
        // ... (rest of the schema)
    ]
}],
  1. Implement regex pattern matching in the rule logic:
const configuration = context.options[0] || {};
const restrictedNamedExports = configuration.restrictedNamedExports || [];

const restrictedPatterns = restrictedNamedExports
    .filter(item => typeof item === 'object' && item.pattern)
    .map(item => new RegExp(item.pattern));

exportedNames.forEach(name => {
    if (restrictedNamedExports.includes(name) || restrictedPatterns.some(pattern => pattern.test(name))) {
        context.report({
            node,
            messageId: 'restrictedNamed',
            data: { name }
        });
    }
});
  1. Update the rule's documentation to include examples of using regex patterns:
### restrictedNamedExports

Examples of **incorrect** code for the `"restrictedNamedExports"` option with regex patterns:

::: incorrect

```js
/*eslint no-restricted-exports: ["error", {
    "restrictedNamedExports": [{"pattern": "^internal"}, "foo", "bar"]
}]*/

export const internalUtil = 1;

export function foo() {}

export class bar {}

:::



4. Validate the updated rule by running existing tests and writing new tests to cover the regex pattern matching functionality. Make sure all tests pass to confirm the rule works as expected.

### Code snippets to check

- [lib](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib)/[rules](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules)/[no-restricted-exports.js](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules/no-restricted-exports.js):[18-171](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules/no-restricted-exports.js#L18-L171) | Snippet contains schema for 'no-restricted-exports' rule, needs update to accept regex patterns for named exports. 

- [lib](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib)/[rules](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules)/[no-restricted-imports.js](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules/no-restricted-imports.js):[1-552](https://github.com/Mayil-AI-Sandbox/eslint-v911/tree/b105595707abc25e32127f88b11cb0b087c05bb1/lib/rules/no-restricted-imports.js#L1-L552) | This snippet checks for specific import name restrictions and patterns, which is similar to the desired functionality for export name restrictions using regex patterns. It may provide a template or example for implementing the new feature.