From 05e08b54dfaf72f613827c48fd4758db88129773 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Thu, 18 Feb 2021 07:35:36 -0800 Subject: [PATCH] eslint formatting --- src/calc/algebraic_operation.ts | 12 +++++----- src/calc/ast_utils.ts | 2 +- src/calc/calc.ts | 24 ++++++++----------- src/calc/column.ts | 12 ++++------ src/calc/constant.ts | 4 ++-- src/calc/destination.ts | 32 +++++++++++-------------- src/calc/display_directive.ts | 4 ++-- src/calc/range.ts | 40 +++++++++++++++---------------- src/calc/reference.ts | 4 ++-- src/calc/row.ts | 8 +++---- src/calc/single_param_function.ts | 2 +- 11 files changed, 66 insertions(+), 78 deletions(-) diff --git a/src/calc/algebraic_operation.ts b/src/calc/algebraic_operation.ts index a937428..0f68966 100644 --- a/src/calc/algebraic_operation.ts +++ b/src/calc/algebraic_operation.ts @@ -1,11 +1,11 @@ import { Table } from '..'; import { err, ok, Result } from '../neverthrow/neverthrow'; +import { Cell, checkChildLength, checkType, ValueProvider } from './ast_utils'; import { Source } from './calc'; +import { Constant } from './constant'; import { Value } from './results'; import { IToken } from 'ebnf'; import { map } from 'lodash'; -import { Cell, checkChildLength, checkType, ValueProvider } from './ast_utils'; -import { Constant } from './constant'; export class AlgebraicOperation implements ValueProvider { private readonly leftSource: ValueProvider; @@ -66,11 +66,11 @@ export class AlgebraicOperation implements ValueProvider { canHaveRightRange: boolean, fn: (left: number, right: number) => number, ): Result => { - let leftValue = this.leftSource.getValue(table, cell); + const leftValue = this.leftSource.getValue(table, cell); if (leftValue.isErr()) { return err(leftValue.error); } - let rightValue = this.rightSource.getValue(table, cell); + const rightValue = this.rightSource.getValue(table, cell); if (rightValue.isErr()) { return err(rightValue.error); } @@ -107,7 +107,7 @@ export class AlgebraicOperation implements ValueProvider { }), ); return ok(new Value(result)); - } else { + } const leftCellValue = leftValue.value.getAsFloat(0, 0); const result: string[][] = map( @@ -122,7 +122,7 @@ export class AlgebraicOperation implements ValueProvider { }), ); return ok(new Value(result)); - } + }; private readonly add = (table: Table, cell: Cell): Result => diff --git a/src/calc/ast_utils.ts b/src/calc/ast_utils.ts index 8bd57b6..bb0f205 100644 --- a/src/calc/ast_utils.ts +++ b/src/calc/ast_utils.ts @@ -1,7 +1,7 @@ -import { IToken } from 'ebnf'; import { Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { Value } from './results'; +import { IToken } from 'ebnf'; export const errIndex0 = new Error('Index 0 used to create a reference'); export const errRelativeReferenceIndex = new Error( diff --git a/src/calc/calc.ts b/src/calc/calc.ts index bbfcaf8..e5d996c 100644 --- a/src/calc/calc.ts +++ b/src/calc/calc.ts @@ -2,20 +2,20 @@ import { err, ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { AlgebraicOperation } from './algebraic_operation'; import { Cell, checkChildLength, checkType, ValueProvider } from './ast_utils'; -import { Reference } from './reference'; import { ConditionalFunctionCall } from './conditional_function'; -import { Range } from './range'; -import { Value } from './results'; -import { SingleParamFunctionCall } from './single_param_function'; -import { Grammars, IToken } from 'ebnf'; -import { concat } from 'lodash'; +import { Constant } from './constant'; +import { Destination, newDestination } from './destination'; import { DefaultFormatter, DisplayDirective, Formatter, } from './display_directive'; -import { Destination, newDestination } from './destination'; -import { Constant } from './constant'; +import { Range } from './range'; +import { Reference } from './reference'; +import { Value } from './results'; +import { SingleParamFunctionCall } from './single_param_function'; +import { Grammars, IToken } from 'ebnf'; +import { concat } from 'lodash'; /** * W3C grammar describing a valid formula at the bottom of a table. @@ -82,9 +82,7 @@ export class Formula { this.source = new Source(ast.children[1], table); } - public merge = (table: Table): Result => { - return this.destination.merge(this.source, table); - }; + public merge = (table: Table): Result => this.destination.merge(this.source, table); } export class Source { @@ -109,9 +107,7 @@ export class Source { /** * getValue returns the evaluated value for this source recursively. */ - public getValue = (table: Table, currentCell: Cell): Result => { - return this.locationDescriptor.getValue(table, currentCell); - }; + public getValue = (table: Table, currentCell: Cell): Result => this.locationDescriptor.getValue(table, currentCell); } const newValueProvider = ( diff --git a/src/calc/column.ts b/src/calc/column.ts index f56bc3c..0d16b51 100644 --- a/src/calc/column.ts +++ b/src/calc/column.ts @@ -1,4 +1,3 @@ -import { IToken } from 'ebnf'; import { err, ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { @@ -10,6 +9,7 @@ import { ValueProvider, } from './ast_utils'; import { Value } from './results'; +import { IToken } from 'ebnf'; export const newColumn = (ast: IToken, table: Table): Result => { try { @@ -22,7 +22,7 @@ export const newColumn = (ast: IToken, table: Table): Result => { return err( new Error( `Formula element '${ast.text}' is a ${ast.type} but expected an ` + - `relatve_column or absolute_column in this position.`, + 'relatve_column or absolute_column in this position.', ), ); } @@ -45,7 +45,7 @@ export abstract class Column implements ValueProvider { } class RelativeColumn extends Column { - private offset: number; + private readonly offset: number; constructor(ast: IToken, table: Table) { super(); @@ -64,9 +64,7 @@ class RelativeColumn extends Column { this.offset = multiplier * parseInt(ast.children[0].text); } - public getIndex = (currentCell: Cell): number => { - return currentCell.column + this.offset; - }; + public getIndex = (currentCell: Cell): number => currentCell.column + this.offset; public getAbsoluteIndex = (): Result => err(errRelativeReferenceIndex); @@ -95,7 +93,7 @@ export class AbsoluteColumn extends Column { default: throw new Error( `Formula element '${ast.text}' is a ${ast.type} but expected ` + - `a 'absolute_column' in this position.`, + 'a \'absolute_column\' in this position.', ); } diff --git a/src/calc/constant.ts b/src/calc/constant.ts index 8c7c2f2..781fc30 100644 --- a/src/calc/constant.ts +++ b/src/calc/constant.ts @@ -1,11 +1,11 @@ -import { IToken } from 'ebnf'; import { ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { Cell, checkType, ValueProvider } from './ast_utils'; import { Value } from './results'; +import { IToken } from 'ebnf'; export class Constant implements ValueProvider { - private value: number; + private readonly value: number; constructor(ast: IToken, table: Table) { const typeErr = checkType(ast, 'real'); diff --git a/src/calc/destination.ts b/src/calc/destination.ts index 7263d8f..f33214f 100644 --- a/src/calc/destination.ts +++ b/src/calc/destination.ts @@ -1,5 +1,3 @@ -import { IToken } from 'ebnf'; -import { range } from 'lodash'; import { err, ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { Cell, checkChildLength, checkType, errIndex0 } from './ast_utils'; @@ -8,6 +6,8 @@ import { AbsoluteColumn } from './column'; import { Formatter } from './display_directive'; import { Range } from './range'; import { AbsoluteRow } from './row'; +import { IToken } from 'ebnf'; +import { range } from 'lodash'; export interface Destination { merge(source: Source, table: Table): Result; @@ -58,8 +58,8 @@ export const newDestination = ( }; export class RowDestination implements Destination { - private row: AbsoluteRow; - private formatter: Formatter; + private readonly row: AbsoluteRow; + private readonly formatter: Formatter; constructor(ast: IToken, table: Table, formatter: Formatter) { this.formatter = formatter; @@ -86,17 +86,15 @@ export class RowDestination implements Destination { public merge = (source: Source, table: Table): Result => { // for cell in row... const cells = range(0, table.getWidth()).map( - (columnNum): Cell => { - return { row: this.row.index, column: columnNum }; - }, + (columnNum): Cell => ({ row: this.row.index, column: columnNum }), ); return mergeForCells(source, table, cells, this.formatter); }; } export class ColumnDestination implements Destination { - private column: AbsoluteColumn; - private formatter: Formatter; + private readonly column: AbsoluteColumn; + private readonly formatter: Formatter; constructor(ast: IToken, table: Table, formatter: Formatter) { this.formatter = formatter; @@ -123,18 +121,16 @@ export class ColumnDestination implements Destination { public merge = (source: Source, table: Table): Result => { // for cell in column (excluding header)... const cells = range(2, table.getHeight()).map( - (rowNum): Cell => { - return { row: rowNum, column: this.column.index }; - }, + (rowNum): Cell => ({ row: rowNum, column: this.column.index }), ); return mergeForCells(source, table, cells, this.formatter); }; } export class CellDestination implements Destination { - private row: AbsoluteRow; - private column: AbsoluteColumn; - private formatter: Formatter; + private readonly row: AbsoluteRow; + private readonly column: AbsoluteColumn; + private readonly formatter: Formatter; constructor(ast: IToken, table: Table, formatter: Formatter) { this.formatter = formatter; @@ -169,13 +165,13 @@ export class CellDestination implements Destination { } export class RangeDestination implements Destination { - private range: Range; - private formatter: Formatter; + private readonly range: Range; + private readonly formatter: Formatter; constructor(ast: IToken, table: Table, formatter: Formatter) { this.formatter = formatter; - let typeErr = checkType(ast, 'range'); + const typeErr = checkType(ast, 'range'); if (typeErr) { throw typeErr; } diff --git a/src/calc/display_directive.ts b/src/calc/display_directive.ts index 68be972..54bb24b 100644 --- a/src/calc/display_directive.ts +++ b/src/calc/display_directive.ts @@ -1,5 +1,5 @@ -import { IToken } from 'ebnf'; import { checkChildLength, checkType } from './ast_utils'; +import { IToken } from 'ebnf'; export interface Formatter { format(num: number | string): string; @@ -15,7 +15,7 @@ export class DefaultFormatter { } export class DisplayDirective { - private decimalLength: number; + private readonly decimalLength: number; constructor(ast: IToken) { let typeError = checkType(ast, 'display_directive'); diff --git a/src/calc/range.ts b/src/calc/range.ts index ac1fb58..c8b05e6 100644 --- a/src/calc/range.ts +++ b/src/calc/range.ts @@ -1,18 +1,18 @@ import { err, ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { Cell, checkChildLength, checkType, ValueProvider } from './ast_utils'; +import { Column } from './column'; import { Reference } from './reference'; import { Value } from './results'; +import { Row } from './row'; import { IToken } from 'ebnf'; import { flatMap, map, range } from 'lodash'; -import { Row } from './row'; -import { Column } from './column'; export class Range implements ValueProvider { - private startRow: Row | undefined; - private startColumn: Column | undefined; - private endRow: Row | undefined; - private endColumn: Column | undefined; + private readonly startRow: Row | undefined; + private readonly startColumn: Column | undefined; + private readonly endRow: Row | undefined; + private readonly endColumn: Column | undefined; constructor(ast: IToken, table: Table) { let typeErr = checkType(ast, 'range'); @@ -79,19 +79,19 @@ export class Range implements ValueProvider { currentCell: Cell, ): Result => { // if no start column is provided, copy it from the currentCell - let startColumn = this.startColumn + const startColumn = this.startColumn ? this.startColumn.getIndex(currentCell) : currentCell.column; // if the column is provided in the first set, but not the second, copy it - let endColumn = this.endColumn + const endColumn = this.endColumn ? this.endColumn.getIndex(currentCell) : startColumn; - let startRow = this.startRow + const startRow = this.startRow ? this.startRow.getIndex(currentCell) : currentCell.row; - let endRow = this.endRow + const endRow = this.endRow ? this.endRow.getIndex(currentCell) : currentCell.row; @@ -124,10 +124,10 @@ export class Range implements ValueProvider { endColumn = this.startColumn; } - const startRowIndex = this.startRow.getAbsoluteIndex(), - endRowIndex = this.endRow.getAbsoluteIndex(), - startColumnIndex = this.startColumn.getAbsoluteIndex(), - endColumnIndex = endColumn.getAbsoluteIndex(); + const startRowIndex = this.startRow.getAbsoluteIndex(); + const endRowIndex = this.endRow.getAbsoluteIndex(); + const startColumnIndex = this.startColumn.getAbsoluteIndex(); + const endColumnIndex = endColumn.getAbsoluteIndex(); if ( startRowIndex.isErr() || @@ -140,17 +140,15 @@ export class Range implements ValueProvider { ); } - const minRow = Math.min(startRowIndex.value, endRowIndex.value), - maxRow = Math.max(startRowIndex.value, endRowIndex.value), - minColumn = Math.min(startColumnIndex.value, endColumnIndex.value), - maxColumn = Math.max(startColumnIndex.value, endColumnIndex.value); + const minRow = Math.min(startRowIndex.value, endRowIndex.value); + const maxRow = Math.max(startRowIndex.value, endRowIndex.value); + const minColumn = Math.min(startColumnIndex.value, endColumnIndex.value); + const maxColumn = Math.max(startColumnIndex.value, endColumnIndex.value); return ok( flatMap(range(minRow, maxRow + 1), (rowNum): Cell[] => range(minColumn, maxColumn + 1).map( - (colNum): Cell => { - return { row: rowNum, column: colNum }; - }, + (colNum): Cell => ({ row: rowNum, column: colNum }), ), ), ); diff --git a/src/calc/reference.ts b/src/calc/reference.ts index 15fe5fa..69fedda 100644 --- a/src/calc/reference.ts +++ b/src/calc/reference.ts @@ -1,10 +1,10 @@ import { ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { Cell, checkType, errIndex0 } from './ast_utils'; -import { IToken } from 'ebnf'; +import { Column, newColumn } from './column'; import { Value } from './results'; import { newRow, Row } from './row'; -import { Column, newColumn } from './column'; +import { IToken } from 'ebnf'; export class Reference { row: Row | undefined; diff --git a/src/calc/row.ts b/src/calc/row.ts index c0f7576..7611a7d 100644 --- a/src/calc/row.ts +++ b/src/calc/row.ts @@ -1,4 +1,3 @@ -import { IToken } from 'ebnf'; import { err, ok, Result } from '../neverthrow/neverthrow'; import { Table } from '../table'; import { @@ -10,6 +9,7 @@ import { ValueProvider, } from './ast_utils'; import { Value } from './results'; +import { IToken } from 'ebnf'; export const newRow = (ast: IToken, table: Table): Result => { try { @@ -22,7 +22,7 @@ export const newRow = (ast: IToken, table: Table): Result => { return err( new Error( `Formula element '${ast.text}' is a ${ast.type} but expected an ` + - `relatve_row or absolute_row in this position.`, + 'relatve_row or absolute_row in this position.', ), ); } @@ -46,7 +46,7 @@ export abstract class Row implements ValueProvider { } class RelativeRow extends Row { - private offset: number; + private readonly offset: number; constructor(ast: IToken, table: Table) { super(); @@ -95,7 +95,7 @@ export class AbsoluteRow extends Row { default: throw new Error( `Formula element '${ast.text}' is a ${ast.type} but expected ` + - `a 'absolute_row' in this position.`, + 'a \'absolute_row\' in this position.', ); } diff --git a/src/calc/single_param_function.ts b/src/calc/single_param_function.ts index 635ff2a..687db77 100644 --- a/src/calc/single_param_function.ts +++ b/src/calc/single_param_function.ts @@ -20,7 +20,7 @@ export class SingleParamFunctionCall implements ValueProvider { throw lengthError; } - let childTypeError = checkType(ast.children[0], 'single_param_function'); + const childTypeError = checkType(ast.children[0], 'single_param_function'); if (childTypeError) { throw childTypeError; }