From c79821c63093fbb8d37c1cdfbddfc854e77a4fde Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Sat, 4 Sep 2021 21:30:52 +0200 Subject: [PATCH] WIP: use eslint-tooling --- .eslintignore | 1 + .eslintrc.js | 70 +----------------- .vscode/settings.json | 7 ++ package.json | 1 + src/AstParser.ts | 16 +++- src/AstTraverser.ts | 4 +- src/errors/NoSourceAnnotations.ts | 2 +- src/errors/ParserError.ts | 6 +- src/errors/handler.ts | 4 +- src/extracts/extract_exports.ts | 8 +- src/extracts/extract_functions.ts | 3 +- src/extracts/extract_source.ts | 2 +- src/extracts/extract_tests.ts | 7 +- src/extracts/extract_variables.ts | 74 +++++++++++-------- src/guards/is_assignment_pattern.ts | 3 +- src/guards/is_binary_expression.ts | 3 +- src/guards/is_call_expression.ts | 13 ++-- src/guards/is_identifier.ts | 11 +-- src/guards/is_literal.ts | 3 +- src/guards/is_logical_expression.ts | 3 +- src/guards/is_member_expression.ts | 9 ++- src/guards/is_return_block_statement.ts | 3 +- src/guards/is_return_statement_with_value.ts | 3 +- src/guards/is_template_literal.ts | 3 +- src/guards/is_unary_expression.ts | 3 +- src/guards/is_variable_declaration_of_kind.ts | 3 +- src/input/DirectoryInput.ts | 3 +- src/input/DirectoryWithConfigInput.ts | 10 ++- src/input/FileInput.ts | 3 +- src/input/InlineInput.ts | 2 +- src/input/Input.ts | 4 +- src/queries/find_all.ts | 2 +- src/queries/find_first.ts | 5 +- src/queries/find_first_of_type.ts | 2 +- src/queries/find_literal.ts | 7 +- src/queries/find_member_call.ts | 7 +- src/queries/find_new_expression.ts | 11 +-- src/queries/find_raw_literal.ts | 8 +- src/queries/find_top_level_constants.ts | 19 +++-- src/tsconfig.json | 4 - src/utils/logger.ts | 10 ++- test/.eslintrc | 19 ----- test/tsconfig.eslint.json | 4 - test/tsconfig.json | 2 +- tsconfig.eslint.json | 16 ++++ yarn.lock | 5 ++ 46 files changed, 198 insertions(+), 210 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 src/tsconfig.json delete mode 100644 test/.eslintrc delete mode 100644 test/tsconfig.eslint.json create mode 100644 tsconfig.eslint.json diff --git a/.eslintignore b/.eslintignore index e6e1f45..149004a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,3 +6,4 @@ production_node_modules/* test/fixtures/* tmp/* jest.config.js +/.eslintrc.js \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js index fefdd2c..fd02fc4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,74 +2,8 @@ module.exports = { root: true, parser: '@typescript-eslint/parser', parserOptions: { - project: 'tsconfig.json', + project: ['./tsconfig.eslint.json'], tsconfigRootDir: __dirname, - ecmaVersion: 2018, - sourceType: 'module', - ecmaFeatures: { - modules: true, - }, - }, - env: { - es6: true, - node: true, - }, - plugins: ['import', '@typescript-eslint'], - extends: [ - 'eslint:recommended', - 'plugin:@typescript-eslint/eslint-recommended', - 'plugin:@typescript-eslint/recommended', - 'prettier', - 'plugin:import/errors', - 'plugin:import/warnings', - 'plugin:import/typescript', - ], - rules: { - '@typescript-eslint/explicit-function-return-type': [ - 'warn', - { - allowExpressions: false, - allowTypedFunctionExpressions: true, - allowHigherOrderFunctions: true, - }, - ], - '@typescript-eslint/explicit-member-accessibility': [ - 'warn', - { - accessibility: 'no-public', - overrides: { - accessors: 'explicit', - constructors: 'no-public', - methods: 'explicit', - properties: 'explicit', - parameterProperties: 'off', - }, - }, - ], - '@typescript-eslint/no-non-null-assertion': 'off', - '@typescript-eslint/no-parameter-properties': [ - 'warn', - { - allows: [ - 'private', - 'protected', - 'public', - 'private readonly', - 'protected readonly', - 'public readonly', - ], - }, - ], - '@typescript-eslint/no-unused-vars': 'off', - '@typescript-eslint/no-use-before-define': [ - 'error', - { - functions: false, - typedefs: false, - }, - ], - 'import/no-unresolved': 'off', - 'no-extra-semi': 'off', - '@typescript-eslint/no-extra-semi': ['off'], }, + extends: ['@exercism/eslint-config-tooling'], } diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3bcecdb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "cSpell.words": [ + "REPRESENTER", + "TSESTree", + "quasis" + ] +} diff --git a/package.json b/package.json index 0913ab1..0a77d3e 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@babel/core": "^7.13.15", "@babel/preset-env": "^7.13.15", "@babel/preset-typescript": "^7.13.0", + "@exercism/eslint-config-tooling": "^0.1.0", "@tsconfig/recommended": "^1.0.1", "@types/jest": "^26.0.22", "@types/node": "^14.14.41", diff --git a/src/AstParser.ts b/src/AstParser.ts index e392f93..7afb998 100644 --- a/src/AstParser.ts +++ b/src/AstParser.ts @@ -4,7 +4,7 @@ import type { TSESTree } from '@typescript-eslint/typescript-estree' import { NoSourceError } from './errors/NoSourceError' import { ParserError } from './errors/ParserError' -import { Input } from './input/Input' +import type { Input } from './input/Input' import { getProcessLogger } from './utils/logger' type Program = TSESTree.Program @@ -82,6 +82,7 @@ export class AstParser { * does not hold locational information (where differences are caused by * whitespace differences) or commentary. */ + // eslint-disable-next-line @typescript-eslint/naming-convention public static REPRESENTER: AstParser = new AstParser({ comment: false, loc: false, @@ -93,6 +94,7 @@ export class AstParser { * hold locational information (in order to be able to extract tokens), but * does not hold any commentary. */ + // eslint-disable-next-line @typescript-eslint/naming-convention public static ANALYZER: AstParser = new AstParser({ comment: false, loc: true, @@ -119,7 +121,9 @@ export class AstParser { const logger = getProcessLogger() logger.log(`=> inputs: ${sources.length}`) - sources.forEach((source): void => logger.log(`\n${source}\n`)) + sources.forEach((source): void => { + logger.log(`\n${source}\n`) + }) if (sources.length === 0) { throw new NoSourceError() @@ -130,8 +134,12 @@ export class AstParser { (source): ParsedSource => new ParsedSource(parseToTree(source, this.options), source) ) - } catch (error) { - throw new ParserError(error) + } catch (error: unknown) { + if (error instanceof Error) { + throw new ParserError(error) + } else { + throw error + } } } } diff --git a/src/AstTraverser.ts b/src/AstTraverser.ts index 7a1cb8d..6b15f96 100644 --- a/src/AstTraverser.ts +++ b/src/AstTraverser.ts @@ -1,5 +1,5 @@ import { visitorKeys } from '@typescript-eslint/visitor-keys' -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node @@ -125,5 +125,5 @@ export class AstTraverser { } export function traverse(root: Node, options: TraverseOptions): void { - return new AstTraverser(options).traverse(root) + return void new AstTraverser(options).traverse(root) } diff --git a/src/errors/NoSourceAnnotations.ts b/src/errors/NoSourceAnnotations.ts index e5a4f05..943551d 100644 --- a/src/errors/NoSourceAnnotations.ts +++ b/src/errors/NoSourceAnnotations.ts @@ -1,4 +1,4 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { AnalysisError } from './AnalysisError' import { WRONG_ANALYZER_FOR_METHOD } from './codes' diff --git a/src/errors/ParserError.ts b/src/errors/ParserError.ts index 7526c21..e422aff 100644 --- a/src/errors/ParserError.ts +++ b/src/errors/ParserError.ts @@ -6,9 +6,9 @@ export class ParserError extends AnalysisError { constructor( public readonly original: Error & { - lineNumber: number - column: number - index: number + lineNumber?: number + column?: number + index?: number }, public readonly source?: string ) { diff --git a/src/errors/handler.ts b/src/errors/handler.ts index e4fe34b..bc3ed35 100644 --- a/src/errors/handler.ts +++ b/src/errors/handler.ts @@ -20,7 +20,7 @@ export function reportException< } const errorMessage = ` -An uncaughtException occurred (code: ${err.code || GENERIC_FAILURE}). +An uncaughtException occurred (code: ${err.code ?? GENERIC_FAILURE}). Error Data: ${JSON.stringify(err)} @@ -34,7 +34,7 @@ ${err.stack ? err.stack : ''} if (typeof process !== 'undefined') { // Write error to stderr as well - process.stderr && process.stderr.write(errorMessage) + process.stderr?.write(errorMessage) } if (process.exit) { diff --git a/src/extracts/extract_exports.ts b/src/extracts/extract_exports.ts index 5df617c..3940fa9 100644 --- a/src/extracts/extract_exports.ts +++ b/src/extracts/extract_exports.ts @@ -1,5 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' -import { Expression } from 'typescript' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { traverse } from '../AstTraverser' import { guardIdentifier } from '../guards/is_identifier' import { guardLiteral } from '../guards/is_literal' @@ -258,7 +258,7 @@ export function extractExports(root: Node): ExtractedExport[] { exports.push( new ExtractedExport( node, - declaration.id?.name || ANONYMOUS, + declaration.id?.name ?? ANONYMOUS, 'default', 'value', 'class' @@ -273,7 +273,7 @@ export function extractExports(root: Node): ExtractedExport[] { exports.push( new ExtractedExport( node, - declaration.id?.name || ANONYMOUS, + declaration.id?.name ?? ANONYMOUS, 'default', 'value', 'function' diff --git a/src/extracts/extract_functions.ts b/src/extracts/extract_functions.ts index ad4bf8f..618ace3 100644 --- a/src/extracts/extract_functions.ts +++ b/src/extracts/extract_functions.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { traverse } from '../AstTraverser' import { guardIdentifier } from '../guards' import { ExtractedVariable } from './extract_variables' diff --git a/src/extracts/extract_source.ts b/src/extracts/extract_source.ts index ac5d96e..89f2069 100644 --- a/src/extracts/extract_source.ts +++ b/src/extracts/extract_source.ts @@ -1,4 +1,4 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { NoSourceAnnotations } from '../errors' type NodeWithLocation = TSESTree.Node & { diff --git a/src/extracts/extract_tests.ts b/src/extracts/extract_tests.ts index a6541c8..67877bc 100644 --- a/src/extracts/extract_tests.ts +++ b/src/extracts/extract_tests.ts @@ -1,10 +1,11 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { traverse } from '../AstTraverser' -import { +import type { CallExpression, - guardCallExpression, SpecificFunctionCall, } from '../guards/is_call_expression' +import { guardCallExpression } from '../guards/is_call_expression' import { guardIdentifier } from '../guards/is_identifier' import { guardLiteral } from '../guards/is_literal' import { guardMemberExpression } from '../guards/is_member_expression' diff --git a/src/extracts/extract_variables.ts b/src/extracts/extract_variables.ts index afc6866..f3a4c49 100644 --- a/src/extracts/extract_variables.ts +++ b/src/extracts/extract_variables.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { findAll } from '../queries/find_all' import { guardIdentifier } from '../guards/is_identifier' @@ -6,6 +7,7 @@ type Node = TSESTree.Node type BindingName = TSESTree.BindingName type Expression = TSESTree.Expression type VariableDeclaration = TSESTree.VariableDeclaration +type VariableDeclarator = TSESTree.VariableDeclarator export class ExtractedVariable { public readonly name: string | null @@ -31,37 +33,51 @@ export function extractVariables(root: Node): ExtractedVariable[] { node.type === AST_NODE_TYPES.VariableDeclaration ) - return declarations.flatMap((node) => { - const { declarations, kind } = node + return declarations.flatMap( + (node: VariableDeclaration): ExtractedVariable[] => { + const { declarations, kind } = node - return declarations.flatMap((declarator) => { - switch (declarator.id.type) { - // const identifier = ... - // const identifier, identifier = ... - case AST_NODE_TYPES.Identifier: { - return [ - new ExtractedVariable(node, declarator.id, kind, declarator.init), - ] - } + return declarations.flatMap( + (declarator: VariableDeclarator): ExtractedVariable[] => { + switch (declarator.id.type) { + // const identifier = ... + // const identifier, identifier = ... + case AST_NODE_TYPES.Identifier: { + return [ + new ExtractedVariable( + node, + declarator.id, + kind, + declarator.init + ), + ] + } - // const [identifier, identifier2] = ... - case AST_NODE_TYPES.ArrayPattern: { - return declarator.id.elements - .map((element) => { - if (element === null || !guardIdentifier(element)) { - return null - } + // const [identifier, identifier2] = ... + case AST_NODE_TYPES.ArrayPattern: { + return declarator.id.elements + .map((element) => { + if (element === null || !guardIdentifier(element)) { + return null + } - return new ExtractedVariable(node, element, kind, declarator.init) - }) - .filter(Boolean) as ExtractedVariable[] - } + return new ExtractedVariable( + node, + element, + kind, + declarator.init + ) + }) + .filter(Boolean) as ExtractedVariable[] + } - // - default: { - return [] + // + default: { + return [] + } + } } - } - }) - }) + ) + } + ) } diff --git a/src/guards/is_assignment_pattern.ts b/src/guards/is_assignment_pattern.ts index e114ab6..0edcc2d 100644 --- a/src/guards/is_assignment_pattern.ts +++ b/src/guards/is_assignment_pattern.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { guardIdentifier } from './is_identifier' import { guardLiteral } from './is_literal' diff --git a/src/guards/is_binary_expression.ts b/src/guards/is_binary_expression.ts index 7795029..8a99e83 100644 --- a/src/guards/is_binary_expression.ts +++ b/src/guards/is_binary_expression.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type BinaryExpression = TSESTree.BinaryExpression diff --git a/src/guards/is_call_expression.ts b/src/guards/is_call_expression.ts index 997be5c..daf408a 100644 --- a/src/guards/is_call_expression.ts +++ b/src/guards/is_call_expression.ts @@ -1,10 +1,9 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' -import { - guardMemberExpression, - SpecificObject, - SpecificProperty, -} from './is_member_expression' -import { guardIdentifier, Identifier } from './is_identifier' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' +import type { SpecificObject, SpecificProperty } from './is_member_expression' +import { guardMemberExpression } from './is_member_expression' +import type { Identifier } from './is_identifier' +import { guardIdentifier } from './is_identifier' type Node = TSESTree.Node export type CallExpression = TSESTree.CallExpression diff --git a/src/guards/is_identifier.ts b/src/guards/is_identifier.ts index 4113e1c..61cc881 100644 --- a/src/guards/is_identifier.ts +++ b/src/guards/is_identifier.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type Identifier = TSESTree.Identifier @@ -15,10 +16,10 @@ export function guardIdentifier( node: Node | null, name?: string ): node is Omit & Identifier { - return ( - !!node && - node.type === AST_NODE_TYPES.Identifier && - (name === undefined || node.name === name) + return Boolean( + node && + node.type === AST_NODE_TYPES.Identifier && + (name === undefined || node.name === name) ) } diff --git a/src/guards/is_literal.ts b/src/guards/is_literal.ts index 052196c..1392be1 100644 --- a/src/guards/is_literal.ts +++ b/src/guards/is_literal.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type Literal = TSESTree.Literal diff --git a/src/guards/is_logical_expression.ts b/src/guards/is_logical_expression.ts index 77f1aaa..4d3d47c 100644 --- a/src/guards/is_logical_expression.ts +++ b/src/guards/is_logical_expression.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type LogicalExpression = TSESTree.LogicalExpression diff --git a/src/guards/is_member_expression.ts b/src/guards/is_member_expression.ts index 713bd04..f773520 100644 --- a/src/guards/is_member_expression.ts +++ b/src/guards/is_member_expression.ts @@ -1,6 +1,9 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' -import { Identifier, guardIdentifier } from './is_identifier' -import { Literal, guardLiteral } from './is_literal' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' +import type { Identifier } from './is_identifier' +import { guardIdentifier } from './is_identifier' +import type { Literal } from './is_literal' +import { guardLiteral } from './is_literal' type Node = TSESTree.Node export type MemberExpression = TSESTree.MemberExpression diff --git a/src/guards/is_return_block_statement.ts b/src/guards/is_return_block_statement.ts index 2e50fd6..2248d47 100644 --- a/src/guards/is_return_block_statement.ts +++ b/src/guards/is_return_block_statement.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node diff --git a/src/guards/is_return_statement_with_value.ts b/src/guards/is_return_statement_with_value.ts index f0bf8f6..239aa9b 100644 --- a/src/guards/is_return_statement_with_value.ts +++ b/src/guards/is_return_statement_with_value.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node diff --git a/src/guards/is_template_literal.ts b/src/guards/is_template_literal.ts index af515dd..b8a6b59 100644 --- a/src/guards/is_template_literal.ts +++ b/src/guards/is_template_literal.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type TemplateLiteral = TSESTree.TemplateLiteral diff --git a/src/guards/is_unary_expression.ts b/src/guards/is_unary_expression.ts index 79ebb34..5a6932e 100644 --- a/src/guards/is_unary_expression.ts +++ b/src/guards/is_unary_expression.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type UnaryExpression = TSESTree.UnaryExpression diff --git a/src/guards/is_variable_declaration_of_kind.ts b/src/guards/is_variable_declaration_of_kind.ts index 6a2fe4a..f63eedb 100644 --- a/src/guards/is_variable_declaration_of_kind.ts +++ b/src/guards/is_variable_declaration_of_kind.ts @@ -1,4 +1,5 @@ -import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' type Node = TSESTree.Node export type VariableKind = TSESTree.VariableDeclaration['kind'] diff --git a/src/input/DirectoryInput.ts b/src/input/DirectoryInput.ts index cbd433e..29c5772 100644 --- a/src/input/DirectoryInput.ts +++ b/src/input/DirectoryInput.ts @@ -2,7 +2,8 @@ import { FileInput } from './FileInput' import nodePath from 'path' import { readDir } from '../utils/fs' -import { Input, TrackOptions, getTrackOptions } from './Input' +import type { Input, TrackOptions } from './Input' +import { getTrackOptions } from './Input' export class DirectoryInput implements Input { constructor( diff --git a/src/input/DirectoryWithConfigInput.ts b/src/input/DirectoryWithConfigInput.ts index 7d4b877..677abc1 100644 --- a/src/input/DirectoryWithConfigInput.ts +++ b/src/input/DirectoryWithConfigInput.ts @@ -1,5 +1,6 @@ import { FileInput } from './FileInput' -import { getTrackOptions, Input } from './Input' +import type { Input } from './Input' +import { getTrackOptions } from './Input' import fs from 'fs' import path from 'path' @@ -14,11 +15,14 @@ export interface MetaConfiguration { exemplar?: string[] example?: string[] } + // eslint-disable-next-line @typescript-eslint/naming-convention forked_from?: string[] } export type PersonReference = { + // eslint-disable-next-line @typescript-eslint/naming-convention github_username: string + // eslint-disable-next-line @typescript-eslint/naming-convention exercism_username: string } @@ -49,8 +53,8 @@ export class DirectoryWithConfigInput implements Input { ) } - private configuration: MetaConfiguration - private trackOptions = getTrackOptions() + private readonly configuration: MetaConfiguration + private readonly trackOptions = getTrackOptions() constructor(private readonly directory: string) { const pathName = path.join(directory, '.meta', 'config.json') diff --git a/src/input/FileInput.ts b/src/input/FileInput.ts index 80ebc6e..0e7e285 100644 --- a/src/input/FileInput.ts +++ b/src/input/FileInput.ts @@ -1,6 +1,7 @@ import { readFile } from '../utils/fs' import { basename } from 'path' -import { getTrackOptions, Input, TrackOptions } from './Input' +import type { Input, TrackOptions } from './Input' +import { getTrackOptions } from './Input' export class FileInput implements Input { public readonly fileName: string diff --git a/src/input/InlineInput.ts b/src/input/InlineInput.ts index 9ea1580..5d30732 100644 --- a/src/input/InlineInput.ts +++ b/src/input/InlineInput.ts @@ -1,4 +1,4 @@ -import { Input } from './Input' +import type { Input } from './Input' export class InlineInput implements Input { /** diff --git a/src/input/Input.ts b/src/input/Input.ts index 5f32be3..9fd05dd 100644 --- a/src/input/Input.ts +++ b/src/input/Input.ts @@ -31,7 +31,7 @@ export function setTestFiles(pattern: RegExp): void { } /** - * Sets the DEFAULT configuration files patern globally + * Sets the DEFAULT configuration files pattern globally * @param pattern */ export function setConfigurationFiles(pattern: RegExp): void { @@ -44,5 +44,5 @@ export interface Input { * @param n the number * @returns at most `n` strings */ - read(n?: number): Promise + read: (n?: number) => Promise } diff --git a/src/queries/find_all.ts b/src/queries/find_all.ts index 82b627f..e3118ee 100644 --- a/src/queries/find_all.ts +++ b/src/queries/find_all.ts @@ -1,4 +1,4 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { traverse } from '../AstTraverser' type Node = TSESTree.Node diff --git a/src/queries/find_first.ts b/src/queries/find_first.ts index 588309f..22921b2 100644 --- a/src/queries/find_first.ts +++ b/src/queries/find_first.ts @@ -1,5 +1,6 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' -import { AstTraverser, traverse } from '../AstTraverser' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import type { AstTraverser } from '../AstTraverser' +import { traverse } from '../AstTraverser' type Node = TSESTree.Node diff --git a/src/queries/find_first_of_type.ts b/src/queries/find_first_of_type.ts index 493e378..1eec7c7 100644 --- a/src/queries/find_first_of_type.ts +++ b/src/queries/find_first_of_type.ts @@ -1,4 +1,4 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { findFirst } from './find_first' type Node = TSESTree.Node diff --git a/src/queries/find_literal.ts b/src/queries/find_literal.ts index 3b33ffe..bfd04a1 100644 --- a/src/queries/find_literal.ts +++ b/src/queries/find_literal.ts @@ -1,6 +1,6 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { findFirst } from './find_first' -import { isLiteral } from '../guards/is_literal' +import { guardLiteral } from '../guards/is_literal' type Node = TSESTree.Node type Literal = TSESTree.Literal @@ -10,6 +10,7 @@ export function findLiteral( root: Node, value: LiteralValue ): Literal | undefined { - const isLiteralValue = (node: Node): node is Literal => isLiteral(node, value) + const isLiteralValue = (node: Node): node is Literal => + guardLiteral(node, value) return findFirst(root, isLiteralValue) } diff --git a/src/queries/find_member_call.ts b/src/queries/find_member_call.ts index 351d4ad..a71de1b 100644 --- a/src/queries/find_member_call.ts +++ b/src/queries/find_member_call.ts @@ -1,5 +1,6 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' -import { CallExpression, guardCallExpression } from '../guards' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import type { CallExpression } from '../guards' +import { guardCallExpression } from '../guards' import { guardMemberExpression } from '../guards/is_member_expression' import { findFirst } from './find_first' @@ -13,5 +14,5 @@ export function findMemberCall( const isMemberCall = (node: Node): node is CallExpression => guardCallExpression(node) && guardMemberExpression(node.callee, object, property) - return findFirst(root, isMemberCall) as CallExpression | undefined + return findFirst(root, isMemberCall) } diff --git a/src/queries/find_new_expression.ts b/src/queries/find_new_expression.ts index 28145af..0e4a2d6 100644 --- a/src/queries/find_new_expression.ts +++ b/src/queries/find_new_expression.ts @@ -1,6 +1,7 @@ -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' import { findFirst } from './find_first' -import { isIdentifier } from '../guards/is_identifier' +import { guardIdentifier } from '../guards/is_identifier' type Node = TSESTree.Node type NewExpression = TSESTree.NewExpression @@ -19,7 +20,7 @@ export function isNewExpression( ): node is NewExpression { return ( node.type === AST_NODE_TYPES.NewExpression && - (!className || isIdentifier(node.callee, className)) + (!className || guardIdentifier(node.callee, className)) ) } @@ -28,6 +29,6 @@ export function findNewExpression( className: T ): NewExpressionWithName | undefined { const isNewClass = (node: Node): node is NewExpressionWithName => - isNewExpression(node) && isIdentifier(node.callee, className) - return findFirst(root, isNewClass) as NewExpressionWithName | undefined + isNewExpression(node) && guardIdentifier(node.callee, className) + return findFirst(root, isNewClass) } diff --git a/src/queries/find_raw_literal.ts b/src/queries/find_raw_literal.ts index d873a44..43e0d3e 100644 --- a/src/queries/find_raw_literal.ts +++ b/src/queries/find_raw_literal.ts @@ -1,12 +1,12 @@ -import { TSESTree } from '@typescript-eslint/typescript-estree' +import type { TSESTree } from '@typescript-eslint/typescript-estree' import { findFirst } from './find_first' -import { isLiteral } from '../guards/is_literal' +import { guardLiteral } from '../guards/is_literal' type Node = TSESTree.Node type Literal = TSESTree.Literal export function findRawLiteral(root: Node, raw: string): Literal | undefined { const isLiteralValue = (node: Node): node is Literal => - isLiteral(node) && node.raw === raw - return findFirst(root, isLiteralValue) as Literal | undefined + guardLiteral(node) && node.raw === raw + return findFirst(root, isLiteralValue) } diff --git a/src/queries/find_top_level_constants.ts b/src/queries/find_top_level_constants.ts index 63945e7..1220602 100644 --- a/src/queries/find_top_level_constants.ts +++ b/src/queries/find_top_level_constants.ts @@ -1,10 +1,9 @@ import { findAll } from './find_all' -import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree' -import { - guardVariableDeclarationOfKind, - VariableKind, -} from '../guards/is_variable_declaration_of_kind' -import { AstTraverser } from '../AstTraverser' +import type { TSESTree } from '@typescript-eslint/typescript-estree' +import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree' +import type { VariableKind } from '../guards/is_variable_declaration_of_kind' +import { guardVariableDeclarationOfKind } from '../guards/is_variable_declaration_of_kind' +import type { AstTraverser } from '../AstTraverser' type Node = TSESTree.Node type VariableDeclaration = TSESTree.VariableDeclaration @@ -28,7 +27,7 @@ function isTopLevelConstant( return true } - if (CONSTANT_MODIFIERS.indexOf(node.type) === -1) { + if (!CONSTANT_MODIFIERS.includes(node.type)) { this.skip() // doesn't traverse this node any further } @@ -55,15 +54,15 @@ export function findTopLevelConstants( return false } - ) as VariableDeclaration[] + )! - return constants.reduce( + return constants.reduce( (declarations, declaration): ProgramConstants => declarations.concat( declaration.declarations.map( (d): ProgramConstant => ({ ...d, kind: declaration.kind }) ) ), - [] as ProgramConstants + [] ) } diff --git a/src/tsconfig.json b/src/tsconfig.json deleted file mode 100644 index 7f41c25..0000000 --- a/src/tsconfig.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": "../tsconfig.json", - "include": ["./"] -} diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 5938135..3ddca6f 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -47,8 +47,12 @@ export class Logger { return Object.freeze({ fatal, - error: console ? global.console.error : error, - log: debug ? (console ? global.console.log : log) : noop, + error: console ? global.console.error.bind(global.console) : error, + log: debug + ? console + ? global.console.log.bind(global.console) + : log + : noop, }) } } @@ -69,5 +73,5 @@ export function setProcessLogger(logger: Readonly): Readonly { * Get the 'global' logger */ export function getProcessLogger(): Logger { - return LIVE_BINDING.current || NOOP_LOGGER + return LIVE_BINDING.current ?? NOOP_LOGGER } diff --git a/test/.eslintrc b/test/.eslintrc deleted file mode 100644 index 3cdf546..0000000 --- a/test/.eslintrc +++ /dev/null @@ -1,19 +0,0 @@ -{ - "parser": "@typescript-eslint/parser", - "parserOptions": { - "project": "./test/tsconfig.eslint.json", - "ecmaVersion": 2018, - "sourceType": "module", - "ecmaFeatures": { - "modules": true - } - }, - "env": { - "es6": true, - "node": true, - "jest": true - }, - "extends": [ - "../.eslintrc" - ] -} diff --git a/test/tsconfig.eslint.json b/test/tsconfig.eslint.json deleted file mode 100644 index ec15924..0000000 --- a/test/tsconfig.eslint.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "compilerOptions": { "strict": true }, - "include": ["../src/**/*.ts", "./**/*.ts"] -} diff --git a/test/tsconfig.json b/test/tsconfig.json index 130badb..e50b1a1 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -7,6 +7,6 @@ }, "noEmit": true }, - "include": ["./", "../src"], + "include": ["./", "../src", "../tsconfig.eslint.json"], "exclude": ["./fixtures"] } diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json new file mode 100644 index 0000000..02d07d1 --- /dev/null +++ b/tsconfig.eslint.json @@ -0,0 +1,16 @@ +{ + "extends": "@tsconfig/recommended/tsconfig.json", + "compilerOptions": { + "target": "ES2016" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, + "module": "CommonJs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, + "isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */, + "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */, + "paths": { + "~src/*": ["src/*"], + "~test/*": ["test/*"] + }, + "noEmit": true + }, + "include": ["src", "test"], + "exclude": ["node_modules", "test/fixtures"] +} diff --git a/yarn.lock b/yarn.lock index c499c0d..585f1a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1224,6 +1224,11 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" +"@exercism/eslint-config-tooling@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@exercism/eslint-config-tooling/-/eslint-config-tooling-0.1.0.tgz#a70b3dd8fec09b839f849e91ae8663be7e9fd4b9" + integrity sha512-rGMQ1WVvrR7RiJ66Zw3t2X3QfiNALDwd2G5ky0kdPz83t45I5wZy1potnxGONUEon81l+wy+7QfN8UbTTeCUzQ== + "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"