From 8a2852bf2ff3efa0413ce310b186ca850dea4fa4 Mon Sep 17 00:00:00 2001 From: limegrass Date: Fri, 23 Feb 2024 03:39:29 -0800 Subject: [PATCH] docs: add docs for pattern-based relative import override configuration --- docs/rules/import-alias.md | 21 +++++++++++++++++ src/rules/import-alias.ts | 48 ++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/docs/rules/import-alias.md b/docs/rules/import-alias.md index d95b207..982f656 100644 --- a/docs/rules/import-alias.md +++ b/docs/rules/import-alias.md @@ -68,6 +68,8 @@ your usage if this does not serve your needs. ### Enabling relative import overrides +#### Path-based overrides + It is possible to allow relative imports for some paths if desires through configuring a `relativeImportOverrides` configuration parameter on the rule. Each configuration requires a path and a depth to be specified, where a depth of `0` allows imports of sibling modules, @@ -107,3 +109,22 @@ In `./src/foo` with `path: "src"` 1. `import "./bar"` for `./src/bar` when `depth` \>= `0`. 2. `import "./bar/baz"` when `depth` \>= `0`. 3. `import "../bar"` when `depth` \>= `1`. + +#### Pattern-based overrides + +Regular expression patterns serve as a potential alternative to path overrides. + +With a configuration like `{ pattern: "index.ts", depth: 0 }` + +1. Relative paths can be used in files such as `./src/index.ts`. +1. Relative paths can be used any file in the folder `./src/index.ts/*`. +1. Relative paths can NOT be used in files such as `./src/foo.ts`. + +With a configuration like `{ pattern: "index\\.(ts|js)$" depth: 0 }` + +1. Relative paths can be used in any file that ends with `index.js` or `index.ts`. +1. Relative paths can be NOT in the folder `./src/index.ts/*`. +1. Relative paths can be NOT used in `./src/foo.ts`. + +If a file matches by both patterns and paths, the maximum depth allowed is simply +the largest of all matched overrides. diff --git a/src/rules/import-alias.ts b/src/rules/import-alias.ts index cefbd10..638b6c3 100644 --- a/src/rules/import-alias.ts +++ b/src/rules/import-alias.ts @@ -205,20 +205,44 @@ const schemaProperties: Record = { type: "array", default: [], items: { - type: "object", - properties: { - path: { - type: "string", - description: - "The starting path from which a relative depth is accepted.", + anyOf: [ + { + type: "object", + properties: { + path: { + type: "string", + description: + "The starting path from which a relative depth is accepted." + + " Required if `pattern` is not provided", + }, + depth: { + type: "number", + description: + "A positive number which represents the" + + " relative depth that is acceptable for the associated path.", + }, + }, }, - depth: { - type: "number", - description: - "A positive number which represents the" + - " relative depth that is acceptable for the associated path.", + + { + type: "object", + properties: { + pattern: { + type: "string", + description: + "The pattern to match against the filename for from" + + " which a relative depth is accepted." + + " Required if `path` is not provided", + }, + depth: { + type: "number", + description: + "A positive number which represents the" + + " relative depth that is acceptable for the associated path.", + }, + }, }, - }, + ], }, }, };