Skip to content

Commit 2be29b2

Browse files
committed
Configure eslint to allow null assertions in test files
1 parent 8853e05 commit 2be29b2

29 files changed

+322
-309
lines changed

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,11 @@ module.exports = {
143143
'sort-keys': ['error', 'asc'],
144144
}
145145
},
146+
{
147+
files: ['**/*.spec.ts'],
148+
rules: {
149+
'@typescript-eslint/no-non-null-assertion': 'off',
150+
}
151+
}
146152
],
147153
}

src/DependencyGraph/AddressMapping/AddressMapping.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ export interface AddressMappingAddSheetOptions {
2222
throwIfSheetAlreadyExists: boolean,
2323
}
2424

25+
export interface AddressMappingGetCellOptions {
26+
throwIfSheetNotExists?: boolean,
27+
throwIfCellNotExists?: boolean,
28+
}
29+
2530
/**
2631
* Manages cell vertices and provides access to vertex by SimpleCellAddress.
2732
* For each sheet it stores vertices according to AddressMappingStrategy: DenseStrategy or SparseStrategy.
28-
* It also stores placeholder entries (DenseStrategy(0, 0)) for sheets that are used in formulas but not yet added.
33+
*
34+
* Pleceholder sheets:
35+
* - for placeholders sheets (sheets that are used in formulas but not yet added), it stores placeholder strategy entries (DenseStrategy(0, 0))
36+
* - placeholder strategy entries may contain EmptyCellVertex-es but never ValueCellVertex or FormulaVertex as they content is empty
37+
* - vertices in placeholder strategy entries are used only for dependency tracking
2938
*/
3039
export class AddressMapping {
3140
private mapping: Map<number, AddressMappingStrategy> = new Map()
@@ -37,21 +46,22 @@ export class AddressMapping {
3746
/**
3847
* Gets the cell vertex at the specified address.
3948
*/
40-
public getCell(address: SimpleCellAddress): Maybe<CellVertex> {
41-
const sheetMapping = this.getStrategyForSheetOrThrow(address.sheet)
42-
return sheetMapping.getCell(address)
43-
}
49+
public getCell(address: SimpleCellAddress, options: AddressMappingGetCellOptions = {}): Maybe<CellVertex> {
50+
const sheetMapping = this.mapping.get(address.sheet)
51+
if (!sheetMapping) {
52+
if (options.throwIfSheetNotExists) {
53+
throw new NoSheetWithIdError(address.sheet)
54+
}
55+
return undefined
56+
}
4457

45-
/**
46-
* Gets the cell vertex at the specified address or throws an error if not found.
47-
*/
48-
public getCellOrThrowError(address: SimpleCellAddress): CellVertex {
49-
const vertex = this.getCell(address)
58+
const cell = sheetMapping.getCell(address)
5059

51-
if (!vertex) {
60+
if (!cell && options.throwIfCellNotExists) {
5261
throw Error('Vertex for address missing in AddressMapping')
5362
}
54-
return vertex
63+
64+
return cell
5565
}
5666

5767
/**

src/DependencyGraph/DependencyGraph.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,15 @@ export class DependencyGraph {
596596
}
597597

598598
public fetchCell(address: SimpleCellAddress): CellVertex {
599-
return this.addressMapping.getCellOrThrowError(address)
599+
return this.addressMapping.getCell(address, { throwIfCellNotExists: true }) as CellVertex
600600
}
601601

602+
/**
603+
* Gets the cell vertex at the specified address.
604+
* @throws {NoSheetWithIdError} if sheet doesn't exist
605+
*/
602606
public getCell(address: SimpleCellAddress): Maybe<CellVertex> {
603-
return this.addressMapping.getCell(address)
607+
return this.addressMapping.getCell(address, { throwIfSheetNotExists: true })
604608
}
605609

606610
public getCellValue(address: SimpleCellAddress): InterpreterValue {
@@ -912,9 +916,9 @@ export class DependencyGraph {
912916
return [dependency.start, this.rangeMapping.getVertexOrThrow(dependency.start, dependency.end)]
913917
} else if (dependency instanceof NamedExpressionDependency) {
914918
const namedExpression = this.namedExpressions.namedExpressionOrPlaceholder(dependency.name, address.sheet)
915-
return [namedExpression.address, this.addressMapping.getCellOrThrowError(namedExpression.address)]
919+
return [namedExpression.address, this.addressMapping.getCell(namedExpression.address, { throwIfCellNotExists: true }) as CellVertex]
916920
} else {
917-
return [dependency, this.addressMapping.getCellOrThrowError(dependency)]
921+
return [dependency, this.addressMapping.getCell(dependency, { throwIfCellNotExists: true }) as CellVertex]
918922
}
919923
})
920924
} else {

src/Operations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ export class Operations {
925925
const targetRange = AbsoluteCellRange.spanFrom(destinationLeftCorner, width, height)
926926

927927
for (const formulaAddress of targetRange.addresses(this.dependencyGraph)) {
928-
const vertex = this.addressMapping.getCellOrThrowError(formulaAddress)
928+
const vertex = this.addressMapping.getCell(formulaAddress, { throwIfCellNotExists: true })
929929
if (vertex instanceof ScalarFormulaVertex && formulaAddress.sheet !== sourceLeftCorner.sheet) {
930930
const ast = vertex.getFormula(this.lazilyTransformingAstService)
931931
const { dependencies } = this.parser.fetchCachedResultForAst(ast)
@@ -942,7 +942,7 @@ export class Operations {
942942
}
943943

944944
const addedGlobalNamedExpressions: string[] = []
945-
const vertex = this.addressMapping.getCellOrThrowError(targetAddress)
945+
const vertex = this.addressMapping.getCell(targetAddress, { throwIfCellNotExists: true }) as CellVertex
946946

947947
for (const namedExpressionDependency of absolutizeDependencies(dependencies, targetAddress)) {
948948
if (!(namedExpressionDependency instanceof NamedExpressionDependency)) {

src/SimpleRangeValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class SimpleRangeValue {
112112
const ret = []
113113
for (let i = 0; i < this._data!.length; i++) {
114114
for (let j = 0; j < this._data![0].length; j++) {
115-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
115+
116116
ret.push(this._data![i][j])
117117
}
118118
}

src/interpreter/plugin/ConditionalAggregationPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AverageResult {
4848
/** Computes key for criterion function cache */
4949
function conditionalAggregationFunctionCacheKey(functionName: string): (conditions: Condition[]) => string {
5050
return (conditions: Condition[]): string => {
51-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
51+
5252
const conditionsStrings = conditions.map((c) => `${c.conditionRange.range!.sheet},${c.conditionRange.range!.start.col},${c.conditionRange.range!.start.row}`)
5353
return [functionName, ...conditionsStrings].join(',')
5454
}

src/parser/FormulaParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ export class FormulaParser extends EmbeddedActionsParser {
831831
}
832832
if (cellArg.reference.type === CellReferenceType.CELL_REFERENCE_RELATIVE
833833
|| cellArg.reference.type === CellReferenceType.CELL_REFERENCE_ABSOLUTE_ROW) {
834-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
834+
835835
absoluteCol = absoluteCol + this.formulaAddress.col
836836
}
837837

test/unit/address-mapping.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
1616

1717
mapping.setCell(address, vertex)
1818

19-
expect(mapping.getCellOrThrowError(address)).toBe(vertex)
19+
expect(mapping.getCell(address)).toBe(vertex)
2020
})
2121

2222
it('set and using different reference when get', () => {
@@ -25,7 +25,7 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
2525

2626
mapping.setCell(adr('A1'), vertex)
2727

28-
expect(mapping.getCellOrThrowError(adr('A1'))).toBe(vertex)
28+
expect(mapping.getCell(adr('A1'))).toBe(vertex)
2929
})
3030

3131
it("get when there's even no column", () => {
@@ -141,8 +141,8 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
141141

142142
mapping.setCell(adr('A2'), vertex1)
143143

144-
expect(mapping.getCellOrThrowError(adr('A1'))).toBe(vertex0)
145-
expect(mapping.getCellOrThrowError(adr('A2'))).toBe(vertex1)
144+
expect(mapping.getCell(adr('A1'))).toBe(vertex0)
145+
expect(mapping.getCell(adr('A2'))).toBe(vertex1)
146146
})
147147

148148
it('set overrides old value', () => {
@@ -153,7 +153,7 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
153153

154154
mapping.setCell(adr('A1'), vertex1)
155155

156-
expect(mapping.getCellOrThrowError(adr('A1'))).toBe(vertex1)
156+
expect(mapping.getCell(adr('A1'))).toBe(vertex1)
157157
})
158158

159159
it("has when there's even no column", () => {
@@ -192,7 +192,7 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
192192
mapping.addRows(0, 0, 1)
193193

194194
expect(mapping.getCell(adr('A1'))).toBe(undefined)
195-
expect(mapping.getCellOrThrowError(adr('A2'))).toEqual(new ValueCellVertex(42, 42))
195+
expect(mapping.getCell(adr('A2'))).toEqual(new ValueCellVertex(42, 42))
196196
expect(mapping.getSheetHeight(0)).toEqual(2)
197197
})
198198

@@ -204,9 +204,9 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
204204

205205
mapping.addRows(0, 1, 1)
206206

207-
expect(mapping.getCellOrThrowError(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
207+
expect(mapping.getCell(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
208208
expect(mapping.getCell(adr('A2'))).toBe(undefined)
209-
expect(mapping.getCellOrThrowError(adr('A3'))).toEqual(new ValueCellVertex(43, 43))
209+
expect(mapping.getCell(adr('A3'))).toEqual(new ValueCellVertex(43, 43))
210210
expect(mapping.getSheetHeight(0)).toEqual(3)
211211
})
212212

@@ -217,7 +217,7 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
217217

218218
mapping.addRows(0, 1, 1)
219219

220-
expect(mapping.getCellOrThrowError(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
220+
expect(mapping.getCell(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
221221
expect(mapping.getCell(adr('A2'))).toBe(undefined)
222222
expect(mapping.getSheetHeight(0)).toEqual(2)
223223
})
@@ -230,11 +230,11 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
230230

231231
mapping.addRows(0, 1, 3)
232232

233-
expect(mapping.getCellOrThrowError(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
233+
expect(mapping.getCell(adr('A1'))).toEqual(new ValueCellVertex(42, 42))
234234
expect(mapping.getCell(adr('A2'))).toBe(undefined)
235235
expect(mapping.getCell(adr('A3'))).toBe(undefined)
236236
expect(mapping.getCell(adr('A4'))).toBe(undefined)
237-
expect(mapping.getCellOrThrowError(adr('A5'))).toEqual(new ValueCellVertex(43, 43))
237+
expect(mapping.getCell(adr('A5'))).toEqual(new ValueCellVertex(43, 43))
238238
expect(mapping.getSheetHeight(0)).toEqual(5)
239239
})
240240

@@ -248,12 +248,12 @@ const sharedExamples = (builder: (width: number, height: number) => AddressMappi
248248

249249
mapping.addRows(0, 1, 1)
250250

251-
expect(mapping.getCellOrThrowError(adr('A1'))).toEqual(new ValueCellVertex(11, 11))
252-
expect(mapping.getCellOrThrowError(adr('B1'))).toEqual(new ValueCellVertex(12, 12))
251+
expect(mapping.getCell(adr('A1'))).toEqual(new ValueCellVertex(11, 11))
252+
expect(mapping.getCell(adr('B1'))).toEqual(new ValueCellVertex(12, 12))
253253
expect(mapping.getCell(adr('A2'))).toBe(undefined)
254254
expect(mapping.getCell(adr('B2'))).toBe(undefined)
255-
expect(mapping.getCellOrThrowError(adr('A3'))).toEqual(new ValueCellVertex(21, 21))
256-
expect(mapping.getCellOrThrowError(adr('B3'))).toEqual(new ValueCellVertex(22, 22))
255+
expect(mapping.getCell(adr('A3'))).toEqual(new ValueCellVertex(21, 21))
256+
expect(mapping.getCell(adr('B3'))).toEqual(new ValueCellVertex(22, 22))
257257
expect(mapping.getSheetHeight(0)).toEqual(3)
258258
})
259259

test/unit/build-engine.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import {HyperFormula} from '../../src'
32
import {plPL} from '../../src/i18n/languages'
43
import {adr} from './testUtils'

test/unit/column-index.spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('ColumnIndex#add', () => {
3535
const columnMap = index.getColumnMap(0, 1)
3636

3737
expect(columnMap.size).toBe(1)
38-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
38+
3939
expect(columnMap.get(1)!.index[0]).toBe(4)
4040
})
4141

@@ -48,7 +48,7 @@ describe('ColumnIndex#add', () => {
4848
const columnMap = index.getColumnMap(0, 0)
4949

5050
expect(columnMap.size).toBe(1)
51-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
51+
5252
expect(columnMap.get(1)!.index[0]).toBe(0)
5353
})
5454

@@ -63,7 +63,7 @@ describe('ColumnIndex#add', () => {
6363
const columnMap = index.getColumnMap(0, 0)
6464

6565
expect(columnMap.size).toBe(1)
66-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
66+
6767
expect(columnMap.get(1)!.index.length).toBe(2)
6868
})
6969

@@ -108,13 +108,11 @@ describe('ColumnIndex#add', () => {
108108

109109
const columnMap = index.getColumnMap(0, 0)
110110

111-
// eslint-disable @typescript-eslint/no-non-null-assertion
112111
expect(columnMap.get('a')!.index.length).toBe(3)
113112
expect(columnMap.get('l')!.index.length).toBe(1)
114113
expect(columnMap.get('ł')!.index.length).toBe(1)
115114
expect(columnMap.get('t')!.index.length).toBe(1)
116115
expect(columnMap.get('ŧ')!.index.length).toBe(1)
117-
// eslint-enable @typescript-eslint/no-non-null-assertion
118116
})
119117

120118
it('should ignore EmptyValue', () => {
@@ -136,7 +134,7 @@ describe('ColumnIndex change/remove', () => {
136134

137135
index.remove(1, adr('A2'))
138136

139-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
137+
140138
const valueIndex = index.getColumnMap(0, 0).get(1)!
141139
expect(valueIndex.index.length).toBe(2)
142140
expect(valueIndex.index).toContain(0)
@@ -151,7 +149,7 @@ describe('ColumnIndex change/remove', () => {
151149

152150
index.remove(undefined, adr('A2'))
153151

154-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
152+
155153
const valueIndex = index.getColumnMap(0, 0).get(1)!
156154
expect(valueIndex.index.length).toBe(3)
157155
expect(valueIndex.index).toContain(0)
@@ -166,7 +164,7 @@ describe('ColumnIndex change/remove', () => {
166164
index.change(1, 2, adr('A1'))
167165

168166
expect(index.getColumnMap(0, 0).keys()).not.toContain(1)
169-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
167+
170168
const valueIndex = index.getColumnMap(0, 0).get(2)!
171169
expect(valueIndex.index).toContain(0)
172170
})

0 commit comments

Comments
 (0)