Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TS errors on dependent projects with certain tsconfig settings (#6360) #6361

Merged
merged 2 commits into from
Sep 6, 2022
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
34 changes: 14 additions & 20 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ export class Block implements IASTNodeLocation, IDeletable {
* @returns Text of block.
*/
toString(opt_maxLength?: number, opt_emptyToken?: string): string {
let text = [];
const tokens = [];
const emptyFieldPlaceholder = opt_emptyToken || '?';

// Temporarily set flag to navigate to all fields.
Expand Down Expand Up @@ -1410,16 +1410,16 @@ export class Block implements IASTNodeLocation, IDeletable {
case ASTNode.types.INPUT: {
const connection = node.getLocation() as Connection;
if (!node.in()) {
text.push(emptyFieldPlaceholder);
tokens.push(emptyFieldPlaceholder);
} else if (shouldAddParentheses(connection)) {
text.push('(');
tokens.push('(');
}
break;
}
case ASTNode.types.FIELD: {
const field = node.getLocation() as Field;
if (field.name !== constants.COLLAPSED_FIELD_NAME) {
text.push(field.getText());
tokens.push(field.getText());
}
break;
}
Expand All @@ -1437,7 +1437,7 @@ export class Block implements IASTNodeLocation, IDeletable {
// If we hit an input on the way up, possibly close out parentheses.
if (node && node.getType() === ASTNode.types.INPUT &&
shouldAddParentheses(node.getLocation() as Connection)) {
text.push(')');
tokens.push(')');
}
}
if (node) {
Expand All @@ -1452,31 +1452,25 @@ export class Block implements IASTNodeLocation, IDeletable {
// Run through our text array and simplify expression to remove parentheses
// around single field blocks.
// E.g. ['repeat', '(', '10', ')', 'times', 'do', '?']
for (let i = 2; i < text.length; i++) {
if (text[i - 2] === '(' && text[i] === ')') {
text[i - 2] = text[i - 1];
text.splice(i - 1, 2);
for (let i = 2; i < tokens.length; i++) {
if (tokens[i - 2] === '(' && tokens[i] === ')') {
tokens[i - 2] = tokens[i - 1];
tokens.splice(i - 1, 2);
}
}

// 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 text: string = tokens.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() || '???';
}, '');

text = text.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;
text = text.substring(0, opt_maxLength - 3) + '...';
}
}
return text;
Expand Down
10 changes: 5 additions & 5 deletions core/field_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ export function fromJson(options: AnyDuringMigration): Field|null {
* @param options
*/
function fromJsonInternal(options: AnyDuringMigration): Field|null {
const fieldObject =
registry.getObject(registry.Type.FIELD, options['type']) as
IRegistrableField |
null;
const fieldObject = registry.getObject(registry.Type.FIELD, options['type']);
if (!fieldObject) {
console.warn(
'Blockly could not create a field of type ' + options['type'] +
'. The field is probably not being registered. This could be because' +
' the file is not loaded, the field does not register itself (Issue' +
' #1584), or the registration is not being reached.');
return null;
} else if (typeof (fieldObject as any)['fromJson'] !== 'function') {
throw new TypeError('returned Field was not a IRegistrableField');
} else {
return (fieldObject as unknown as IRegistrableField).fromJson(options);
}
return fieldObject.fromJson(options);
}

export const TEST_ONLY = {
Expand Down
14 changes: 6 additions & 8 deletions core/utils/toolbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,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 <xml> document.');
}
} else if (toolboxDef instanceof Element) {
parsedToolboxDef = toolboxDef;
}
} else {
toolboxDef = null;
}
return toolboxDef;
return parsedToolboxDef;
}

export const TEST_ONLY = {
Expand Down