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

Re: Re: Move to ESM #106

Merged
merged 32 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1d0756d
build(BREAKING CHANGES): add `"type": "module"` in `package.json`
mizdra Dec 28, 2021
cae48aa
deps: yarn add -D typescript@next
mizdra Dec 28, 2021
44a1054
build: use nightly version tsserver
mizdra Dec 28, 2021
03e2117
build: Change `"module": "node12"` and `"module": "moduleResolution"`…
mizdra Dec 28, 2021
888b431
fix: add `.js` extension
mizdra Dec 28, 2021
bfd8558
fix: add `/index.js`
mizdra Dec 28, 2021
6fb215f
deps: update dependencies to supported versions of ESM
mizdra Dec 28, 2021
25a66f0
fix: fix breaking changes of chalk
mizdra Dec 28, 2021
ab398aa
fix: import non-ESM (CJS) packages with import/require syntax
mizdra Dec 28, 2021
be4d269
fix: fix es module interop
mizdra Dec 28, 2021
b8cfc2e
fix: rename `.eslintrc.js` to `.eslintrc.cjs`
mizdra Dec 28, 2021
9e91c7c
build: disable `import/no-unresolved` rule
mizdra Dec 28, 2021
36e4c54
build: include `.cjs`, `.mjs`, `.cts` and `.mts` as lint targets
mizdra Dec 28, 2021
d97a2b4
deps: update shared eslint config
mizdra Dec 28, 2021
a932cb1
fix: convert `bin/` to ESM
mizdra Dec 28, 2021
69595d7
fix: import the CJS package with ESM-interop
mizdra Dec 28, 2021
6db3b65
fix: convert `__dirname` to `dirname(fileURLToPath(import.meta.url))`
mizdra Dec 27, 2021
c5dc7b0
fix: fix mismatch between type definitions and runtime types
mizdra Dec 28, 2021
0a980ad
fix: remove `module.exports`
mizdra Dec 28, 2021
bca4371
fix: make the entire `fixtures/` CJS
mizdra Dec 28, 2021
cdc1672
fix: convert `jest.config.js` to `jest.config.mjs`
mizdra Dec 28, 2021
1afa2db
deps: update jest toolchain
mizdra Dec 28, 2021
c550aec
fix: make jest ESM-ready
mizdra Dec 28, 2021
f5f57f0
deps: yarn add -D enhanced-resolve
mizdra Dec 28, 2021
9a33888
build: use custom resolver
mizdra Dec 28, 2021
deea149
deps: yarn add -D @jest/globals
mizdra Dec 28, 2021
7683a9d
fix: fix `ReferenceError: jest is not defined`
mizdra Dec 28, 2021
009081e
fix: don't use `jest.requireActual`
mizdra Dec 28, 2021
45b5015
deps: yarn remove @jest/globalsa
mizdra Dec 28, 2021
2b4bd5e
fix: include `.cjs`, `.mjs`, `.cts` and `.mts` as coverage targets
mizdra Dec 28, 2021
3b79917
build: ignore lint error of `bin/`
mizdra Dec 28, 2021
19016e1
refactor: use ESM version of comlink
mizdra Dec 28, 2021
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/dist
/tsconfig.src.tsbuildinfo
/tsconfig.test.tsbuildinfo
/bin
7 changes: 5 additions & 2 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
overrides: [
// for typescript
{
files: ['*.ts', '*.tsx'],
files: ['*.ts', '*.tsx', '*.cts', '*.mts'],
parserOptions: {
project: ['./tsconfig.src.json', './tsconfig.test.json'],
},
Expand All @@ -32,11 +32,14 @@ module.exports = {
'@typescript-eslint/no-unsafe-return': 'off',
// 煩すぎるので off
'@typescript-eslint/no-unsafe-argument': 'off',
// disable because this rule do not support ESM in TypeScript.
// ref: https://github.com/import-js/eslint-plugin-import/issues/2170
'import/no-unresolved': 'off',
},
},
// for test
{
files: ['test/**/*.{ts,tsx}'],
files: ['test/**/*.{ts,tsx,cts,mts}'],
env: {
jest: true,
},
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
// あまりカジュアルに起動時に処理を実行したくない、という狙いがある。
// *.ts ファイルが変更されたら jest のテストが自動実行がされる点は変わらないので、
// これで開発体験が損なわれることはないはず。
"jest.autoRun": { "watch": true }
"jest.autoRun": {
"watch": true
},
// Use nightly version tsserver
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 1 addition & 1 deletion bin/eslint-interactive → bin/eslint-interactive.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env -S node -r source-map-support/register

const { run } = require('../dist');
import { run } from '../dist/index.js';

run({
argv: process.argv,
Expand Down
3 changes: 3 additions & 0 deletions fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
15 changes: 11 additions & 4 deletions jest.config.js → jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

/** @typedef {import('ts-jest/dist/types')} */
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
// eslint-disable-next-line import/no-default-export
export default {
displayName: 'test',
preset: 'ts-jest',
preset: 'ts-jest/presets/default-esm',
testMatch: ['<rootDir>/test/**/*.test.ts?(x)'],
// Since fixtures-tmp are rewritten during testing, if they are in the watch target, the test will be retrying infinitely.
watchPathIgnorePatterns: ['<rootDir>/fixtures-tmp/'],
// ESLint v8 では `pkg.exports` を利用したモジュールに依存しているが、jest は `pkg.exports` を解釈できないため、
// そのままだと jest がコケてしまう。そこで `moduleNameMapper` を使って無理やりモジュール名を解決している。
moduleNameMapper: {
'@eslint/eslintrc/universal': '@eslint/eslintrc/dist/eslintrc-universal.cjs',
// Map `./**/xxx.js` to `./**/xxx` (for ESM)
'^(\\.{1,2}/.*)\\.js$': '$1',
},
collectCoverageFrom: ['<rootDir>/src/**/*.{ts,tsx}', '!**/*.d.ts'],
// for ESM
resolver: '<rootDir>/test/test-util/jest/resolver.cjs',
// do not transform `import` statements (for ESM)
transform: {},
collectCoverageFrom: ['<rootDir>/src/**/*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}', '!**/*.d.ts'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
useESM: true,
},
},
};
30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
"author": "mizdra <[email protected]>",
"license": "MIT",
"private": false,
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.src.json",
"dev": "tsc-watch -p tsconfig.src.json --onSuccess 'bin/eslint-interactive fixtures --rulesdir fixtures/rules --ext .js,.jsx,.mjs'",
"dev": "tsc-watch -p tsconfig.src.json --onSuccess 'bin/eslint-interactive.js fixtures --rulesdir fixtures/rules --ext .js,.jsx,.mjs'",
"lint": "run-s -c lint:*",
"lint:tsc": "run-s -c lint:tsc:*",
"lint:tsc:src": "tsc -p tsconfig.src.json --noEmit",
"lint:tsc:test": "tsc -p tsconfig.test.json --noEmit",
"lint:eslint": "eslint --ignore-pattern '/fixtures' .",
"lint:prettier": "prettier --check .",
"test": "jest --colors"
"test": "NODE_OPTIONS=--experimental-vm-modules jest --colors"
},
"prettier": "@mizdra/prettier-config-mizdra",
"renovate": "github>mizdra/renovate-config-mizdra",
"packageManager": "[email protected]",
"devDependencies": {
"@jest/types": "^27.1.0",
"@mizdra/eslint-config-mizdra": "^1.0.0",
"@jest/types": "^27.4.2",
"@mizdra/eslint-config-mizdra": "^1.1.0",
"@mizdra/prettier-config-mizdra": "^0.4.0",
"@types/cli-table": "^0.3.0",
"@types/eslint": "^8.2.0",
Expand All @@ -34,45 +35,46 @@
"@types/yargs": "^16.0.4",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"enhanced-resolve": "^5.8.3",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.2",
"jest": "^27.1.0",
"jest": "^27.4.5",
"jest-mock-process": "^1.4.1",
"npm-run-all": "^4.1.5",
"prettier": "2.4.1",
"strip-ansi": "^6.0.0",
"ts-jest": "^27.0.5",
"strip-ansi": "^7.0.1",
"ts-jest": "^27.1.2",
"tsc-watch": "^4.2.9",
"typescript": "^4.1.2"
"typescript": "^4.6.0-dev.20211228"
},
"dependencies": {
"boxen": "^5.1.2",
"chalk": "^4.1.2",
"boxen": "^6.2.1",
"chalk": "^5.0.0",
"cli-table": "^0.3.11",
"comlink": "^4.3.1",
"enquirer": "^2.3.6",
"eslint-formatter-codeframe": "^7.32.1",
"estraverse": "^5.3.0",
"is-installed-globally": "^0.4.0",
"node-pager": "^0.3.6",
"ora": "^5.1.0",
"ora": "^6.0.1",
"source-map-support": "^0.5.20",
"terminal-link": "^2.1.1",
"terminal-link": "^3.0.0",
"yargs": "^16.2.0"
},
"peerDependencies": {
"eslint": "^7.0.0 || ^8.0.0"
},
"engines": {
"node": ">=14.0"
"node": "^14.13.1 || >=16.0.0"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"bin": {
"eslint-interactive": "bin/eslint-interactive"
"eslint-interactive": "bin/eslint-interactive.js"
},
"keywords": [
"eslint"
Expand Down
6 changes: 3 additions & 3 deletions src/actions/apply-suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import chalk from 'chalk';
import { Remote } from 'comlink';
import { ESLint } from 'eslint';
import ora from 'ora';
import { promptToInputReuseFilterScript } from '../cli/prompt';
import { SerializableCore } from '../core-worker';
import { promptToInputReuseFilterScript } from '../cli/prompt.js';
import { SerializableCore } from '../core-worker.js';
import {
editFileWithEditor,
generateExampleFilterScriptFilePath,
generateFilterScriptFilePath,
} from '../util/filter-script';
} from '../util/filter-script.js';

export async function doApplySuggestionsAction(
core: Remote<SerializableCore>,
Expand Down
4 changes: 2 additions & 2 deletions src/actions/disable-per-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import chalk from 'chalk';
import { Remote } from 'comlink';
import { ESLint } from 'eslint';
import ora from 'ora';
import { promptToInputDescription } from '../cli/prompt';
import { SerializableCore } from '../core-worker';
import { promptToInputDescription } from '../cli/prompt.js';
import { SerializableCore } from '../core-worker.js';

export async function doDisablePerFileAction(
core: Remote<SerializableCore>,
Expand Down
4 changes: 2 additions & 2 deletions src/actions/disable-per-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import chalk from 'chalk';
import { Remote } from 'comlink';
import { ESLint } from 'eslint';
import ora from 'ora';
import { promptToInputDescription } from '../cli/prompt';
import { SerializableCore } from '../core-worker';
import { promptToInputDescription } from '../cli/prompt.js';
import { SerializableCore } from '../core-worker.js';

export async function doDisablePerLineAction(
core: Remote<SerializableCore>,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/fix.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import { Remote } from 'comlink';
import ora from 'ora';
import { SerializableCore } from '../core-worker';
import { SerializableCore } from '../core-worker.js';

export async function doFixAction(core: Remote<SerializableCore>, selectedRuleIds: string[]) {
const fixingSpinner = ora('Fixing...').start();
Expand Down
6 changes: 3 additions & 3 deletions src/actions/make-fixable-and-fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import chalk from 'chalk';
import { Remote } from 'comlink';
import { ESLint } from 'eslint';
import ora from 'ora';
import { promptToInputReuseScript } from '../cli/prompt';
import { SerializableCore } from '../core-worker';
import { promptToInputReuseScript } from '../cli/prompt.js';
import { SerializableCore } from '../core-worker.js';
import {
editFileWithEditor,
generateExampleFixableMakerScriptFilePath,
generateFixableMakerScriptFilePath,
} from '../util/filter-script';
} from '../util/filter-script.js';

export async function doMakeFixableAndFixAction(
core: Remote<SerializableCore>,
Expand Down
4 changes: 2 additions & 2 deletions src/actions/print-details-of-results.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Remote } from 'comlink';
import { ESLint } from 'eslint';
import { promptToInputDisplayMode } from '../cli/prompt';
import { SerializableCore } from '../core-worker';
import { promptToInputDisplayMode } from '../cli/prompt.js';
import { SerializableCore } from '../core-worker.js';

export async function doPrintDetailsOfResultsAction(
core: Remote<SerializableCore>,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/parse-argv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yargs from 'yargs';
import { Config } from '../types';
import { Config } from '../types.js';

/** Parse argv into the config object of eslint-interactive */
export function parseArgv(argv: string[]): Config {
Expand Down
6 changes: 4 additions & 2 deletions src/cli/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* istanbul ignore file */

import { prompt } from 'enquirer';
import { Action, DisplayMode } from '../types';
import enquirer from 'enquirer';
import { Action, DisplayMode } from '../types.js';

const { prompt } = enquirer;

// When combined with worker, for some reason the enquirer grabs the SIGINT and the process continues to survive.
// Therefore, the process is explicitly terminated.
Expand Down
14 changes: 7 additions & 7 deletions src/core-worker.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { parentPort } from 'worker_threads';
import { expose } from 'comlink';
import nodeEndpoint from 'comlink/dist/umd/node-adapter';
import nodeEndpoint from 'comlink/dist/esm/node-adapter.mjs';
import { ESLint } from 'eslint';
import { Core } from './core';
import { SuggestionFilter } from './transforms/apply-suggestions';
import { FixableMaker } from './transforms/make-fixable-and-fix';
import { Config } from './types';
import { Core } from './core.js';
import { SuggestionFilter } from './transforms/apply-suggestions.js';
import { FixableMaker } from './transforms/make-fixable-and-fix.js';
import { Config } from './types.js';

/**
* @file This is a wrapper module for using the Core API with comlink.
Expand Down Expand Up @@ -61,5 +61,5 @@ export class SerializableCore {
return this.core.makeFixableAndFix(results, ruleIds, fixableMaker);
}
}

expose(SerializableCore, nodeEndpoint(parentPort));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expose(SerializableCore, (nodeEndpoint as any)(parentPort));
12 changes: 6 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { tmpdir } from 'os';
import { join } from 'path';
import { ESLint } from 'eslint';
import pager from 'node-pager';
import { format } from './formatter';
import transformRule, { TransformRuleOption } from './rules/transform';
import { SuggestionFilter } from './transforms/apply-suggestions';
import { FixableMaker } from './transforms/make-fixable-and-fix';
import { Config, DisplayMode, Transform } from './types';
import { filterResultsByRuleId, scanUsedPluginsFromResults } from './util/eslint';
import { format } from './formatter/index.js';
import transformRule, { TransformRuleOption } from './rules/transform.js';
import { SuggestionFilter } from './transforms/apply-suggestions.js';
import { FixableMaker } from './transforms/make-fixable-and-fix.js';
import { Config, DisplayMode, Transform } from './types.js';
import { filterResultsByRuleId, scanUsedPluginsFromResults } from './util/eslint.js';

/**
* The core of eslint-interactive.
Expand Down
8 changes: 3 additions & 5 deletions src/formatter/colors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* istanbul ignore file */

import { Color } from 'chalk';

export const FAILED_COLOR: typeof Color = 'redBright';
export const ERROR_COLOR: typeof Color = 'red';
export const WARNING_COLOR: typeof Color = 'yellow';
export const FAILED_COLOR = 'redBright' as const;
export const ERROR_COLOR = 'red' as const;
export const WARNING_COLOR = 'yellow' as const;
2 changes: 1 addition & 1 deletion src/formatter/format-by-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import { ESLint } from 'eslint';
import { ERROR_COLOR, FAILED_COLOR, WARNING_COLOR } from './colors';
import { ERROR_COLOR, FAILED_COLOR, WARNING_COLOR } from './colors.js';

function pluralize(word: string, count: number): string {
return count > 1 ? `${word}s` : word;
Expand Down
7 changes: 4 additions & 3 deletions src/formatter/format-by-rules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import chalk from 'chalk';
import Table from 'cli-table';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import Table = require('cli-table');
import { ESLint } from 'eslint';
// import terminalLink from 'terminal-link';
import { ERROR_COLOR } from './colors';
import { takeRuleStatistics } from './take-rule-statistics';
import { ERROR_COLOR } from './colors.js';
import { takeRuleStatistics } from './take-rule-statistics.js';

function numCell(num: number): string {
return num > 0 ? chalk[ERROR_COLOR].bold(num) : num.toString();
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ESLint } from 'eslint';
import { formatByFiles } from './format-by-files';
import { formatByRules } from './format-by-rules';
import { formatByFiles } from './format-by-files.js';
import { formatByRules } from './format-by-rules.js';

export const format: ESLint.Formatter['format'] = (results, data) => {
return formatByFiles(results) + '\n' + formatByRules(results, data);
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/take-rule-statistics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ESLint, Linter } from 'eslint';
import { RuleStatistic } from '../types';
import { groupBy } from '../util/array';
import { RuleStatistic } from '../types.js';
import { groupBy } from '../util/array.js';

/** 指定されたルールのエラー/警告の件数などの統計を取る */
function takeRuleStatistic(ruleId: string, messages: Linter.LintMessage[]): RuleStatistic {
Expand Down
Loading