Skip to content
Draft
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
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

62 changes: 0 additions & 62 deletions .eslintrc.cjs

This file was deleted.

2 changes: 0 additions & 2 deletions bin/diff2html.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env node

// The build creates lib/main.js after lint runs.
// eslint-disable-next-line @typescript-eslint/no-var-requires, node/no-missing-require
const { main } = require('../lib/main.js');
main();
93 changes: 93 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { fileURLToPath } from 'node:url';

import js from '@eslint/js';
import json from '@eslint/json';
import tsPlugin from '@typescript-eslint/eslint-plugin';
import { defineConfig, globalIgnores, includeIgnoreFile } from 'eslint/config';
import prettier from 'eslint-config-prettier/flat';
import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript';
import importX from 'eslint-plugin-import-x';
import jest from 'eslint-plugin-jest';
import node from 'eslint-plugin-n';
import optimizeRegex from 'eslint-plugin-optimize-regex';
import promise from 'eslint-plugin-promise';
import sonarjs from 'eslint-plugin-sonarjs';
import globals from 'globals';

const projectFile = name => fileURLToPath(new URL(name, import.meta.url));

export default defineConfig([
includeIgnoreFile(projectFile('.gitignore'), { gitignoreResolution: true }),
globalIgnores(['build/']),
{
files: ['**/*.{js,cjs,mjs,jsx,ts,tsx}'],
extends: [
js.configs.recommended,
importX.flatConfigs.errors,
importX.flatConfigs.warnings,
node.configs['flat/recommended-module'],
promise.configs['flat/recommended'],
sonarjs.configs.recommended,
],
languageOptions: { globals: { ...globals.browser, ...globals.node } },
plugins: { 'optimize-regex': optimizeRegex },
rules: {
'import-x/no-unresolved': 'error',
'n/no-missing-import': 'off', // import-x resolves both TS source and JS output specifiers.
'n/no-unpublished-bin': 'error',
'n/no-unsupported-features/es-syntax': 'off', // TS targets the supported runtime syntax.
'optimize-regex/optimize-regex': 'error',
'sonarjs/no-duplicate-string': 'off',
'sonarjs/cognitive-complexity': 'off',
},
settings: {
'import-x/resolver-next': [
createTypeScriptImportResolver({ project: projectFile('tsconfig.json'), alwaysTryTypes: true }),
],
},
},
{
files: ['eslint.config.mjs'],
settings: { n: { version: '^20.19.0 || ^22.13.0 || >=24' } },
},
{
files: ['**/*.cjs', 'bin/**/*.js'],
languageOptions: { sourceType: 'commonjs' },
},
{
files: ['bin/diff2html.js'],
// Check the entry point source before the build emits lib/main.js.
rules: {
'n/no-missing-require': [
'error',
{ resolverConfig: { alias: { '../lib/main.js$': projectFile('src/main.ts') } } },
],
},
},
{
files: ['**/*.jsx'],
languageOptions: { parserOptions: { ecmaFeatures: { jsx: true } } },
},
{
files: ['**/*.{js,jsx}'],
settings: { 'import-x/parsers': { '@typescript-eslint/parser': ['.jsx', '.ts', '.tsx'] } },
},
{
files: ['**/*.{ts,tsx}'],
extends: [tsPlugin.configs['flat/recommended'], importX.flatConfigs.typescript],
},
{
// Match Jest's default test discovery.
files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
extends: [jest.configs['flat/recommended'], jest.configs['flat/style']],
},
{
files: ['**/*.{js,cjs,mjs,jsx,ts,tsx}'],
extends: [prettier],
},
{
files: ['**/*.json'],
language: 'json/json',
extends: [json.configs.recommended],
},
]);
33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
},
"preferGlobal": true,
"scripts": {
"eslint": "eslint --ignore-path .gitignore \"**/*.{js,jsx,ts,tsx,json}\"",
"eslint": "eslint .",
"lint:check": "yarn run eslint",
"lint:fix": "yarn run eslint --fix",
"prettier": "prettier --ignore-path .gitignore '**/*.+(js|jsx|ts|tsx|json|css|html|md|mdx)'",
"prettier": "prettier --ignore-path .gitignore '**/*.+(js|cjs|mjs|jsx|ts|tsx|json|css|html|md|mdx)'",
"format:check": "yarn run prettier --check",
"format:fix": "yarn run prettier --write",
"build": "rm -rf lib; tsc -p tsconfig.json --outDir lib",
Expand All @@ -65,7 +65,7 @@
"diff2html": "./bin/diff2html.js"
},
"lint-staged": {
"**/*.+(js|jsx|ts|tsx|json)": [
"**/*.+(js|cjs|mjs|jsx|ts|tsx|json)": [
"prettier --write",
"eslint --fix"
],
Expand All @@ -87,24 +87,26 @@
"@babel/core": "^7.23.7",
"@babel/preset-env": "^7.23.8",
"@babel/preset-typescript": "^7.23.3",
"@eslint/js": "10.0.1",
"@eslint/json": "2.1.1",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.11",
"@types/node": "12.20.55",
"@types/node-fetch": "2.6.13",
"@types/yargs": "17.0.35",
"@typescript-eslint/eslint-plugin": "6.18.1",
"@typescript-eslint/parser": "6.18.1",
"@typescript-eslint/eslint-plugin": "8.74.0",
"@typescript-eslint/parser": "8.74.0",
"babel-jest": "^29.7.0",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jest": "27.6.3",
"eslint-plugin-json": "3.1.0",
"eslint-plugin-node": "11.1.0",
"eslint": "10.16.0",
"eslint-config-prettier": "10.1.9",
"eslint-import-resolver-typescript": "4.4.5",
"eslint-plugin-import-x": "4.18.1",
"eslint-plugin-jest": "29.17.2",
"eslint-plugin-n": "18.6.0",
"eslint-plugin-optimize-regex": "1.2.1",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-sonarjs": "0.23.0",
"eslint-plugin-promise": "7.3.0",
"eslint-plugin-sonarjs": "4.2.3",
"globals": "17.14.0",
"husky": "8.0.3",
"is-ci-cli": "2.2.0",
"jest": "29.7.0",
Expand All @@ -115,6 +117,9 @@
"ts-node": "^10.9.2",
"typescript": "5.3.3"
},
"resolutions": {
"eslint-plugin-sonarjs/typescript": "5.3.3"
},
"license": "MIT",
"files": [
"bin",
Expand Down
14 changes: 10 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ function prepareHTML(diffHTMLContent: string, config: Configuration, colorScheme
const pageTitle = config.pageTitle;
const pageHeader = config.pageHeader;

const gitHubTheme =
colorScheme === 'light' ? lightGitHubTheme : colorScheme === 'dark' ? darkGitHubTheme : autoGitHubTheme;

const baseStyle = colorScheme === 'light' ? lightBaseStyle : colorScheme === 'dark' ? darkBaseStyle : autoBaseStyle;
let gitHubTheme = autoGitHubTheme;
let baseStyle = autoBaseStyle;
if (colorScheme === 'light') {
gitHubTheme = lightGitHubTheme;
baseStyle = lightBaseStyle;
} else if (colorScheme === 'dark') {
gitHubTheme = darkGitHubTheme;
baseStyle = darkBaseStyle;
}

/* HACK:
* Replace needs to receive a function as the second argument to perform an exact replacement.
Expand Down Expand Up @@ -204,6 +209,7 @@ async function copyToClipboard(text: string): Promise<void> {
// clipboardy@2 also mistakes Podman on WSL2 for the Windows host. Keep its Linux helper order.
const args = ['--clipboard', '--input'];
const options = { input: text };
// eslint-disable-next-line sonarjs/no-os-command-from-path -- Prefer the system xsel before the bundled fallback.
if (childProcess.spawnSync('xsel', args, options).status === 0) return;

const bundledXsel = path.resolve(path.dirname(require.resolve('clipboardy')), 'fallbacks', 'linux', 'xsel');
Expand Down
33 changes: 17 additions & 16 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ import { Argv } from './yargs.js';
import { Diff2HtmlConfig } from 'diff2html';
import { OutputFormatType, DiffStyleType, LineMatchingType } from 'diff2html/lib/types.js';

const outputFormats = new Map<Argv['style'], OutputFormatType>([
['line', OutputFormatType.LINE_BY_LINE],
['side', OutputFormatType.SIDE_BY_SIDE],
]);
const diffStyles = new Map<Argv['diffStyle'], DiffStyleType>([
['char', DiffStyleType.CHAR],
['word', DiffStyleType.WORD],
]);
const lineMatching = new Map<Argv['matching'], LineMatchingType>([
['lines', LineMatchingType.LINES],
['words', LineMatchingType.WORDS],
['none', LineMatchingType.NONE],
]);

export function parseArgv(argv: Argv): [Diff2HtmlConfig, Configuration] {
const diff2htmlOptions: Diff2HtmlConfig = {
outputFormat:
argv.style === 'side'
? OutputFormatType.SIDE_BY_SIDE
: argv.style === 'line'
? OutputFormatType.LINE_BY_LINE
: undefined,
diffStyle:
argv.diffStyle === 'char' ? DiffStyleType.CHAR : argv.diffStyle === 'word' ? DiffStyleType.WORD : undefined,
matching:
argv.matching === 'lines'
? LineMatchingType.LINES
: argv.matching === 'words'
? LineMatchingType.WORDS
: argv.matching === 'none'
? LineMatchingType.NONE
: undefined,
outputFormat: outputFormats.get(argv.style),
diffStyle: diffStyles.get(argv.diffStyle),
matching: lineMatching.get(argv.matching),
drawFileList: argv.summary !== 'hidden',
matchWordsThreshold: argv.matchWordsThreshold,
matchingMaxComparisons: argv.matchingMaxComparisons,
Expand Down
1 change: 1 addition & 0 deletions src/http-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function put(url: string, payload: object): Promise<unknown> {
return JSON.parse(bodyText);
} catch (error) {
if (error instanceof SyntaxError) {
// eslint-disable-next-line preserve-caught-error -- Node 12 does not support Error cause options.
throw new Error(`Failed to parse response json.\nBody:\n\n${bodyText}`);
} else {
throw error;
Expand Down
2 changes: 0 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// eslint-disable-next-line @typescript-eslint/ban-types
export function print(message: string, ...params: (string | number | object)[]): void {
console.log(message, ...params);
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function error(message: string, ...params: (string | number | object)[]): void {
console.error(message, params);
}
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
export function exists(filePath: string): boolean {
try {
return fs.existsSync(filePath);
} catch (ignore) {
} catch {
return false;
}
}
Expand Down
Loading