Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/profico/eslint-plugin-profico.git"
},
"version": "2.0.0",
"version": "2.0.1-0",
"description": "ESLint plugin and configurations by Profico",
"main": "lib/index.js",
"scripts": {
Expand Down
45 changes: 45 additions & 0 deletions src/rules/type-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Rule } from "eslint";
import { TSESTree } from "@typescript-eslint/utils";
import { categorizeImports, processImportGroup } from "../utils/imports";

const typeImports: Rule.RuleModule = {
meta: {
fixable: "code",
type: "problem",
messages: {
mixedTypeValueImports:
"Type imports should be separated from value imports within each group.",
},
docs: {
url: "https://github.com/profico/eslint-plugin-profico#type-imports",
},
},
create(context) {
return {
Program(node) {
const importNodes = node.body.filter(
(stmt): stmt is TSESTree.ImportDeclaration =>
stmt.type === "ImportDeclaration",
);

if (importNodes.length === 0) {
return;
}

const { absoluteImports, relativeImports } =
categorizeImports(importNodes);

// Process absolute imports (may need trailing newline if relative imports exist)
processImportGroup(
absoluteImports,
context,
relativeImports.length > 0,
);
// Process relative imports (never needs trailing newline as it's the last group)
processImportGroup(relativeImports, context, false);
},
};
},
};

export default typeImports;
24 changes: 24 additions & 0 deletions src/tests/type-imports/invalid-cases/base.invalid-case.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type * as ReactTypes from 'react';

import React, { useState } from 'react';
import Form, { FormProps, FormSubmitHandler } from 'components/Forms/Form';
import type Lalalal from 'lalala/lalalal';
import AddNicknameDialogHeader from 'components/SvgIcons/AddNicknameDialogHeader';
import useToggleState from 'utils/hooks/useToggleState';
import useBreakpoint from 'utils/hooks/useBreakpoint';
import fetch from 'utils/static/fetchRelative';
import colors from 'styles/themes/colors';
import { type Kokos } from 'kokos/kokos';
import { Grid, Button, Typography } from '@mui/material';
import { PostNicknameData } from 'types/User';
import { type Kokos } from 'kokos/trokos';
import type NekiNoviType from './precooltyNekiNoviTypepe';

import NicknameInput from './NicknameInput';
import type PreCoolType from './precooltype';
import SuccessDialog from './SuccessDialog';
import ConfirmDialog from './ConfirmDialog';
import { something } from './something';
import { type barakokula, someValue } from './barakokula';


5 changes: 5 additions & 0 deletions src/tests/type-imports/invalid-cases/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from "path";

import { readAllTxtFilesInDir } from "../../../utils/tests";

export default readAllTxtFilesInDir(path.join(__dirname, "."), ["index.ts"]);
28 changes: 28 additions & 0 deletions src/tests/type-imports/type-imports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { RuleTester } from "eslint";

import typeImports from "../../rules/type-imports";

import invalidCases from "./invalid-cases";
import validCases from "./valid-cases";

const tester = new RuleTester({
languageOptions: {
parser: require("@typescript-eslint/parser"),
parserOptions: {
sourceType: "module",
ecmaVersion: 2015,
},
},
});

tester.run("profico/type-imports", typeImports, {
valid: validCases,
invalid: invalidCases.map<RuleTester.InvalidTestCase>((code, index) => ({
code,
output: validCases[index],
errors: [
{ messageId: "mixedTypeValueImports" },
{ messageId: "mixedTypeValueImports" },
],
})),
});
23 changes: 23 additions & 0 deletions src/tests/type-imports/valid-cases/base.valid-case.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import AddNicknameDialogHeader from 'components/SvgIcons/AddNicknameDialogHeader';
import colors from 'styles/themes/colors';
import useBreakpoint from 'utils/hooks/useBreakpoint';
import useToggleState from 'utils/hooks/useToggleState';
import fetch from 'utils/static/fetchRelative';
import { Grid, Button, Typography } from '@mui/material';
import Form, { FormProps, FormSubmitHandler } from 'components/Forms/Form';
import React, { useState } from 'react';
import { PostNicknameData } from 'types/User';

import type * as ReactTypes from 'react';
import type Lalalal from 'lalala/lalalal';
import { type Kokos } from 'kokos/kokos';
import { type Kokos } from 'kokos/trokos';

import ConfirmDialog from './ConfirmDialog';
import NicknameInput from './NicknameInput';
import SuccessDialog from './SuccessDialog';
import { something } from './something';

import type NekiNoviType from './precooltyNekiNoviTypepe';
import type PreCoolType from './precooltype';
import { type barakokula, someValue } from './barakokula';
5 changes: 5 additions & 0 deletions src/tests/type-imports/valid-cases/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from "path";

import { readAllTxtFilesInDir } from "../../../utils/tests";

export default readAllTxtFilesInDir(path.join(__dirname, "."), ["index.ts"]);
Loading
Loading