Skip to content

Commit

Permalink
feat!: add support for resolving tsconfig from the file path
Browse files Browse the repository at this point in the history
Given TSConfig and ESLint both seem to support nested
configuration files and resolving the most relevant one
based off the current file, it seems reasonable that this
plugin should also.

The preferred tsconfig will be the tsconfig closest to the file moving
forward with additional resolution based off the current file's directory
as before included. If we only went with the former, find-up
could potentially not resolve something that was previous
resolvable with an explicit path like
```
- package.json
- src
        - foo.ts
- config
        - tsconfig.json
```

Despite the new default behavior, it seems unlikely to produce a result
that would cause linter complaints that would not have existed within
TypeScript itself. Thus, the major version will not be bumped following
this.

Unfortunately, as the functions for resolving configuration are currently
mocked at the moment, there would have to be a bit of investment to add
those tests. Coupled with the fact that there would probably be a
significant rewrite to support #10, and I'm okay leaving this without
tests for the time being.

Closes #15

BREAKING CHANGE: TSConfig file resolution may resolve to a different
file than before, potentially causing new lint violations.
  • Loading branch information
Limegrass committed Mar 18, 2024
1 parent cbe13f7 commit 3dfbbb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/alias-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ type AliasConfig = {
};
};

function resolveTsconfigFilePath(cwd: string, aliasConfigPath?: string) {
const configFilePath = findUpSync(
[aliasConfigPath, "tsconfig.json", "jsconfig.json"].filter(
Boolean
) as string[],
{ cwd }
);
function resolveTsconfigFilePath(
startDirs: string[],
aliasConfigPath?: string
) {
const configFilePath = startDirs
.map((dir) => {
return findUpSync(
[aliasConfigPath, "tsconfig.json", "jsconfig.json"].filter(
Boolean
) as string[],
{ cwd: dir }
);
})
.find((tsconfigPath) => !!tsconfigPath);

if (!configFilePath) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion src/rules/import-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ const importAliasRule: Rule.RuleModule = {
let aliasesResult: AliasConfig[];
try {
const configFilePath = resolveTsconfigFilePath(
cwd,
[filepath, cwd],
aliasConfigPath
);
const tsconfig = loadTsconfig(configFilePath);
Expand Down

0 comments on commit 3dfbbb6

Please sign in to comment.