diff --git a/core/block.ts b/core/block.ts index 9037345ae57..f226105ebfc 100644 --- a/core/block.ts +++ b/core/block.ts @@ -1309,7 +1309,7 @@ export class Block implements IASTNodeLocation, IDeletable { * @return Text of block. */ toString(opt_maxLength?: number, opt_emptyToken?: string): string { - let text = []; + const text = []; const emptyFieldPlaceholder = opt_emptyToken || '?'; // Temporarily set flag to navigate to all fields. @@ -1397,26 +1397,20 @@ export class Block implements IASTNodeLocation, IDeletable { } // Join the text array, removing spaces around added parentheses. - // AnyDuringMigration because: Type 'string' is not assignable to type - // 'any[]'. - text = text.reduce(function(acc, value) { + let combinedText: string = text.reduce(function(acc, value) { return acc + (acc.substr(-1) === '(' || value === ')' ? '' : ' ') + value; - }, '') as AnyDuringMigration; - // AnyDuringMigration because: Property 'trim' does not exist on type - // 'any[]'. - text = (text as AnyDuringMigration).trim() || '???'; + }, ''); + + combinedText = combinedText.trim() || '???'; if (opt_maxLength) { // TODO: Improve truncation so that text from this block is given // priority. E.g. "1+2+3+4+5+6+7+8+9=0" should be "...6+7+8+9=0", not // "1+2+3+4+5...". E.g. "1+2+3+4+5=6+7+8+9+0" should be "...4+5=6+7...". - if (text.length > opt_maxLength) { - // AnyDuringMigration because: Type 'string' is not assignable to type - // 'any[]'. - text = (text.substring(0, opt_maxLength - 3) + '...') as - AnyDuringMigration; + if (combinedText.length > opt_maxLength) { + combinedText = combinedText.substring(0, opt_maxLength - 3) + '...'; } } - return text; + return combinedText; } /** diff --git a/core/field_registry.ts b/core/field_registry.ts index 2cf98a32b37..31f37783a42 100644 --- a/core/field_registry.ts +++ b/core/field_registry.ts @@ -68,7 +68,7 @@ export function fromJson(options: AnyDuringMigration): Field|null { */ function fromJsonInternal(options: AnyDuringMigration): Field|null { const fieldObject = - registry.getObject(registry.Type.FIELD, options['type']) as + registry.getObject(registry.Type.FIELD, options['type']) as unknown as IRegistrableField | null; if (!fieldObject) { diff --git a/core/generator.ts b/core/generator.ts index 44d53e1e0bf..2e098331714 100644 --- a/core/generator.ts +++ b/core/generator.ts @@ -120,7 +120,7 @@ export class Generator { 'No workspace specified in workspaceToCode call. Guessing.'); workspace = common.getMainWorkspace(); } - let code = []; + const lines = []; this.init(workspace); const blocks = workspace.getTopBlocks(true); for (let i = 0, block; block = blocks[i]; i++) { @@ -144,21 +144,13 @@ export class Generator { line = line + this.injectId(this.STATEMENT_SUFFIX, block); } } - code.push(line); + lines.push(line); } } - // AnyDuringMigration because: Type 'string' is not assignable to type - // 'any[]'. - // Blank line between each section. - code = code.join('\n') as AnyDuringMigration; - // AnyDuringMigration because: Argument of type 'any[]' is not assignable - // to parameter of type 'string'. AnyDuringMigration because: Type 'string' - // is not assignable to type 'any[]'. - code = this.finish(code as AnyDuringMigration) as AnyDuringMigration; - // Final scrubbing of whitespace. - // AnyDuringMigration because: Property 'replace' does not exist on type - // 'any[]'. - code = (code as AnyDuringMigration).replace(/^\s+\n/, ''); + + let code = lines.join('\n'); + code = this.finish(code); + code = code.replace(/^\s+\n/, ''); code = code.replace(/\n\s+$/, '\n'); code = code.replace(/[ \t]+\n/g, '\n'); return code; diff --git a/core/utils/toolbox.ts b/core/utils/toolbox.ts index c3834fd8b0a..7fa14843f39 100644 --- a/core/utils/toolbox.ts +++ b/core/utils/toolbox.ts @@ -403,20 +403,18 @@ function addAttributes(node: Node, obj: AnyDuringMigration) { */ export function parseToolboxTree(toolboxDef: Element|null|string): Element| null { + let parsedToolboxDef: Element|null = null; if (toolboxDef) { - if (typeof toolboxDef !== 'string' && !(toolboxDef instanceof Element)) { - toolboxDef = null; - } if (typeof toolboxDef === 'string') { - toolboxDef = Xml.textToDom(toolboxDef); - if (toolboxDef.nodeName.toLowerCase() !== 'xml') { + parsedToolboxDef = Xml.textToDom(toolboxDef); + if (parsedToolboxDef.nodeName.toLowerCase() !== 'xml') { throw TypeError('Toolbox should be an document.'); } + } else if (toolboxDef instanceof Element) { + parsedToolboxDef = toolboxDef; } - } else { - toolboxDef = null; } - return toolboxDef; + return parsedToolboxDef; } export const TEST_ONLY = {