Skip to content

Commit

Permalink
feat: introduce rule to check for .ts extension
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsos committed Dec 10, 2024
1 parent fbe6551 commit 8410e68
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/rules/no-dot-ts-import.md
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)
46 changes: 46 additions & 0 deletions src/rules/no-dot-ts-import.ts
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,
});
46 changes: 46 additions & 0 deletions tests/rules/no-dot-ts-import.ts
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',
},
],
},
],
});

0 comments on commit 8410e68

Please sign in to comment.