Skip to content

Commit

Permalink
feat: add and replace stylistic rules around imports
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonC committed Jul 30, 2024
1 parent 21780ee commit 31025c7
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 32 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/audit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as path from 'path';

import * as prompts from '@clack/prompts';
import CVSS, {
DetailedVectorObject,
VectorMetric,
type DetailedVectorObject,
type VectorMetric,
} from '@turingpointde/cvss.js';
import definitions from '@turingpointde/cvss.js/lib/cvss_3_0.json' assert { type: 'json' };
import chalk from 'chalk';
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"eslint-plugin-simple-import-sort": "^12.1.1",
"fs-extra": "^11.1.1",
"queue-microtask": "^1.2.3"
},
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'@tablecheck',
'@nx',
'react-refresh',
'simple-import-sort',
],

globals: {
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-config/src/overrides/buildBaseTypescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export const baseTypescriptRules: Linter.RulesRecord = {
},
},
],
'@typescript-eslint/consistent-type-imports': [
'error',
{
disallowTypeAnnotations: false,
fixStyle: 'inline-type-imports',
},
],
'@tablecheck/prefer-shortest-import': 'error',
};

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/src/overrides/cypress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Linter } from 'eslint';
import { type Linter } from 'eslint';

import { namingRules } from '../rules/namingConvention';
import { mergeDeep as merge } from '../utils/merge';
Expand Down
9 changes: 8 additions & 1 deletion packages/eslint-config/src/presets/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ if (!process.env.NODE_ENV) {
module.exports = {
extends: ['airbnb', 'plugin:eslint-comments/recommended', 'prettier'],

plugins: ['eslint-comments', 'promise', '@tablecheck', '@nx', '@emotion'],
plugins: [
'eslint-comments',
'promise',
'@tablecheck',
'@nx',
'@emotion',
'simple-import-sort',
],

parserOptions: {
ecmaVersion: 'latest',
Expand Down
27 changes: 23 additions & 4 deletions packages/eslint-config/src/rules/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,33 @@ export const generalRules: Linter.RulesRecord = {
},
],
'import/newline-after-import': 'error',
'import/order': [
'import/order': 'off',
'simple-import-sort/imports': [
'error',
{
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
'newlines-between': 'always',
alphabetize: { order: 'asc', caseInsensitive: false },
groups: [
// Node.js builtins.
[
'^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)',
],
// Packages. `react` related packages come first.
['^react', '^@?\\w'],
// Internal packages.
['^(@|@local|~)(/.*|$)'],
// Side effect imports.
['^\\u0000'],
// Parent imports. Put `..` last.
['^\\.\\.(?!/?$)', '^\\.\\./?$'],
// Other relative imports. Put same-folder imports and `.` last.
['^\\./(?=.*/)(?!/?$)', '^\\.(?!/?$)', '^\\./?$'],
// Style imports.
['^.+\\.s?css$'],
],
},
],
'simple-import-sort/exports': 'error',
'import/first': 'error',
'import/no-duplicates': 'error',
// https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
'import/prefer-default-export': 'off',
'import/no-default-export': 'error',
Expand Down
7 changes: 5 additions & 2 deletions packages/eslint-plugin/__tests__/forbiddenImports.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import { TSESLint } from '@typescript-eslint/utils';
import { type TSESLint } from '@typescript-eslint/utils';

import { forbiddenImports as rule, messageId } from '../src/forbiddenImports';
import {
forbiddenImports as rule,
type messageId,
} from '../src/forbiddenImports';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/__tests__/shortestImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join as pathJoin } from 'path';

import { RuleTester } from '@typescript-eslint/rule-tester';

import { shortestImport as rule, messageId } from '../src/shortestImport';
import { messageId, shortestImport as rule } from '../src/shortestImport';

const typescriptSetups = [
{
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/consistentReactImport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ImportSpecifier } from '@typescript-eslint/types/dist/generated/ast-spec';
import { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { RuleFix } from '@typescript-eslint/utils/dist/ts-eslint';
import { type RuleFix } from '@typescript-eslint/utils/dist/ts-eslint';

export const messageId = 'consistentReactImport';

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/forbiddenImports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TSESTree } from '@typescript-eslint/types';
import { TSESLint } from '@typescript-eslint/utils';
import { RuleFix } from '@typescript-eslint/utils/ts-eslint';
import { type TSESLint } from '@typescript-eslint/utils';
import { type RuleFix } from '@typescript-eslint/utils/ts-eslint';

type ImportDeclaration = TSESTree.ImportDeclaration;
type ImportSpecifier = TSESTree.ImportSpecifier;
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/shortestImport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as path from 'path';

import type { TSESTree } from '@typescript-eslint/types';
import { AST_NODE_TYPES, TSESLint } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, type TSESLint } from '@typescript-eslint/utils';
import fs from 'fs-extra';
import { CompilerOptions } from 'typescript';
import { type CompilerOptions } from 'typescript';

type ImportExpression = TSESTree.ImportDeclaration;
type ImportDeclaration = TSESTree.ImportExpression;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/executors/publint/executor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExecutorContext } from '@nx/devkit';
import { type ExecutorContext } from '@nx/devkit';
import { dynamicImport } from 'tsimportlib';

export default async function runExecutor(
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/executors/quality/executor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execSync } from 'child_process';

import { ExecutorContext } from '@nx/devkit';
import { type ExecutorContext } from '@nx/devkit';
import lintRun from '@nx/eslint/src/executors/lint/lint.impl.js';

import { configCheck } from './configs.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/quality/eslintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';

import { Tree } from '@nx/devkit';
import { type Tree } from '@nx/devkit';

import { getNxProjectRoot } from '../../utils/nx';
import { outputPrettyFile } from '../../utils/prettier';
Expand Down
8 changes: 4 additions & 4 deletions packages/nx/src/generators/quality/generator.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as path from 'path';

import {
addDependenciesToPackageJson,
formatFiles,
generateFiles,
addDependenciesToPackageJson,
Tree,
type Tree,
updateJson,
} from '@nx/devkit';
import { PackageJson } from 'type-fest';
import { type PackageJson } from 'type-fest';

import { getLatestVersions } from '../../utils/dependencies';
import generateIcons from '../ts-carbon-icons/generator';
import generateFileTypes from '../ts-file-types/generator';
import { FileTypesGeneratorSchema } from '../ts-file-types/schema';
import { type FileTypesGeneratorSchema } from '../ts-file-types/schema';
import generateConfig from '../ts-node-config/generator';

import { generateEslintConfig } from './eslintConfig';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/quality/projectConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as path from 'path';

import {
Tree,
addProjectConfiguration,
readProjectConfiguration,
type Tree,
updateProjectConfiguration,
} from '@nx/devkit';
import merge from 'lodash/merge';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/ts-carbon-icons/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';

import { Tree } from '@nx/devkit';
import { type Tree } from '@nx/devkit';

import { getNxProjectRoot } from '../../utils/nx';
import { detectInstalledVersion } from '../../utils/packageJson';
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/generators/ts-file-types/generator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from 'path';

import { formatFiles, generateFiles, Tree } from '@nx/devkit';
import { formatFiles, generateFiles, type Tree } from '@nx/devkit';

import { getNxProjectRoot } from '../../utils/nx';

import { FileTypesGeneratorSchema } from './schema';
import { type FileTypesGeneratorSchema } from './schema';

export async function tsFileTypesGenerator(
tree: Tree,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/ts-node-config/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';

import { Tree } from '@nx/devkit';
import { type Tree } from '@nx/devkit';
import * as fs from 'fs-extra';
import uniq from 'lodash/uniq';

Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/generators/vite-lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree } from '@nx/devkit';
import { type Tree } from '@nx/devkit';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { libraryGeneratorInternal } from '@nx/js/src/generators/library/library';

Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/nx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import { Tree, getProjects } from '@nx/devkit';
import { getProjects, type Tree } from '@nx/devkit';

export function getNxProjectRoot(tree: Tree, projectName: string) {
const project = getProjects(tree).get(projectName);
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/packageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as path from 'path';

import fs from 'fs-extra';
import {
format as prettyFormatPackage,
check as checkPackageFormat,
format as prettyFormatPackage,
} from 'prettier-package-json';
import * as semver from 'semver';
import type { PackageJson } from 'type-fest';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/tempFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';

import { Tree, generateFiles } from '@nx/devkit';
import { generateFiles, type Tree } from '@nx/devkit';
import fs from 'fs-extra';

type GenerateFilesFunction = (
Expand Down

0 comments on commit 31025c7

Please sign in to comment.