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

Use eslint-tooling #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ production_node_modules/*
test/fixtures/*
tmp/*
jest.config.js
/.eslintrc.js
70 changes: 2 additions & 68 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": [
"REPRESENTER",
"TSESTree",
"quasis"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 12 additions & 4 deletions src/AstParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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
}
}
}
}
4 changes: 2 additions & 2 deletions src/AstTraverser.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion src/errors/NoSourceAnnotations.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
6 changes: 3 additions & 3 deletions src/errors/ParserError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
4 changes: 2 additions & 2 deletions src/errors/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -34,7 +34,7 @@ ${err.stack ? err.stack : '<no stack>'}

if (typeof process !== 'undefined') {
// Write error to stderr as well
process.stderr && process.stderr.write(errorMessage)
process.stderr?.write(errorMessage)
}

if (process.exit) {
Expand Down
8 changes: 4 additions & 4 deletions src/extracts/extract_exports.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'
Expand All @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/extracts/extract_functions.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/extracts/extract_source.ts
Original file line number Diff line number Diff line change
@@ -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 & {
Expand Down
7 changes: 4 additions & 3 deletions src/extracts/extract_tests.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
74 changes: 45 additions & 29 deletions src/extracts/extract_variables.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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'

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
Expand All @@ -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 []
}
}
}
}
})
})
)
}
)
}
3 changes: 2 additions & 1 deletion src/guards/is_assignment_pattern.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
3 changes: 2 additions & 1 deletion src/guards/is_binary_expression.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 6 additions & 7 deletions src/guards/is_call_expression.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading