Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add option to allow relative imports #3

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# eslint-plugin-no-relative-base-url-imports
# @opencreek/eslint-plugin
reckter marked this conversation as resolved.
Show resolved Hide resolved

Disalows relative path across the baseUrl of your tsconfig

Expand All @@ -13,16 +13,20 @@ npm i eslint --save-dev
Next, install `eslint-plugin-no-relative-base-url-imports`:

```sh
npm install eslint-plugin-no-relative-base-url-imports --save-dev
npm install @opencreek/eslint-plugin --save-dev
```

```sh
yarn add --dev @opencreek/eslint-plugin
```

## Usage

Add `no-relative-base-url-imports` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
Add `@opencreek` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
reckter marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"plugins": ["no-relative-base-url-imports"]
"plugins": ["@opencreek"]
reckter marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand All @@ -31,11 +35,28 @@ Then configure the rules you want to use under the rules section.
```json
{
"rules": {
"no-relative-base-url-imports/rule-name": 2
"@opencreek/no-relative-imports": [
reckter marked this conversation as resolved.
Show resolved Hide resolved
"error",
{
"baseUrl": "./src"
}
]
}
}
```

## Supported Rules

- Fill in provided rules here
### `@opencreek/no-relative-imports` Disable relative imports.
reckter marked this conversation as resolved.
Show resolved Hide resolved

Config options

```ts
{
"baseUrl": "./src", // The base url that you have set in the tsconfig
"allowLocalImports": "local" // possible values: "local" | "in-base-path".
// "local": Allows local imports (eg.: "./test")
// "in-base-path": Allows everything that does not go back to the base url level (eg: "../../test" in "src/a/b/c/test.ts")
}

```
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const rules = {
export const configs = {
recommended: {
rules: {
"@opencreek/opencreek/no-relative-imports": "error",
"@opencreek/no-relative-imports": "error",
reckter marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
10 changes: 5 additions & 5 deletions lib/rules/no-relative-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export default creator<Options, MessageIds>({
options.baseUrl ?? "."
)

const relativeFileName = fileName.replace(basePath, "")
const levels = relativeFileName.split("/").length - 2
const fileNameInsideBasePath = fileName.replace(basePath, "")
const levels = fileNameInsideBasePath.split("/").length - 2
const levelImport = "../".repeat(levels)

const filePath = path.dirname(fileName)
Expand All @@ -74,7 +74,7 @@ export default creator<Options, MessageIds>({
node.source.value
)
const isInBasePath = filePath.startsWith(basePath)
const absoluteImportPathWithoutBase =
const absoluteImportPathInsideBasePath =
absoluteImportPath.replace(basePath + "/", "")

// we import something outside of the basePath
Expand All @@ -99,7 +99,7 @@ export default creator<Options, MessageIds>({
fix: (fixer) => {
return fixer.replaceText(
node.source,
`"${absoluteImportPathWithoutBase}"`
`"${absoluteImportPathInsideBasePath}"`
)
},
})
Expand All @@ -122,7 +122,7 @@ export default creator<Options, MessageIds>({
fix: (fixer) => {
return fixer.replaceText(
node.source,
`"${absoluteImportPathWithoutBase}"`
`"${absoluteImportPathInsideBasePath}"`
)
},
})
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@opencreek/eslint-plugin-opencreek",
"name": "@opencreek/eslint-plugin",
reckter marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.2.0",
"description": "Disalows relative path across the baseUrl of your tsconfig",
"description": "Disallows relative path across the baseUrl of your tsconfig",
"type": "module",
"keywords": [
"eslint",
Expand All @@ -13,7 +13,7 @@
"build/**"
],
"author": "Opencreek Technology<oss@opencreek.tech>",
"repository": "https://github.com/opencreek/provider-stack",
"repository": "https://github.com/opencreek/eslint-plugin",
reckter marked this conversation as resolved.
Show resolved Hide resolved
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down