Skip to content

Commit

Permalink
feat(typescript-estree): skip isTTY version check if user passes `l…
Browse files Browse the repository at this point in the history
…oggerFn` on unsupported TypeScript version warning (#7739)
  • Loading branch information
hyperupcall committed Nov 11, 2023
1 parent be2777c commit 9656e13
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function createParseSettings(
typeof options.tsconfigRootDir === 'string'
? options.tsconfigRootDir
: process.cwd();
const passedLoggerFn = typeof options.loggerFn === 'function';
const parseSettings: MutableParseSettings = {
allowInvalidAST: options.allowInvalidAST === true,
code,
Expand Down Expand Up @@ -135,7 +136,7 @@ export function createParseSettings(
});
}

warnAboutTSVersion(parseSettings);
warnAboutTSVersion(parseSettings, passedLoggerFn);

return parseSettings;
}
Expand Down
44 changes: 26 additions & 18 deletions packages/typescript-estree/src/parseSettings/warnAboutTSVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,31 @@ const isRunningSupportedTypeScriptVersion = semver.satisfies(

let warnedAboutTSVersion = false;

export function warnAboutTSVersion(parseSettings: ParseSettings): void {
if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
const isTTY =
typeof process === 'undefined' ? false : process.stdout?.isTTY;
if (isTTY) {
const border = '=============';
const versionWarning = [
border,
'WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.',
'You may find that it works just fine, or you may not.',
`SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
`YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
'Please only submit bug reports when using the officially supported version.',
border,
];
parseSettings.log(versionWarning.join('\n\n'));
}
warnedAboutTSVersion = true;
export function warnAboutTSVersion(
parseSettings: ParseSettings,
passedLoggerFn: boolean,
): void {
if (isRunningSupportedTypeScriptVersion || warnedAboutTSVersion) {
return;
}

if (
passedLoggerFn ||
(typeof process === 'undefined' ? false : process.stdout?.isTTY)
) {
const border = '=============';
const versionWarning = [
border,
'WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.',
'You may find that it works just fine, or you may not.',
`SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
`YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
'Please only submit bug reports when using the officially supported version.',
border,
].join('\n\n');

parseSettings.log(versionWarning);
}

warnedAboutTSVersion = true;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import semver from 'semver';

import * as parser from '../../src/parser';
import type * as Parser from '../../src/parser';

jest.mock('semver');

const resetIsTTY = process.stdout.isTTY;

describe('Warn on unsupported TypeScript version', () => {
let parser: typeof Parser;

beforeEach(async () => {
// @ts-expect-error -- We don't support ESM imports of local code yet.
parser = await import('../../src/parser');
});
afterEach(() => {
jest.resetModules();
jest.resetAllMocks();
Expand All @@ -26,7 +32,18 @@ describe('Warn on unsupported TypeScript version', () => {
);
});

it('should not warn the user when the user is running on a non TTY process', () => {
it('should warn the user if they are running on a non TTY process and a custom loggerFn was passed', () => {
(semver.satisfies as jest.Mock).mockReturnValue(false);
const loggerFn = jest.fn();
process.stdout.isTTY = false;

parser.parse('', {
loggerFn,
});
expect(loggerFn).toHaveBeenCalled();
});

it('should not warn the user if they are running on a non TTY process and a custom loggerFn was not passed', () => {
(semver.satisfies as jest.Mock).mockReturnValue(false);
jest.spyOn(console, 'log').mockImplementation();
process.stdout.isTTY = false;
Expand Down

0 comments on commit 9656e13

Please sign in to comment.