Skip to content

Commit

Permalink
Merge pull request #138 from mizdra/fix-codeframe-bug
Browse files Browse the repository at this point in the history
Fix `Display details of lint result` failing during global installation
  • Loading branch information
mizdra committed Feb 5, 2022
2 parents a5c17f7 + 1151733 commit 5775a18
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ Cargo.lock
/.cache
.netlify
*.tsbuildinfo
/fixtures-tmp
/fixtures-tmp
mizdra-eslint-interactive-*.tgz
2 changes: 1 addition & 1 deletion bin/eslint-interactive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --enable-source-maps --unhandled-rejections=strict
#!/usr/bin/env -S node --enable-source-maps --unhandled-rejections=strict --experimental-import-meta-resolve

import { run } from '../dist/index.js';

Expand Down
1 change: 0 additions & 1 deletion e2e-test/global-installation/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/mizdra-eslint-interactive-*.tgz
/package-lock.json
3 changes: 0 additions & 3 deletions e2e-test/global-installation/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ npm install -g $(ls mizdra-eslint-interactive-*.tgz)
## install peerDeps manually
npm install -g eslint@$(cat ../../node_modules/eslint/package.json | jq -r .version)

## workaround for https://github.com/mizdra/eslint-interactive/issues/133
npm install source-map-support


# Test cases

Expand Down
21 changes: 20 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { tmpdir } from 'os';
import { join } from 'path';
import { fileURLToPath } from 'url';
import { ESLint } from 'eslint';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import isInstalledGlobally = require('is-installed-globally');
import { format } from './formatter/index.js';
import {
eslintInteractivePlugin,
Expand Down Expand Up @@ -77,7 +80,23 @@ export class Core {
*/
async formatResultDetails(results: ESLint.LintResult[], ruleIds: (string | null)[]): Promise<string> {
const eslint = new ESLint(this.baseOptions);
const formatter = await eslint.loadFormatter(this.config.formatterName ?? 'codeframe');
const formatterName = this.config.formatterName ?? 'codeframe';

// When eslint-interactive is installed globally, eslint-formatter-codeframe will also be installed globally.
// On the other hand, `eslint.loadFormatter` cannot load the globally installed formatter by name. So here it loads them by path.
const resolvedFormatterNameOrPath =
isInstalledGlobally && formatterName === 'codeframe'
? fileURLToPath(
// @ts-expect-error
await import.meta.resolve(
'eslint-formatter-codeframe',
// @ts-expect-error
await import.meta.resolve('@mizdra/eslint-interactive'),
),
)
: formatterName;

const formatter = await eslint.loadFormatter(resolvedFormatterNameOrPath);
return formatter.format(filterResultsByRuleId(results, ruleIds));
}

Expand Down

0 comments on commit 5775a18

Please sign in to comment.