eamodio / vscode-find-related

Finds files related to the current file based on user-defined configuration rules in VS Code
MIT License
30 stars 12 forks source link

JS/TS default rules #8

Open elado opened 6 years ago

elado commented 6 years ago

this is what I use:

    "findrelated.applyRulesets": [
        "js"
    ],
    "findrelated.rulesets": [
        {
            "name": "js",
            "rules": [
                {
                    "pattern": "(.+)\/([^\/]+?)\\.([tj]sx?)$",
                    "locators": [
                        "$1\/{tests,__tests__,__test__}/$2.spec.$3"
                    ]
                },
                {
                    "pattern": "(.+)\/(?:tests|__tests__|__test__)\/([^\/]+?)\\.spec\\.([tj]sx?)$",
                    "locators": [
                        "$1\/$2.$3"
                    ]
                }
            ]
        }
    ],

works for my projects, can probably be improved, but a good start.

eamodio commented 6 years ago

@elado why the tests,__tests__,__test__ to handle differently named test folders?

forivall commented 4 years ago

yeah, they cover different common patterns for the defaults used by javascript test runners (i typically use ava and jest).

I modified the rules to:

"rules": [
      {
        "pattern": "(.+)\/([^\/]+?)\\.([tj]sx?)$",
        "locators": [
          "$1/{test,tests,__tests__,__test__}/$2{.spec,.test,}.$3"
        ]
      },
      {
        "pattern": "src/(.+)\/([^\/]+?)\\.([tj]sx?)$",
        "locators": [
          "src/$1/{test,tests,__tests__,__test__}/$2{.spec,.test,}.$3",
          "{test,tests,__tests__,__test__}/$1/$2{.spec,.test,}.$3",
        ]
      },
      {
        "pattern": "(.+)\/(?:tests?|__tests?__)\/((?:.+?/)?(?:[^\/]+?))(?:\\.spec|\\.test)?\\.([tj]sx?)$",
        "locators": [
          "$1/$2.$3",
          "src/$1/$2.$3"
        ]
      },
      {
        "pattern": "(?:tests?|__tests?__)\/((?:.+?/)?(?:[^\/]+?))(?:\\.spec|\\.test)?\\.([tj]sx?)$",
        "locators": [
          "$1.$2",
          "src/$1.$2"
        ]
      }
    ]
diminutivesloop commented 4 years ago

This new ruleset should also handle the common pattern of including spec files in the same directory as the code under test. This is pattern is widely followed in the Angular community (see here for an example).

I'm using the following ruleset for javascript files in my Angular projects:

{
  "name": "javascript",
  "rules": [
    {
      "pattern": "(.*)\\.spec\\.([tj]sx?)$",
      "locators": [
        "$1.$2",
      ]
    },
    {
      "pattern": "(.*)\\.([tj]sx?)$",
      "locators": [
        "$1.spec.$2",
      ]
    }
  ]
}

Also I'm no regex wizard but the patterns mentioned before look like they might be more complicated than they need to be.

github-actions[bot] commented 1 year ago

This issue needs more information and has not had recent activity. Please provide the missing information or it will be closed in 7 days. Thanks!