Skip to content
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added 13 trigonometry functions: SEC, CSC, SINH, COSH, TANH, COTH, SECH, CSCH, ACOT, ASINH, ACOSH, ATANH, ACOTH. (#485)
- Added 6 engineering functions: OCT2BIN, OCT2DEC, OCT2HEX, HEX2BIN, HEX2OCT, HEX2DEC. (#497)
- Added a configuration option to evaluate reference to an empty cells as a zero. (#476)
- Added new error type: missing licence. (#306)
- Added detailed error messages for error values. (#506)
- Added ability to handle more characters in quoted sheet names. (#509)
- Added support for escaping apostrophe character in quoted sheet names. (#64)
Expand Down
9 changes: 7 additions & 2 deletions src/Cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ export enum ErrorType {
/** Cyclic dependency. */
CYCLE = 'CYCLE',

/* Wrong address reference. */
/** Wrong address reference. */
REF = 'REF',

/* Generic error */
/** Invalid/missing licence error. */
LIC = 'LIC',

/** Generic error */
ERROR = 'ERROR'
}

export type TranslatableErrorType = Exclude<ErrorType, ErrorType.LIC>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing type for translation package was not enough? If I understood correctly ts-ignore was needed there. Do we need another type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this propagates into Config (e.g. buildErrorMapping)


export const EmptyValue = Symbol('Empty value')
export type EmptyValueType = typeof EmptyValue
export type InternalNoErrorCellValue = number | string | boolean | EmptyValueType
Expand Down
4 changes: 2 additions & 2 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {ErrorType} from './Cell'
import {ErrorType, TranslatableErrorType} from './Cell'
import {defaultParseToDateTime} from './DateTimeDefault'
import {DateTime, instanceOfSimpleDate, SimpleDate, SimpleDateTime, SimpleTime} from './DateTimeHelper'
import {
Expand Down Expand Up @@ -468,7 +468,7 @@ export class Config implements ConfigParams, ParserConfig {
*
* @internal
*/
public readonly errorMapping: Record<string, ErrorType>
public readonly errorMapping: Record<string, TranslatableErrorType>
/** @inheritDoc */
public readonly maxRows: number
/** @inheritDoc */
Expand Down
4 changes: 3 additions & 1 deletion src/Serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {simpleCellAddress, SimpleCellAddress} from './Cell'
import {CellError, ErrorType, simpleCellAddress, SimpleCellAddress} from './Cell'
import {CellValue, DetailedCellError, Exporter, NoErrorCellValue} from './CellValue'
import {Config} from './Config'
import {DependencyGraph, FormulaCellVertex, MatrixVertex, ParsingErrorVertex} from './DependencyGraph'
import {ErrorMessage} from './error-message'
import {LicenseKeyValidityState} from './helpers/licenseKeyValidator'
import {Maybe} from './Maybe'
import {buildLexerConfig, Unparser} from './parser'
import {NamedExpressions} from './NamedExpressions'
Expand Down
1 change: 1 addition & 0 deletions src/error-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ export class ErrorMessage {
public static NonZero = 'Argument cannot be 0.'
public static FunctionName = (arg: string) => `Function name ${arg} not recognized.`
public static NamedExpressionName = (arg: string) => `Named expression ${arg} not recognized.`
public static LicenseKey = (arg: string) => `License key is ${arg}.`
}
15 changes: 9 additions & 6 deletions src/i18n/TranslationPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {ErrorType} from '../Cell'
import {ErrorType, TranslatableErrorType} from '../Cell'
import {Maybe} from '../Maybe'
import {MissingTranslationError, ProtectedFunctionTranslationError} from '../errors'
import {ErrorTranslationSet, TranslationSet, UIElement, UITranslationSet} from './index'
Expand Down Expand Up @@ -43,11 +43,11 @@ export class TranslationPackage {
}, {} as Record<string, string>)
}

public buildErrorMapping(): Record<string, ErrorType> {
public buildErrorMapping(): Record<string, TranslatableErrorType> {
return Object.keys(this.errors).reduce((ret, key) => {
ret[this.errors[key as ErrorType]] = key as ErrorType
ret[this.errors[key as TranslatableErrorType]] = key as TranslatableErrorType
return ret
}, {} as Record<string, ErrorType>)
}, {} as Record<string, TranslatableErrorType>)
}

public isFunctionTranslated(key: string): boolean {
Expand Down Expand Up @@ -78,6 +78,9 @@ export class TranslationPackage {
}

public getErrorTranslation(key: ErrorType): string {
if(key === ErrorType.LIC) {
return `#${ErrorType.LIC}!`
}
const val = this.errors[key]
if (val === undefined) {
throw new MissingTranslationError(`errors.${key}`)
Expand All @@ -104,8 +107,8 @@ export class TranslationPackage {
}

private checkErrors(): void {
for (const key of Object.values(ErrorType)) {
if (!(key in this.errors)) {
for(const key of Object.values(ErrorType)){
if(! (key in this.errors) && (key !== ErrorType.LIC)){
throw new MissingTranslationError(`errors.${key}`)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {ErrorType} from '../Cell'
import {TranslatableErrorType} from '../Cell'
import {buildTranslationPackage, RawTranslationPackage, TranslationPackage} from './TranslationPackage'

export type TranslationSet = Record<string, string>
export type UITranslationSet = Record<UIElement, string>
export type ErrorTranslationSet = Record<ErrorType, string>
export type ErrorTranslationSet = Record<TranslatableErrorType, string>
export {RawTranslationPackage, TranslationPackage, buildTranslationPackage}

export enum UIElement {
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/FunctionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class FunctionRegistry {
}
}

private static functionIsProtected(functionId: string) {
public static functionIsProtected(functionId: string) {
return this._protectedPlugins.has(functionId)
}

Expand Down Expand Up @@ -224,4 +224,4 @@ export class FunctionRegistry {
this.structuralChangeFunctions.add(functionId)
}
}
}
}
4 changes: 4 additions & 0 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {ColumnSearchStrategy} from '../ColumnSearch/ColumnSearchStrategy'
import {Config} from '../Config'
import {DateTimeHelper} from '../DateTimeHelper'
import {DependencyGraph} from '../DependencyGraph'
import {LicenseKeyValidityState} from '../helpers/licenseKeyValidator'
import {ErrorMessage} from '../error-message'
import {Matrix, NotComputedMatrix} from '../Matrix'
import {Maybe} from '../Maybe'
Expand Down Expand Up @@ -194,6 +195,9 @@ export class Interpreter {
}
}
case AstNodeType.FUNCTION_CALL: {
if(this.config.licenseKeyValidityState !== LicenseKeyValidityState.VALID && !FunctionRegistry.functionIsProtected(ast.procedureName)) {
return new CellError(ErrorType.LIC, ErrorMessage.LicenseKey(this.config.licenseKeyValidityState))
}
const pluginEntry = this.functionRegistry.getFunction(ast.procedureName)
if (pluginEntry && this.config.translationPackage.isFunctionTranslated(ast.procedureName)) {
const [pluginFunction, pluginInstance] = pluginEntry as [string, any]
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ParserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {ErrorType} from '../Cell'
import {ErrorType, TranslatableErrorType} from '../Cell'
import {TranslationPackage} from '../i18n'

export interface ParserConfig {
functionArgSeparator: string,
decimalSeparator: '.' | ',',
translationPackage: TranslationPackage,
errorMapping: Record<string, ErrorType>,
errorMapping: Record<string, TranslatableErrorType>,
maxColumns: number,
maxRows: number,
}
19 changes: 19 additions & 0 deletions test/licence.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {ErrorType, HyperFormula} from '../src'
import {CellError} from '../src/Cell'
import {ErrorMessage} from '../src/error-message'
import {adr, detailedError} from './testUtils'

describe( 'Wrong licence', () => {
it('eval', () => {
const engine = HyperFormula.buildFromArray([['=TRUE()', null, 1, '=A(']], {licenseKey: ''})
expect(engine.getCellValue(adr('A1'))).toEqual(detailedError(ErrorType.LIC, ErrorMessage.LicenseKey('missing')))
expect(engine.getCellValue(adr('B1'))).toEqual(null)
expect(engine.getCellValue(adr('C1'))).toEqual(1)
expect(engine.getCellValue(adr('D1'))).toEqual(detailedError(ErrorType.ERROR, ErrorMessage.ParseError))
})

it('serialization', () => {
const engine = HyperFormula.buildFromArray([['=TRUE()', null, 1, '=A(']], {licenseKey: ''})
expect(engine.getSheetSerialized(0)).toEqual([['=TRUE()', null, 1, '=A(']])
})
})
7 changes: 7 additions & 0 deletions test/parser/error-literals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ describe('Parsing error literals', () => {
expect(ast.error.type).toEqual(ErrorType.VALUE)
})

it('should not parse #LIC! as license error', () => {
const parser = buildEmptyParserWithCaching(new Config())
const ast = parser.parse('=#LIC!', simpleCellAddress(0, 0, 0)).ast as ErrorAst
expect(ast.type).toBe(AstNodeType.ERROR)
expect(ast.error.type).toEqual(ErrorType.ERROR)
})

it('should parse error literals with ?', () => {
const parser = buildEmptyParserWithCaching(new Config())
const ast = parser.parse('=#NAME?', simpleCellAddress(0, 0, 0)).ast as ErrorAst
Expand Down