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

feat(linter/no_restricted_imports): add the no_restricted_imports rules #7629

Merged
merged 5 commits into from
Dec 10, 2024

Conversation

Spoutnik97
Copy link
Contributor

add first test cases related to the 'paths' config

Note that the test cases and configuration format is not the same as the original ESLint rule.
What is the oxc team strategy to develop such a rule? Is it ok to adapt the config format ?


I started a discussion here : #7534 (comment)
I copy/paste the content here. Maybe it is more relevant?

I am working to implement this no-restricted-imports rule.
I have several problems:

How to handle multiple format configuration in rust?
The eslint config can be: "fs", ["fs"], or {paths: [{name: "fs"}]}. But Rust needs only one type. I don't know how to do this in rust.

Is it ok to cover only the {paths: [{name: "fs"}]} case ?

How to parse this config with the from_configuration method?
Here is what I have done:

   fn from_configuration(value: serde_json::Value) -> Self {
        let mut paths = Vec::new();
        let mut patterns = Vec::new();

        if let Some(obj) = value.as_object() {
            // Handle paths array
            if let Some(paths_value) = obj.get("paths") {
                if let Some(paths_array) = paths_value.as_array() {
                    for path_value in paths_array {
                        if let Ok(path) = serde_json::from_value(path_value.clone()) {
                            paths.push(path);
                        }
                    }
                }
            }

            // Handle patterns array
            if let Some(patterns_value) = obj.get("patterns") {
                if let Some(patterns_array) = patterns_value.as_array() {
                    for pattern_value in patterns_array {
                        if let Ok(pattern) = serde_json::from_value(pattern_value.clone()) {
                            patterns.push(pattern);
                        }
                    }
                }
            }
        }

        Self { paths, patterns }
    }

But here is my result:

[RestrictedPath { name: "foo", import_names: None, message: None }]

-------- rule config --------
{
  "paths": [
    {
      "name": "foo",
      "importNames": [
        "AllowedObject"
      ]
    }
  ]
}

Note the "None" values

Copy link

graphite-app bot commented Dec 3, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Dec 3, 2024
@Spoutnik97 Spoutnik97 force-pushed the linter/no_restricted_imports branch from 8301062 to d3f2c74 Compare December 3, 2024 23:06
Copy link

codspeed-hq bot commented Dec 3, 2024

CodSpeed Performance Report

Merging #7629 will not alter performance

Comparing Spoutnik97:linter/no_restricted_imports (c9a1134) with main (8883968)

Summary

✅ 29 untouched benchmarks

@Spoutnik97 Spoutnik97 force-pushed the linter/no_restricted_imports branch 2 times, most recently from 1d5880b to 00be669 Compare December 4, 2024 22:48
@camc314
Copy link
Contributor

camc314 commented Dec 5, 2024

The eslint config can be: "fs", ["fs"], or {paths: [{name: "fs"}]}. But Rust needs only one type. I don't know how to do this in rust.

probably want to normalize them?

e.g. match on the json value type

match value { 
Value::Array => { ... } 
Value::String => { ... } 

and normalize it into what you want it to be

@Spoutnik97 Spoutnik97 force-pushed the linter/no_restricted_imports branch from 00be669 to e2c3acc Compare December 5, 2024 21:14
@Spoutnik97
Copy link
Contributor Author

Spoutnik97 commented Dec 5, 2024

@camc314 thanks, I'll try it to accept other configs.

I am still fighting with the test which failed :

---- size_asserts stdout ----
thread 'size_asserts' panicked at crates/oxc_linter/src/lib.rs:60:5:
assertion failed: std::mem::size_of::<RuleEnum>() == 16


failures:
    size_asserts

Whereas I tried to improve performances by replacing Vec to a Box in the struct, I still have the error, and I don't know how to improve it. Could you give me some help about it please?

@Spoutnik97 Spoutnik97 force-pushed the linter/no_restricted_imports branch from e2c3acc to 3acf3ec Compare December 5, 2024 21:23
@Spoutnik97
Copy link
Contributor Author

Ok I found a solution. Tell me if it looks correct

@Boshen Boshen requested a review from camc314 December 8, 2024 05:55
@Boshen Boshen merged commit 3d5f0a1 into oxc-project:main Dec 10, 2024
25 checks passed
Boshen added a commit that referenced this pull request Dec 10, 2024
## [0.15.0] - 2024-12-10

- 39b9c5d linter: [**BREAKING**] Remove unmaintained security plugin
(#7773) (Boshen)

### Features

- 065f7dc linter: Support `expectTypeOf`, `assert` and `assertType` in
`vitest/expect-expect` (#7742) (Yuichiro Yamashita)
- 3d5f0a1 linter/no_restricted_imports: Add the no_restricted_imports
rules (#7629) (Guillaume Piedigrossi)

### Bug Fixes

- ad27b20 linter: Only resolve esm files for import plugin (#7720)
(Boshen)
- 5e6053f linter: False positive in `eslint/yoda` (#7719) (dalaoshu)

### Refactor

- c6a19aa linter: Remove unused `serde` features (#7738) (Boshen)
- b9a2b35 linter: Remove `aho-corasick` (#7718) (Boshen)

### Testing

- 62f0a22 linter: Port `react-jsx-uses-vars` rules to no_unused_vars
(#7731) (Tyler Earls)
- 02f9903 linter: Add regression tests for `import/namespace` (#7723)
(dalaoshu)

Co-authored-by: Boshen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linter Area - Linter C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants