diff --git a/CHANGELOG.md b/CHANGELOG.md index 40a3cbec63..e0580fd8b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/Cell.ts b/src/Cell.ts index 4fb697d753..f47cd984be 100644 --- a/src/Cell.ts +++ b/src/Cell.ts @@ -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 + export const EmptyValue = Symbol('Empty value') export type EmptyValueType = typeof EmptyValue export type InternalNoErrorCellValue = number | string | boolean | EmptyValueType diff --git a/src/Config.ts b/src/Config.ts index 0c74d3936a..352de4d625 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -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 { @@ -468,7 +468,7 @@ export class Config implements ConfigParams, ParserConfig { * * @internal */ - public readonly errorMapping: Record + public readonly errorMapping: Record /** @inheritDoc */ public readonly maxRows: number /** @inheritDoc */ diff --git a/src/Serialization.ts b/src/Serialization.ts index 8416e7625e..94868b7f67 100644 --- a/src/Serialization.ts +++ b/src/Serialization.ts @@ -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' diff --git a/src/error-message.ts b/src/error-message.ts index 2fbe1c40f5..c4c38ffd37 100644 --- a/src/error-message.ts +++ b/src/error-message.ts @@ -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}.` } diff --git a/src/i18n/TranslationPackage.ts b/src/i18n/TranslationPackage.ts index fbd02b84f6..d86ad76de9 100644 --- a/src/i18n/TranslationPackage.ts +++ b/src/i18n/TranslationPackage.ts @@ -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' @@ -43,11 +43,11 @@ export class TranslationPackage { }, {} as Record) } - public buildErrorMapping(): Record { + public buildErrorMapping(): Record { 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) + }, {} as Record) } public isFunctionTranslated(key: string): boolean { @@ -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}`) @@ -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}`) } } diff --git a/src/i18n/index.ts b/src/i18n/index.ts index a4b6ce0bb0..075495e0f2 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -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 export type UITranslationSet = Record -export type ErrorTranslationSet = Record +export type ErrorTranslationSet = Record export {RawTranslationPackage, TranslationPackage, buildTranslationPackage} export enum UIElement { diff --git a/src/interpreter/FunctionRegistry.ts b/src/interpreter/FunctionRegistry.ts index 34a5123f50..77a6e7a328 100644 --- a/src/interpreter/FunctionRegistry.ts +++ b/src/interpreter/FunctionRegistry.ts @@ -116,7 +116,7 @@ export class FunctionRegistry { } } - private static functionIsProtected(functionId: string) { + public static functionIsProtected(functionId: string) { return this._protectedPlugins.has(functionId) } @@ -224,4 +224,4 @@ export class FunctionRegistry { this.structuralChangeFunctions.add(functionId) } } -} \ No newline at end of file +} diff --git a/src/interpreter/Interpreter.ts b/src/interpreter/Interpreter.ts index 5a866355f7..1667de853b 100644 --- a/src/interpreter/Interpreter.ts +++ b/src/interpreter/Interpreter.ts @@ -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' @@ -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] diff --git a/src/parser/ParserConfig.ts b/src/parser/ParserConfig.ts index 489ec24e48..44a0d592c0 100644 --- a/src/parser/ParserConfig.ts +++ b/src/parser/ParserConfig.ts @@ -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, + errorMapping: Record, maxColumns: number, maxRows: number, } diff --git a/test/licence.spec.ts b/test/licence.spec.ts new file mode 100644 index 0000000000..c34ff1ebd8 --- /dev/null +++ b/test/licence.spec.ts @@ -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(']]) + }) +}) diff --git a/test/parser/error-literals.spec.ts b/test/parser/error-literals.spec.ts index 403ccded91..d2eadadd53 100644 --- a/test/parser/error-literals.spec.ts +++ b/test/parser/error-literals.spec.ts @@ -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