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(typescript-estree): add parserOption to turn on debug logs #1413

Merged
merged 6 commits into from Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/experimental-utils/src/ts-eslint/ParserOptions.ts
@@ -1,3 +1,5 @@
import { DebugLevel } from '@typescript-eslint/typescript-estree';

export interface ParserOptions {
comment?: boolean;
ecmaFeatures?: {
Expand All @@ -9,6 +11,7 @@ export interface ParserOptions {
errorOnUnknownASTType?: boolean;
extraFileExtensions?: string[];
// ts-estree specific
debugLevel?: DebugLevel;
filePath?: string;
loc?: boolean;
noWatch?: boolean;
Expand Down
Expand Up @@ -3,9 +3,9 @@ import path from 'path';
import * as ts from 'typescript';
import { Extra } from '../parser-options';
import {
getTsconfigPath,
DEFAULT_COMPILER_OPTIONS,
ASTAndProgram,
getTsconfigPath,
createDefaultCompilerOptionsFromExtra,
} from './shared';

const log = debug('typescript-eslint:typescript-estree:createDefaultProgram');
Expand All @@ -31,7 +31,7 @@ function createDefaultProgram(

const commandLine = ts.getParsedCommandLineOfConfigFile(
tsconfigPath,
DEFAULT_COMPILER_OPTIONS,
createDefaultCompilerOptionsFromExtra(extra),
{ ...ts.sys, onUnRecoverableConfigFileDiagnostic: () => {} },
);

Expand Down
Expand Up @@ -3,7 +3,7 @@ import * as ts from 'typescript';
import { Extra } from '../parser-options';
import {
ASTAndProgram,
DEFAULT_COMPILER_OPTIONS,
createDefaultCompilerOptionsFromExtra,
getScriptKind,
} from './shared';

Expand Down Expand Up @@ -67,7 +67,7 @@ function createIsolatedProgram(code: string, extra: Extra): ASTAndProgram {
noResolve: true,
target: ts.ScriptTarget.Latest,
jsx: extra.jsx ? ts.JsxEmit.Preserve : undefined,
...DEFAULT_COMPILER_OPTIONS,
...createDefaultCompilerOptionsFromExtra(extra),
},
compilerHost,
);
Expand Down
Expand Up @@ -6,9 +6,9 @@ import { WatchCompilerHostOfConfigFile } from './WatchCompilerHostOfConfigFile';
import {
canonicalDirname,
CanonicalPath,
getTsconfigPath,
DEFAULT_COMPILER_OPTIONS,
createDefaultCompilerOptionsFromExtra,
getCanonicalFileName,
getTsconfigPath,
} from './shared';

const log = debug('typescript-eslint:typescript-estree:createWatchProgram');
Expand Down Expand Up @@ -233,7 +233,7 @@ function createWatchProgram(
// create compiler host
const watchCompilerHost = ts.createWatchCompilerHost(
tsconfigPath,
DEFAULT_COMPILER_OPTIONS,
createDefaultCompilerOptionsFromExtra(extra),
ts.sys,
ts.createSemanticDiagnosticsBuilderProgram,
diagnosticReporter,
Expand Down
15 changes: 14 additions & 1 deletion packages/typescript-estree/src/create-program/shared.ts
Expand Up @@ -20,6 +20,19 @@ const DEFAULT_COMPILER_OPTIONS: ts.CompilerOptions = {
noUnusedParameters: true,
};

function createDefaultCompilerOptionsFromExtra(
extra: Extra,
): ts.CompilerOptions {
if (extra.debugLevel.has('typescript')) {
return {
...DEFAULT_COMPILER_OPTIONS,
extendedDiagnostics: true,
};
}

return DEFAULT_COMPILER_OPTIONS;
}

// This narrows the type so we can be sure we're passing canonical names in the correct places
type CanonicalPath = string & { __brand: unknown };

Expand Down Expand Up @@ -83,7 +96,7 @@ export {
ASTAndProgram,
canonicalDirname,
CanonicalPath,
DEFAULT_COMPILER_OPTIONS,
createDefaultCompilerOptionsFromExtra,
getCanonicalFileName,
getScriptKind,
getTsconfigPath,
Expand Down
15 changes: 15 additions & 0 deletions packages/typescript-estree/src/parser-options.ts
@@ -1,11 +1,25 @@
import { Program } from 'typescript';
import { TSESTree, TSNode } from './ts-estree';

type DebugModule = 'typescript-eslint' | 'eslint' | 'typescript';
/**
* For convenience:
* - true === ['typescript-eslint']
* - false === []
*
* An array of modules to turn explicit debugging on for.
* - 'typescript-eslint' is the same as setting the env var `DEBUG=typescript-eslint:*`
* - 'eslint' is the same as setting the env var `DEBUG=eslint:*`
* - 'typescript' is the same as setting `extendedDiagnostics: true` in your tsconfig compilerOptions
armano2 marked this conversation as resolved.
Show resolved Hide resolved
*/
export type DebugLevel = boolean | DebugModule[];

export interface Extra {
code: string;
comment: boolean;
comments: TSESTree.Comment[];
createDefaultProgram: boolean;
debugLevel: Set<DebugModule>;
errorOnTypeScriptSyntacticAndSemanticIssues: boolean;
errorOnUnknownASTType: boolean;
extraFileExtensions: string[];
Expand All @@ -25,6 +39,7 @@ export interface Extra {
export interface TSESTreeOptions {
comment?: boolean;
createDefaultProgram?: boolean;
debugLevel?: DebugLevel;
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
errorOnUnknownASTType?: boolean;
extraFileExtensions?: string[];
Expand Down
34 changes: 30 additions & 4 deletions packages/typescript-estree/src/parser.ts
@@ -1,7 +1,8 @@
import semver from 'semver';
import * as ts from 'typescript';
import debug from 'debug';
import { sync as globSync } from 'glob';
import isGlob from 'is-glob';
import semver from 'semver';
import * as ts from 'typescript';
import { astConverter } from './ast-converter';
import { convertError } from './convert';
import { createDefaultProgram } from './create-program/createDefaultProgram';
Expand Down Expand Up @@ -91,6 +92,7 @@ function resetExtra(): void {
comment: false,
comments: [],
createDefaultProgram: false,
debugLevel: new Set(),
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: false,
extraFileExtensions: [],
Expand All @@ -109,6 +111,31 @@ function resetExtra(): void {
}

function applyParserOptionsToExtra(options: TSESTreeOptions): void {
/**
* Configure Debug logging
*/
if (options.debugLevel === true) {
extra.debugLevel = new Set(['typescript-eslint']);
} else if (Array.isArray(options.debugLevel)) {
extra.debugLevel = new Set(options.debugLevel);
}
if (extra.debugLevel.size > 0) {
// debug doesn't support multiple `enable` calls, so have to do it all at once
const namespaces = [];
if (extra.debugLevel.has('typescript-eslint')) {
namespaces.push('typescript-eslint:*');
}
if (
extra.debugLevel.has('eslint') ||
// make sure we don't turn off the eslint debug if it was enabled via --debug
debug.enabled('eslint:*')
) {
// https://github.com/eslint/eslint/blob/9dfc8501fb1956c90dc11e6377b4cb38a6bea65d/bin/eslint.js#L25
namespaces.push('eslint:*,-eslint:code-path');
}
debug.enable(namespaces.join(','));
}

/**
* Track range information in the AST
*/
Expand Down Expand Up @@ -415,10 +442,9 @@ export {
parse,
parseAndGenerateServices,
ParseAndGenerateServicesResult,
ParserServices,
TSESTreeOptions,
version,
};
export { DebugLevel, ParserServices, TSESTreeOptions } from './parser-options';
export { simpleTraverse } from './simple-traverse';
export { visitorKeys } from './visitor-keys';
export * from './ts-estree';
Expand Down