Skip to content
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
98f8d62
registering languages
izulin Mar 20, 2020
f1d0779
linter
izulin Mar 20, 2020
39878ef
changing config
izulin Mar 20, 2020
714c54e
Import HyperFormula from index
voodoo11 Mar 20, 2020
6384127
Merge branch 'develop' into pu/issue-36
izulin Mar 20, 2020
7225021
linter, small fix
izulin Mar 20, 2020
a7de553
getSheetPrefix
izulin Mar 20, 2020
aaeee08
simpler
izulin Mar 22, 2020
6aaccd1
refactor
izulin Mar 23, 2020
be008c8
simplifying the code
izulin Mar 23, 2020
daa2b48
.
izulin Mar 23, 2020
3b223ea
Merge branch 'develop' into pu/issue-36
izulin Mar 24, 2020
b4bb05f
.
izulin Mar 25, 2020
54a5db7
Merge branch 'develop' into pu/issue-36
izulin Mar 26, 2020
0fa35dc
Merge branch 'develop' into pu/issue-36
izulin Mar 27, 2020
81c6e09
Merge branch 'develop' into pu/issue-36
izulin Mar 27, 2020
b738e05
Update src/Config.ts
izulin Mar 27, 2020
4cd791e
fallback on this test
izulin Mar 27, 2020
333506c
Merge branch 'pu/issue-36' of https://github.com/handsontable/hyperfo…
izulin Mar 27, 2020
85dac1d
packaging translationpackages into class
izulin Mar 27, 2020
7f6406e
hermetization
izulin Mar 27, 2020
7661eb1
hermetization
izulin Mar 27, 2020
971836e
separate file
izulin Mar 27, 2020
233252a
.
izulin Mar 27, 2020
ab59d18
Merge branch 'develop' into pu/issue-36
izulin Mar 30, 2020
3f15681
methods
izulin Mar 31, 2020
ac896f9
docs
izulin Mar 31, 2020
d171099
registering/deregistering languages
izulin Mar 31, 2020
e36307e
linter
izulin Mar 31, 2020
1be0607
fix
izulin Mar 31, 2020
4f98beb
refactor
izulin Mar 31, 2020
71b4b2f
unregister
izulin Mar 31, 2020
2d48d1a
linter
izulin Mar 31, 2020
f0f7209
refactor
izulin Mar 31, 2020
dbd00f0
cleanup
izulin Apr 1, 2020
30ec7cb
linter
izulin Apr 1, 2020
c92543f
nesting
izulin Apr 1, 2020
378911a
linter
izulin Apr 1, 2020
3c139a9
checking translation
izulin Apr 1, 2020
a191810
sanitization
izulin Apr 1, 2020
f21eb53
linter
izulin Apr 1, 2020
4b6e209
Merge branch 'develop' into pu/issue-36
izulin Apr 6, 2020
0381289
throwing errors
izulin Apr 6, 2020
3321f89
meaningful errors
izulin Apr 6, 2020
355ea84
smarter reload in config
izulin Apr 6, 2020
f9897de
.
izulin Apr 6, 2020
754efff
Fix imports
voodoo11 Apr 6, 2020
7df5981
config not exported
izulin Apr 6, 2020
edb4486
cleanup
izulin Apr 6, 2020
4c15583
errors
izulin Apr 6, 2020
f910846
desc
izulin Apr 6, 2020
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
4 changes: 2 additions & 2 deletions src/BuildEngineFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {LazilyTransformingAstService} from './'
import {LazilyTransformingAstService} from './LazilyTransformingAstService'
import {CellContentParser} from './CellContentParser'
import {Exporter} from './CellValue'
import {buildColumnSearchStrategy, ColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
Expand Down Expand Up @@ -102,7 +102,7 @@ export class BuildEngineFactory {

public static buildFromSheet(sheet: Sheet, configInput?: Partial<ConfigParams>): EngineState {
const config = new Config(configInput)
const newsheetprefix = config.translationPackage.getUITranslation(UIElement.NEW_SHEET_PREFIX)! + '1'
const newsheetprefix = config.translationPackage.getUITranslation(UIElement.NEW_SHEET_PREFIX) + '1'
return this.buildEngine(config, {[newsheetprefix]: sheet})
}

Expand Down
2 changes: 1 addition & 1 deletion src/CellValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class Exporter {
}

private detailedError(error: CellError): DetailedCellError {
return new DetailedCellError(error, this.config.getErrorTranslationFor(error.type))
return new DetailedCellError(error, this.config.translationPackage.getErrorTranslation(error.type))
}

private cellValueRounding(value: number): number {
Expand Down
24 changes: 4 additions & 20 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ErrorType} from './Cell'
import {defaultParseToDate, instanceOfSimpleDate, SimpleDate} from './DateHelper'
import {ExpectedOneOfValues, ExpectedValueOfType} from './errors'
import {AlwaysDense, ChooseAddressMapping} from './DependencyGraph/AddressMapping/ChooseAddressMappingPolicy'
import {HyperFormula} from './index'
import {HyperFormula} from './HyperFormula'
import {defaultPrintDate} from './format/format'
import {TranslationPackage} from './i18n'
import {AbsPlugin} from './interpreter/plugin/AbsPlugin'
Expand Down Expand Up @@ -526,22 +526,6 @@ export class Config implements ConfigParams, ParserConfig {
return new Config(mergedConfig)
}

public getFunctionTranslationFor = (functionTranslationKey: string): string => {
const translation = this.translationPackage.getFunctionTranslation(functionTranslationKey)
if(translation === undefined) {
throw new Error('No translation for function.')
}
return translation
}

public getErrorTranslationFor = (functionTranslationKey: ErrorType): string => {
const translation = this.translationPackage.getErrorTranslation(functionTranslationKey)
if(translation === undefined) {
throw new Error('No translation for error.')
}
return translation
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public allFunctionPlugins(): any[] {
return [...Config.defaultPlugins, ...this.functionPlugins]
Expand All @@ -554,7 +538,7 @@ export class Config implements ConfigParams, ParserConfig {
for (const functionKey in plugin.implementedFunctions) {
const pluginFunctionData = plugin.implementedFunctions[functionKey]
if (pluginFunctionData.isVolatile) {
volatileFunctions.add(this.getFunctionTranslationFor(pluginFunctionData.translationKey))
volatileFunctions.add(this.translationPackage.getFunctionTranslation(pluginFunctionData.translationKey))
}
}
}
Expand All @@ -569,7 +553,7 @@ export class Config implements ConfigParams, ParserConfig {
for (const functionKey in plugin.implementedFunctions) {
const pluginFunctionData = plugin.implementedFunctions[functionKey]
if (pluginFunctionData.isDependentOnSheetStructureChange) {
structuralChangeFunctions.add(this.getFunctionTranslationFor(pluginFunctionData.translationKey))
structuralChangeFunctions.add(this.translationPackage.getFunctionTranslation(pluginFunctionData.translationKey))
}
}
}
Expand All @@ -583,7 +567,7 @@ export class Config implements ConfigParams, ParserConfig {
for (const functionKey in plugin.implementedFunctions) {
const pluginFunctionData = plugin.implementedFunctions[functionKey]
if (pluginFunctionData.doesNotNeedArgumentsToBeComputed) {
functionsWhichDoesNotNeedArgumentsToBeComputed.add(this.getFunctionTranslationFor(pluginFunctionData.translationKey))
functionsWhichDoesNotNeedArgumentsToBeComputed.add(this.translationPackage.getFunctionTranslation(pluginFunctionData.translationKey))
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/DependencyGraph/SheetMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export class SheetMapping {
private lastSheetId = -1

constructor(private languages: TranslationPackage) {
const sheetNamePrefix = languages.getUITranslation(UIElement.NEW_SHEET_PREFIX)
if(sheetNamePrefix===undefined) {
throw new Error('No translation for sheet prefix.')
}
this.sheetNamePrefix = sheetNamePrefix
this.sheetNamePrefix = languages.getUITranslation(UIElement.NEW_SHEET_PREFIX)
}

public addSheet(newSheetDisplayName: string = `${this.sheetNamePrefix}${this.lastSheetId + 2}`): number {
Expand Down
7 changes: 2 additions & 5 deletions src/HyperFormula.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AbsoluteCellRange} from './AbsoluteCellRange'
import { AbsoluteCellRange } from './AbsoluteCellRange'
import {CellType, CellValueType, getCellType, getCellValueType, NoErrorCellValue, SimpleCellAddress} from './Cell'
import {CellContent, CellContentParser, RawCellContent} from './CellContentParser'
import {CellValue, ExportedChange, Exporter} from './CellValue'
Expand Down Expand Up @@ -226,10 +226,6 @@ export class HyperFormula implements TypedEmitter {
}
}

public static unregisterAllLanguages(): void {
this.registeredLanguages = new Map()
}

/**
* Returns all registered languages codes.
*/
Expand Down Expand Up @@ -1657,3 +1653,4 @@ export class HyperFormula implements TypedEmitter {
}
}
}

2 changes: 1 addition & 1 deletion src/Serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Serialization {
} else {
const value: CellValue = this.getCellValue(address)
if(value instanceof DetailedCellError) {
return this.config.getErrorTranslationFor(value.error.type)
return this.config.translationPackage.getErrorTranslation(value.error.type)
} else {
return value
}
Expand Down
44 changes: 31 additions & 13 deletions src/i18n/TranslationPackage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ErrorType} from '../Cell'
import {Maybe} from '../Maybe'
import {ErrorTranslationSet, TranslationSet, UIElement, UITranslationSet} from './index'

export interface RawTranslationPackage {
Expand Down Expand Up @@ -36,27 +35,46 @@ export class TranslationPackage {
}, {} as Record<string, ErrorType>)
}

public getFunctionTranslation(key: string): Maybe<string> {
return this.functions[key]
public isFunctionTranslated(key: string): boolean {
return this.functions[key] !== undefined
}
public getErrorTranslation(key: ErrorType): Maybe<string> {
return this.errors[key]

public getFunctionTranslation(key: string): string {
const val = this.functions[key]
if(val === undefined) {
throw new Error(`No translation for function ${key}.`)

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.

It would be cool if for all five Error messages in this file we could introduce a new type:

export class MissingTranslationError extends Error {
  constructor(key: string) {
    super(`Translation for ${key} is missing in the translation package you're using.`)
  }
}

Usage:

throw new MissingTranslationError(`functions.${key}`)

Then we can describe them correctly within public API TSDoc. Thanks

} else {
return val
}
}
public getUITranslation(key: UIElement): Maybe<string> {
return this.ui[key]
public getErrorTranslation(key: ErrorType): string {
const val = this.errors[key]
if(val === undefined) {
throw new Error(`No translation for error ${key}.`)
} else {
return val
}
}
public getUITranslation(key: UIElement): string {
const val = this.ui[key]
if(val === undefined) {
throw new Error(`No translation for ui ${key}.`)
} else {
return val
}
}

private checkUI(): void {
for(const err of Object.values(UIElement)){
if(! (err in this.ui)){
throw new Error('No translation for error.')
for(const iter of Object.values(UIElement)){
if(! (iter in this.ui)){
throw new Error(`Translation for ui ${iter} is required.`)
}
}
}
private checkErrors(): void {
for(const err of Object.values(ErrorType)){
if(! (err in this.errors)){
throw new Error('No translation for error.')
for(const iter of Object.values(ErrorType)){
if(! (iter in this.errors)){
throw new Error(`Translation for error ${iter} is required.`)
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CellError, EmptyValue } from './Cell'
import { CellError, EmptyValue, ErrorType } from './Cell'
import { CellValue, DetailedCellError, ExportedCellChange, ExportedNamedExpressionChange } from './CellValue'
import {
InvalidAddressError,
Expand All @@ -10,7 +10,8 @@ import {
} from './errors'
import {Sheets} from './GraphBuilder'
import {HyperFormula} from './HyperFormula'
import {enGB} from './i18n'
import {Config} from './Config'
import {languages} from './i18n'
import {LazilyTransformingAstService} from './LazilyTransformingAstService'

class HyperFormulaNS extends HyperFormula {
Expand All @@ -26,7 +27,8 @@ class HyperFormulaNS extends HyperFormula {
public static EvaluationSuspendedError = EvaluationSuspendedError
}

HyperFormula.registerLanguage('enGB', enGB)
const defaultLanguage = Config.defaultConfig.language
HyperFormula.registerLanguage(defaultLanguage, languages[defaultLanguage])

export default HyperFormulaNS

Expand All @@ -46,4 +48,5 @@ export {
ExportedNamedExpressionChange,
NoOperationToUndo,
EvaluationSuspendedError,
ErrorType

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.

Any new export should be added to HyperFormulaNS as well.

}
3 changes: 1 addition & 2 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ export class Interpreter {
}
case AstNodeType.FUNCTION_CALL: {
const pluginEntry = this.pluginCache.get(ast.procedureName)
const procedureName = this.config.translationPackage.getFunctionTranslation(ast.procedureName)
if (pluginEntry && procedureName!==undefined) {
if (pluginEntry && this.config.translationPackage.isFunctionTranslated(ast.procedureName)) {
const [pluginInstance, pluginFunction] = pluginEntry
return pluginInstance[pluginFunction](ast, formulaAddress)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/LexerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface ILexerConfig {
}

export const buildLexerConfig = (config: ParserConfig): ILexerConfig => {
const offsetProcedureNameLiteral = config.getFunctionTranslationFor('OFFSET')
const offsetProcedureNameLiteral = config.translationPackage.getFunctionTranslation('OFFSET')
const errorMapping = config.errorMapping
const functionMapping = config.translationPackage.buildFunctionMapping()

Expand Down
2 changes: 0 additions & 2 deletions src/parser/ParserConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ export interface ParserConfig {
volatileFunctions(): Set<string>,
structuralChangeFunctions(): Set<string>,
functionsWhichDoesNotNeedArgumentsToBeComputed(): Set<string>,
getErrorTranslationFor(errorType: ErrorType): string,
getFunctionTranslationFor(functionName: string): string,
}
2 changes: 1 addition & 1 deletion src/parser/ParserWithCaching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class ParserWithCaching {
return this.computeHashOfAstNode(ast.value) + imageWithWhitespace('%', ast.leadingWhitespace)
}
case AstNodeType.ERROR: {
const image = this.config.getErrorTranslationFor(
const image = this.config.translationPackage.getErrorTranslation(
ast.error ? ast.error.type : ErrorType.ERROR
)
return imageWithWhitespace(image, ast.leadingWhitespace)
Expand Down
6 changes: 4 additions & 2 deletions src/parser/Unparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class Unparser {
}
case AstNodeType.FUNCTION_CALL: {
const args = ast.args.map((arg) => this.unparseAst(arg, address)).join(this.config.functionArgSeparator)
const procedureName = this.config.translationPackage.getFunctionTranslation(ast.procedureName) ?? ast.procedureName
const procedureName = this.config.translationPackage.isFunctionTranslated(ast.procedureName) ?
this.config.translationPackage.getFunctionTranslation(ast.procedureName) :
ast.procedureName
const rightPart = procedureName + '(' + args + imageWithWhitespace(')', ast.internalWhitespace)
return imageWithWhitespace(rightPart, ast.leadingWhitespace)
}
Expand Down Expand Up @@ -66,7 +68,7 @@ export class Unparser {
return this.unparseAst(ast.value, address) + imageWithWhitespace('%', ast.leadingWhitespace)
}
case AstNodeType.ERROR: {
const image = this.config.getErrorTranslationFor(
const image = this.config.translationPackage.getErrorTranslation(
ast.error ? ast.error.type : ErrorType.ERROR
)
return imageWithWhitespace(image, ast.leadingWhitespace)
Expand Down
6 changes: 3 additions & 3 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {EmptyValue, HyperFormula} from '../src'
import {Config} from '../src/Config'
import {enGB, plPL} from '../src/i18n'
import {unregisterAllLanguages} from './testUtils'

describe('Config', () => {
beforeEach(() => {
HyperFormula.unregisterAllLanguages()
unregisterAllLanguages()
HyperFormula.registerLanguage('plPL', plPL)
HyperFormula.registerLanguage('enGB', enGB)
})

it('works', () => {
Expand All @@ -30,7 +30,7 @@ describe('Config', () => {
it('can translate functions', () => {
const config = new Config({ language: 'plPL' })

expect(config.getFunctionTranslationFor('SUM')).toEqual('SUMA')
expect(config.translationPackage.getFunctionTranslation('SUM')).toEqual('SUMA')
})

it( 'validation: boolean params', () => {
Expand Down
5 changes: 4 additions & 1 deletion test/extending-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {HyperFormula} from '../src'
import {ErrorType} from '../src/Cell'
import {enGB} from '../src/i18n'
import {ErrorType} from '../src'
import {FunctionPlugin, PluginFunctionType} from '../src/interpreter/plugin/FunctionPlugin'
import './testConfig.ts'
import {adr, detailedError} from './testUtils'
Expand All @@ -24,6 +25,8 @@ describe('Plugins', () => {
], {functionPlugins: [FooPlugin]})

expect(engine.getCellValue(adr('A1'))).toBe(42)
HyperFormula.unregisterLanguage('enGB')
HyperFormula.registerLanguage('enGB', enGB)
})

it('cleanup - broken', () => { //TODO: loading plugin automatically adds translation to plugin cache
Expand Down
1 change: 1 addition & 0 deletions test/i18n.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {HyperFormula} from '../src'
import {ErrorType} from '../src/Cell'
import {Config} from '../src/Config'
import {languages, plPL, TranslationPackage} from '../src/i18n'
import {CellAddress} from '../src/parser'
Expand Down
3 changes: 1 addition & 2 deletions test/interpreter/scalar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {CellError, ErrorType} from '../../src/Cell'
import {CellError, ErrorType} from '../../src'
import {Config} from '../../src/Config'
import {DateHelper} from '../../src/DateHelper'
import {ArithmeticHelper, max, min} from '../../src/interpreter/ArithmeticHelper'
import '../testConfig'
import {NumberLiteralHelper} from '../../src/NumberLiteralHelper'

describe('nonstrictadd', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/parser/compute-hash-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import {Config} from '../../src/Config'
import {SheetMapping} from '../../src/DependencyGraph'
import {buildTranslationPackage, enGB, plPL} from '../../src/i18n'
import {buildLexerConfig, FormulaLexer, ParserWithCaching} from '../../src/parser'
import {adr} from '../testUtils'
import {adr, unregisterAllLanguages} from '../testUtils'

describe('Compute hash from ast', () => {
beforeEach(() => {
HyperFormula.unregisterAllLanguages()
unregisterAllLanguages()
HyperFormula.registerLanguage('plPL', plPL)
HyperFormula.registerLanguage('enGB', enGB)
})

const config = new Config()
Expand Down
4 changes: 2 additions & 2 deletions test/parser/compute-hash-from-tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Config} from '../../src/Config'
import {SheetMapping} from '../../src/DependencyGraph'
import {enGB, plPL} from '../../src/i18n'
import {buildLexerConfig, FormulaLexer, ParserWithCaching} from '../../src/parser'
import {unregisterAllLanguages} from '../testUtils'

describe('computeHashFromTokens', () => {
const computeFunc = (code: string, address: SimpleCellAddress, language: string = 'enGB'): string => {
Expand All @@ -16,9 +17,8 @@ describe('computeHashFromTokens', () => {
return parser.computeHashFromTokens(tokens, address)
}
beforeEach(() => {
HyperFormula.unregisterAllLanguages()
unregisterAllLanguages()
HyperFormula.registerLanguage('plPL', plPL)
HyperFormula.registerLanguage('enGB', enGB)
})

it('simple case', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/parser/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ import {
RowRangeAst
} from '../../src/parser/Ast'
import {ColumnAddress} from '../../src/parser/ColumnAddress'
import {adr} from '../testUtils'
import {adr, unregisterAllLanguages} from '../testUtils'
import {RowAddress} from '../../src/parser/RowAddress'

describe('ParserWithCaching', () => {
beforeEach(() => {
HyperFormula.unregisterAllLanguages()
unregisterAllLanguages()
HyperFormula.registerLanguage('plPL', plPL)
HyperFormula.registerLanguage('enGB', enGB)
})

it('integer literal', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/parser/range-offset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {simpleCellAddress} from '../../src/Cell'
import {Config} from '../../src/Config'
import {SheetMapping} from '../../src/DependencyGraph'
import {buildTranslationPackage, enGB} from '../../src/i18n'
import {AstNodeType, CellRangeAst, ParserWithCaching, ParsingErrorType} from '../../src/parser'
import {RangeSheetReferenceType} from '../../src/parser/Ast'
import {Config} from '../../src/Config'

describe('Parser - range offset', () => {
it('OFFSET - usage with range', () => {
Expand Down
Loading