Skip to content

Commit

Permalink
docs: add docs for pattern-based relative import override configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Limegrass committed Feb 23, 2024
1 parent c3dc8d4 commit 8a2852b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
21 changes: 21 additions & 0 deletions docs/rules/import-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
48 changes: 36 additions & 12 deletions src/rules/import-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,44 @@ const schemaProperties: Record<keyof ImportAliasOptions, JSONSchema4> = {
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.",
},
},
},
},
],
},
},
};
Expand Down

0 comments on commit 8a2852b

Please sign in to comment.