Skip to content

Commit

Permalink
style: update prettier@2 to prettier@3, reformat all files
Browse files Browse the repository at this point in the history
prettier@3 defaults trailingComma to on, which is nicer anyways.
Since this change is accepted, all files are formatted with the new
style using `fd . --exec npx prettier --write;`.

This change was spurred on by the update included in #18.
  • Loading branch information
Limegrass committed Dec 1, 2024
1 parent 00ec691 commit 15a69ce
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 91 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Encourage use of defined aliases in TSConfig/JSConfig through ESLint.

## Why

- Automatic imports by tsserver resolve to relative paths that can be normalized.
- It's easier to refactor by finding and replacing an absolute module path
without worrying about crafting the regex for `../` and `./`
- Automatic imports by tsserver resolve to relative paths that can be normalized.
- It's easier to refactor by finding and replacing an absolute module path
without worrying about crafting the regex for `../` and `./`

## Requirements

- Node 14+
- Node 14+

## Install

Expand All @@ -32,8 +32,8 @@ Check the [rules documentation][docs-import-alias] for further configuration.
{
"plugins": ["@limegrass/import-alias"],
"rules": {
"@limegrass/import-alias/import-alias": "error"
}
"@limegrass/import-alias/import-alias": "error",
},
}
```

Expand All @@ -44,8 +44,8 @@ The configuration above is also equivalent to
{
"extends": [
// ... - your other extends, such as `"eslint:recommended"`
"plugin:@limegrass/import-alias/recommended"
]
"plugin:@limegrass/import-alias/recommended",
],
}
```

Expand Down
30 changes: 15 additions & 15 deletions docs/rules/import-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Given the following `tsconfig.json`
// ...
"paths": {
"#src/*": ["src/*"],
"#rules/*": ["src/rules/*"]
}
}
"#rules/*": ["src/rules/*"],
},
},
}
```

Expand Down Expand Up @@ -55,10 +55,10 @@ parameter of a function named `potato` for aliasing.
"@limegrass/import-alias/import-alias": [
"error",
{
"aliasImportFunctions": ["require", "mock", "potato"]
}
]
}
"aliasImportFunctions": ["require", "mock", "potato"],
},
],
},
}
```

Expand All @@ -79,10 +79,10 @@ configuration key. An example for a tsconfig found in a `config` folder of a pro
"@limegrass/import-alias/import-alias": [
"error",
{
"aliasConfigPath": "config/tsconfig.json"
}
]
}
"aliasConfigPath": "config/tsconfig.json",
},
],
},
}
```

Expand Down Expand Up @@ -110,10 +110,10 @@ The follow example would allow sibling imports for the entire project.
"@limegrass/import-alias/import-alias": [
"error",
{
"relativeImportOverrides": [{ "path": ".", "depth": 0 }]
}
]
}
"relativeImportOverrides": [{ "path": ".", "depth": 0 }],
},
],
},
}
```

Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export default [
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier",
"plugin:@limegrass/import-alias/recommended",
"prettier",
),
),
{
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"jest": "^29.7.0",
"jest-mock": "^29.7.0",
"lint-staged": "^12.3.5",
"prettier": "^2.5.1",
"prettier": "^3",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.4",
"ts-node": "^10.7.0",
Expand Down
12 changes: 6 additions & 6 deletions src/alias-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type AliasConfig = {
function loadTsconfig(
eslintConfigPath: string,
aliasConfigPath: string | undefined,
codeFilePath: string
codeFilePath: string,
) {
let config: ConfigLoaderResult;
try {
Expand All @@ -26,7 +26,7 @@ function loadTsconfig(
config = loadConfig(aliasConfigPath);
} else {
config = loadConfig(
joinPath(eslintConfigPath, aliasConfigPath)
joinPath(eslintConfigPath, aliasConfigPath),
);
}
} else {
Expand All @@ -35,15 +35,15 @@ function loadTsconfig(
} catch (error) {
if (error instanceof SyntaxError) {
throw new Error(
`SyntaxError in TSConfig/JSConfig: ${error.message}`
`SyntaxError in TSConfig/JSConfig: ${error.message}`,
);
}
throw error;
}

if (config.resultType !== "success") {
throw new Error(
`validate tsconfig or jsconfig provided and ensure compilerOptions.baseUrl is set: ${config.message}`
`validate tsconfig or jsconfig provided and ensure compilerOptions.baseUrl is set: ${config.message}`,
);
}

Expand All @@ -52,7 +52,7 @@ function loadTsconfig(

function loadAliasConfigs(
config: ConfigLoaderSuccessResult,
projectBaseDir: string
projectBaseDir: string,
): AliasConfig[] {
return Object.entries(config.paths).reduce(
(configs, [aliasGlob, aliasPaths]) => {
Expand All @@ -67,7 +67,7 @@ function loadAliasConfigs(
});
return configs;
},
[] as AliasConfig[]
[] as AliasConfig[],
);
}

Expand Down
44 changes: 22 additions & 22 deletions src/rules/import-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function isPermittedRelativeImport(
importModuleName: string,
relativeImportOverrides: RelativeImportConfig[],
filepath: string,
projectBaseDir: string
projectBaseDir: string,
) {
const isRelativeImport =
importModuleName.length > 0 && importModuleName[0] !== ".";
Expand All @@ -32,7 +32,7 @@ function isPermittedRelativeImport(

const importParts = importModuleName.split("/");
const relativeDepth = importParts.filter(
(moduleNamePart) => moduleNamePart === ".."
(moduleNamePart) => moduleNamePart === "..",
).length;
const relativeFilepath = relative(projectBaseDir, filepath);

Expand All @@ -53,13 +53,13 @@ function isPermittedRelativeImport(
function getAliasSuggestion(
importModuleName: string,
aliasConfigs: AliasConfig[],
absoluteDir: string
absoluteDir: string,
) {
const currentAliasConfig: AliasConfig | undefined = aliasConfigs.find(
({ alias }) => {
const [baseModulePath] = importModuleName.split("/");
return baseModulePath === alias;
}
},
);

let absoluteModulePath: string | undefined = undefined;
Expand All @@ -75,15 +75,15 @@ function getAliasSuggestion(
const bestAliasConfig = getBestAliasConfig(
aliasConfigs,
currentAliasConfig,
absoluteModulePath
absoluteModulePath,
);

if (bestAliasConfig && bestAliasConfig !== currentAliasConfig) {
return slash(
absoluteModulePath.replace(
bestAliasConfig.path.absolute,
bestAliasConfig.alias
)
bestAliasConfig.alias,
),
);
}
}
Expand All @@ -92,7 +92,7 @@ function getAliasSuggestion(
function getBestAliasConfig(
aliasConfigs: AliasConfig[],
currentAlias: AliasConfig | undefined,
absoluteModulePath: string
absoluteModulePath: string,
) {
const importPathParts = absoluteModulePath.split(pathSep);
return aliasConfigs.reduce((currentBest, potentialAlias) => {
Expand All @@ -101,7 +101,7 @@ function getBestAliasConfig(
(isValid, aliasPathPart, index) => {
return isValid && importPathParts[index] === aliasPathPart;
},
true
true,
);
const isMoreSpecificAlias =
!currentBest ||
Expand Down Expand Up @@ -303,7 +303,7 @@ const importAliasRule: Rule.RuleModule = {
return reportProgramError(
"a filepath must be provided, try with --stdin-filename, " +
"call eslint on a file, " +
"or save your buffer as a file and restart eslint in your editor."
"or save your buffer as a file and restart eslint in your editor.",
);
}

Expand Down Expand Up @@ -331,7 +331,7 @@ const importAliasRule: Rule.RuleModule = {

const getReportDescriptor = (
[moduleStart, moduleEnd]: [number, number],
importModuleName: string
importModuleName: string,
) => {
// preserve user quote style
const quotelessRange: AST.Range = [moduleStart + 1, moduleEnd - 1];
Expand All @@ -341,7 +341,7 @@ const importAliasRule: Rule.RuleModule = {
importModuleName,
relativeImportOverrides,
filepath,
projectBaseDir
projectBaseDir,
)
) {
return undefined;
Expand All @@ -350,7 +350,7 @@ const importAliasRule: Rule.RuleModule = {
const aliasSuggestion = getAliasSuggestion(
importModuleName,
aliasesResult,
absoluteDir
absoluteDir,
);

if (aliasSuggestion) {
Expand All @@ -359,7 +359,7 @@ const importAliasRule: Rule.RuleModule = {
fix: (fixer: Rule.RuleFixer) => {
return fixer.replaceTextRange(
quotelessRange,
aliasSuggestion
aliasSuggestion,
);
},
};
Expand All @@ -370,7 +370,7 @@ const importAliasRule: Rule.RuleModule = {
if (!isAllowBaseUrlResolvedImport) {
const joinedModulePath = joinPath(
projectBaseDir,
importModuleName
importModuleName,
);
let moduleExists = false;
try {
Expand All @@ -387,22 +387,22 @@ const importAliasRule: Rule.RuleModule = {
const aliasConfig = getBestAliasConfig(
aliasesResult,
undefined,
joinedModulePath
joinedModulePath,
);

if (aliasConfig) {
const suggestedPathImport = slash(
joinedModulePath.replace(
aliasConfig.path.absolute,
aliasConfig.alias
)
aliasConfig.alias,
),
);
return {
message: `import ${importModuleName} can be written as ${suggestedPathImport}`,
fix: (fixer: Rule.RuleFixer) => {
return fixer.replaceTextRange(
quotelessRange,
suggestedPathImport
suggestedPathImport,
);
},
};
Expand All @@ -421,12 +421,12 @@ const importAliasRule: Rule.RuleModule = {
node:
| ImportDeclaration
| ExportAllDeclaration
| ExportNamedDeclaration
| ExportNamedDeclaration,
) => {
if (node.source?.range && typeof node.source.value === "string") {
const suggestion = getReportDescriptor(
node.source.range,
node.source.value
node.source.value,
);

if (suggestion) {
Expand Down Expand Up @@ -455,7 +455,7 @@ const importAliasRule: Rule.RuleModule = {
) {
const suggestion = getReportDescriptor(
importNameNode.range,
importNameNode.value
importNameNode.value,
);

if (suggestion) {
Expand Down
4 changes: 2 additions & 2 deletions tests/rules/import-alias.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function mockReaddir(filepath: string) {
export function getMockAliasConfig(
pathModule: typeof path,
platform: "win32" | "posix",
projectDir: string
projectDir: string,
) {
return [
{
Expand All @@ -51,7 +51,7 @@ export function getMockAliasConfig(
absolute: pathModule[platform].join(
projectDir,
"src",
"sub-directory"
"sub-directory",
),
},
},
Expand Down
Loading

0 comments on commit 15a69ce

Please sign in to comment.