-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce rule to check for .ts extension
- Loading branch information
Showing
3 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
title: '@rotki/no-dot-ts-import' | ||
description: description | ||
since: v0.6.0 | ||
--- | ||
|
||
# @rotki/no-dot-ts-import | ||
|
||
> description | ||
## :book: Rule Details | ||
|
||
This rule reports the usage of the .ts extension when importing dependencies. | ||
|
||
- :black_nib:️ The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule. | ||
|
||
<eslint-code-block> | ||
|
||
<!-- eslint-skip --> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script lang="ts" setup> | ||
/* eslint @rotki/no-dot-ts-import: "error" */ | ||
import { something } from '@/packages/something'; | ||
</script> | ||
<!-- ✗ BAD --> | ||
<script lang="ts" setup> | ||
/* eslint @rotki/no-dot-ts-import: "error" */ | ||
import { something } from '@/packages/something.ts'; | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :gear: Options | ||
|
||
```json | ||
{ | ||
"@rotki/no-dot-ts-import": ["error"] | ||
} | ||
``` | ||
|
||
- | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in `@rotki/eslint-plugin` v0.6.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/rotki/eslint-plugin/blob/master/src/rules/no-dot-ts-import.ts) | ||
- [Test source](https://github.com/rotki/eslint-plugin/tree/master/tests/rules/no-dot-ts-import.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createEslintRule } from '../utils'; | ||
|
||
export const RULE_NAME = 'no-dot-ts-import'; | ||
|
||
export type MessageIds = 'invalidTSExtension'; | ||
|
||
export type Options = []; | ||
|
||
export default createEslintRule<Options, MessageIds>({ | ||
create(context) { | ||
return { | ||
ImportDeclaration(node) { | ||
const importDeclaration = node.source.value; | ||
if (!importDeclaration.endsWith('.ts')) | ||
return; | ||
|
||
const lastIndexOfExtension = importDeclaration.lastIndexOf('.ts'); | ||
const replacement = importDeclaration.substring(0, lastIndexOfExtension); | ||
|
||
context.report({ | ||
data: { | ||
import: importDeclaration, | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceText(node.source, `'${replacement}'`); | ||
}, | ||
messageId: 'invalidTSExtension', | ||
node: node.source, | ||
}); | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
description: 'Checks and replaces .ts extension in import statements.', | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
invalidTSExtension: `'{{ import }}' has a .ts extension, please remove it'`, | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
name: RULE_NAME, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { RuleTester } from 'eslint'; | ||
import vueParser from 'vue-eslint-parser'; | ||
import rule from '../../src/rules/no-dot-ts-import'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: vueParser, | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
}, | ||
}, | ||
}); | ||
|
||
tester.run('no-dot-ts-import', rule as never, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script lang="ts" setup> | ||
import { something } from '@/packages/something'; | ||
</script> | ||
`.trim(), | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<script lang="ts" setup> | ||
import { something } from '@/packages/something.ts'; | ||
</script> | ||
`.trim(), | ||
output: ` | ||
<script lang="ts" setup> | ||
import { something } from '@/packages/something'; | ||
</script> | ||
`.trim(), | ||
errors: [ | ||
{ | ||
messageId: 'invalidTSExtension', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |