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

feat: start TypeScript migration #42

Merged
merged 5 commits into from
Mar 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/wicked-schools-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": minor
---

feat!: start TypeScript migration
13 changes: 13 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('eslint').Linter.Config}
*/
module.exports = {
root: true,
plugins: ['eslint-plugin', 'import-x'],
Expand All @@ -18,6 +21,7 @@ module.exports = {
ecmaVersion: 2020,
},
rules: {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off',

'eslint-plugin/consistent-output': ['error', 'always'],
Expand Down Expand Up @@ -55,6 +59,15 @@ module.exports = {
},

overrides: [
{
files: ['*.js', '*.ts'],
excludedFiles: ['test/fixtures'],
settings: {
'import-x/resolver': {
typescript: true,
},
},
},
{
files: 'test/**',
env: {
Expand Down
32 changes: 0 additions & 32 deletions .github/workflows/pkg-size.yml

This file was deleted.

9 changes: 9 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('@babel/core').TransformOptions}
*/
module.exports = {
presets: [
[
Expand All @@ -22,4 +25,10 @@ module.exports = {
],
sourceMaps: 'inline',
retainLines: true,
overrides: [
{
include: '**/*.ts',
presets: ['@babel/typescript'],
},
],
}
16 changes: 10 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const testCompiled = process.env.TEST_COMPILED === '1'

const srcDir = testCompiled ? 'lib' : 'src'

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
collectCoverage: true,
collectCoverage: !testCompiled,
modulePathIgnorePatterns: ['<rootDir>/test/fixtures/with-syntax-error'],
moduleNameMapper: {
'^core/(.+)$': '<rootDir>/src/core/$1',
'^eslint-plugin-import-x$': '<rootDir>/src/index.js',
'^eslint-plugin-import-x/config/(.+)$': '<rootDir>/config/$1',
'^core/(.+)$': `<rootDir>/${srcDir}/core/$1`,
'^eslint-plugin-import-x$': `<rootDir>/${srcDir}`,
'^eslint-plugin-import-x/package.json$': '<rootDir>/package.json',
'^eslint-plugin-import-x/(.+)$': '<rootDir>/src/$1',
'^rules/(.+)$': '<rootDir>/src/rules/$1',
'^eslint-plugin-import-x/(.+)$': `<rootDir>/${srcDir}/$1`,
'^rules/(.+)$': `<rootDir>/${srcDir}/rules/$1`,
},
testMatch: [
'<rootDir>/test/**/*.spec.js',
'<rootDir>/test/**/*.spec.ts',
'!<rootDir>/test/fixtures/**/*.js',
],
}
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
"main": "lib/index.js",
"exports": {
".": "./lib/index.js",
"./config/*": "./config/*",
"./package.json": "./package.json",
"./*": "./lib/*"
},
"files": [
"config",
"lib"
"lib",
"!lib/*.tsbuildinfo"
],
"keywords": [
"eslint",
Expand All @@ -31,19 +30,21 @@
"export"
],
"scripts": {
"build": "babel --quiet --out-dir lib src",
"build": "rimraf lib && tsc -p src",
"lint": "yarn lint:es && yarn update:eslint-docs --check",
"lint:es": "eslint . --cache",
"prebuild": "rimraf lib",
"prepare": "patch-package",
"release": "changeset publish",
"test": "jest",
"test-compiled": "yarn build && cross-env TEST_COMPILED=1 jest",
"update:eslint-docs": "yarn build && eslint-doc-generator --rule-doc-title-format prefix-name --rule-doc-section-options false --rule-list-split meta.docs.category --ignore-config stage-0 --config-emoji recommended,☑️",
"watch": "yarn test --watch"
},
"peerDependencies": {
"eslint": "^7.2.0 || ^8"
},
"dependencies": {
"@typescript-eslint/utils": "^5.62.0",
"debug": "^4.3.4",
"doctrine": "^3.0.0",
"eslint-import-resolver-node": "^0.3.9",
Expand All @@ -54,23 +55,30 @@
},
"devDependencies": {
"@1stg/prettier-config": "^4.0.1",
"@1stg/tsconfig": "^2.3.3",
"@angular-eslint/template-parser": "^17.2.1",
"@babel/cli": "^7.23.9",
"@babel/core": "^7.24.0",
"@babel/eslint-parser": "^7.23.10",
"@babel/plugin-proposal-decorators": "^7.24.0",
"@babel/plugin-proposal-export-default-from": "^7.23.3",
"@babel/preset-env": "^7.24.0",
"@babel/preset-flow": "^7.24.0",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@babel/register": "^7.23.7",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@eslint/import-test-order-redirect-scoped": "link:./test/fixtures/order-redirect-scoped",
"@test-scope/some-module": "link:./test/fixtures/symlinked-module",
"@types/eslint": "^8.56.5",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@types/json-schema": "^7.0.15",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/typescript-estree": "^5.62.0",
"@unts/patch-package": "^8.0.0",
"cross-env": "^7.0.3",
"enhanced-resolve": "^5.16.0",
"escope": "^4.0.0",
"eslint": "^7.2.0 || ^8",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -88,6 +96,7 @@
"redux": "^5.0.1",
"rimraf": "^5.0.5",
"svelte": "^4.2.12",
"typescript": "^5.4.2"
"type-fest": "^4.12.0",
"typescript": "~5.1.0"
}
}
15 changes: 15 additions & 0 deletions patches/@typescript-eslint+utils+5.62.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
index 9a3a1fd..46b3961 100644
--- a/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
+++ b/node_modules/@typescript-eslint/utils/dist/ts-eslint/Rule.d.ts
@@ -6,6 +6,10 @@ import type { Scope } from './Scope';
import type { SourceCode } from './SourceCode';
export type RuleRecommendation = 'error' | 'strict' | 'warn' | false;
interface RuleMetaDataDocs {
+ /**
+ * The category the rule falls under
+ */
+ category?: string;
/**
* Concise description of the rule
*/
4 changes: 2 additions & 2 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SourceCode } from 'eslint'

import parse from './utils/parse'
import visit from './utils/visit'
import resolve from './utils/resolve'
import { relative, resolve } from './utils/resolve'
import isIgnored, { hasValidExtension } from './utils/ignore'

import { hashObject } from './utils/hash'
Expand Down Expand Up @@ -506,7 +506,7 @@ ExportMap.parse = function (path, content, context) {
const namespaces = new Map()

function remotePath(value) {
return resolve.relative(value, path, context.settings)
return relative(value, path, context.settings)
}

function resolveImport(value) {
Expand Down
6 changes: 4 additions & 2 deletions config/electron.js → src/config/electron.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { PluginConfig } from '../types'

/**
* Default settings for Electron applications.
*/
module.exports = {
export = {
settings: {
'import-x/core-modules': ['electron'],
},
}
} satisfies PluginConfig
7 changes: 4 additions & 3 deletions config/errors.js → src/config/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { PluginConfig } from '../types'

/**
* unopinionated config. just the things that are necessarily runtime errors
* waiting to happen.
* @type {Object}
*/
module.exports = {
export = {
plugins: ['import-x'],
rules: {
'import-x/no-unresolved': 2,
Expand All @@ -12,4 +13,4 @@ module.exports = {
'import-x/default': 2,
'import-x/export': 2,
},
}
} satisfies PluginConfig
8 changes: 5 additions & 3 deletions config/react-native.js → src/config/react-native.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { PluginConfig } from '../types'

/**
* - adds platform extensions to Node resolver
* adds platform extensions to Node resolver
*/
module.exports = {
export = {
settings: {
'import-x/resolver': {
node: {
Expand All @@ -10,4 +12,4 @@ module.exports = {
},
},
},
}
} satisfies PluginConfig
11 changes: 7 additions & 4 deletions config/react.js → src/config/react.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { PluginConfig } from '../types'

/**
* Adds `.jsx` as an extension, and enables JSX parsing.
*
* Even if _you_ aren't using JSX (or .jsx) directly, if your dependencies
* define jsnext:main and have JSX internally, you may run into problems
* if you don't enable these settings at the top level.
*/
module.exports = {
export = {
settings: {
'import-x/extensions': ['.js', '.jsx'],
},

parserOptions: {
ecmaFeatures: { jsx: true },
ecmaFeatures: {
jsx: true,
},
},
}
} satisfies PluginConfig
7 changes: 4 additions & 3 deletions config/recommended.js → src/config/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PluginConfig } from '../types'

/**
* The basics.
* @type {Object}
*/
module.exports = {
export = {
plugins: ['import-x'],

rules: {
Expand All @@ -25,4 +26,4 @@ module.exports = {
sourceType: 'module',
ecmaVersion: 2018,
},
}
} satisfies PluginConfig
7 changes: 4 additions & 3 deletions config/stage-0.js → src/config/stage-0.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { PluginConfig } from '../types'

/**
* Rules in progress.
*
* Do not expect these to adhere to semver across releases.
* @type {Object}
*/
module.exports = {
export = {
plugins: ['import-x'],
rules: {
'import-x/no-deprecated': 1,
},
}
} as PluginConfig
13 changes: 7 additions & 6 deletions config/typescript.js → src/config/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { PluginConfig } from '../types'

/**
* This config:
* 1) adds `.jsx`, `.ts`, `.cts`, `.mts`, and `.tsx` as an extension
Expand All @@ -7,28 +9,27 @@
// Omit `.d.ts` because 1) TypeScript compilation already confirms that
// types are resolved, and 2) it would mask an unresolved
// `.ts`/`.tsx`/`.js`/`.jsx` implementation.
const typeScriptExtensions = ['.ts', '.cts', '.mts', '.tsx']
const typeScriptExtensions = ['.ts', '.tsx'] as const

const allExtensions = [...typeScriptExtensions, '.js', '.jsx']
const allExtensions = [...typeScriptExtensions, '.js', '.jsx'] as const

module.exports = {
export = {
settings: {
'import-x/extensions': allExtensions,
'import-x/external-module-folders': ['node_modules', 'node_modules/@types'],
'import-x/parsers': {
'@typescript-eslint/parser': typeScriptExtensions,
'@typescript-eslint/parser': [...typeScriptExtensions, '.cts', '.mts'],
},
'import-x/resolver': {
node: {
extensions: allExtensions,
},
},
},

rules: {
// analysis/correctness

// TypeScript compilation already ensures that named imports exist in the referenced module
'import-x/named': 'off',
},
}
} satisfies PluginConfig
7 changes: 4 additions & 3 deletions config/warnings.js → src/config/warnings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { PluginConfig } from '../types'

/**
* more opinionated config.
* @type {Object}
*/
module.exports = {
export = {
plugins: ['import-x'],
rules: {
'import-x/no-named-as-default': 1,
'import-x/no-named-as-default-member': 1,
'import-x/no-duplicates': 1,
},
}
} satisfies PluginConfig
Loading
Loading