Skip to content

[Google Blockly] use 'unnamed' for functions without names#55320

Merged
mikeharv merged 4 commits into
staging-nextfrom
mike/fix-unnamed-functions-bug
Dec 7, 2023
Merged

[Google Blockly] use 'unnamed' for functions without names#55320
mikeharv merged 4 commits into
staging-nextfrom
mike/fix-unnamed-functions-bug

Conversation

@mikeharv

@mikeharv mikeharv commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Fixes CDO Blockly projects that include function call blocks associated with functions with blank ('') names.

Example CDO Project:

image

Google Blockly Before:
image (168)

Google Blockly After:

image

Explanation

This bug potentially affects Dance projects that:

  • Were last saved before the lab was migrated to mainline Google Blockly (2022 and earlier)
  • Include a function definition block named with an empty string
  • Include at least one call block pointing to the above function

CDO Blockly supports unnamed functions:

image (169)

Google Blockly fixed unnamed functions when they are renamed by forcing a name:
image (170)

If we detect that there is a function without a name, we can give the name 'unnamed' which will prevent the crash. For definition blocks, this happens in the NAME field. For call blocks, this happens in the mutator.

Links

See # 1 here: https://docs.google.com/document/d/1nCJIB2UHnHt-uPjyHBR9AZxxQbaeHdz5aeBchyRlZMI/edit

Testing story

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

@mikeharv mikeharv requested a review from a team December 6, 2023 17:13
Comment thread apps/src/blockly/addons/cdoXml.js Outdated
}

if (fieldElement.textContent === '') {
fieldElement.textContent = 'unnamed';

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.

do we have to worry about translations or does blockly always use 'unnamed'?

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.

I tested this and it seems like we use 'unnamed' for every language.

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.

This matches our current behavior for students who rename existing functions. Did we already have a task to localize the default "do something" name as well?

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.

yep do something was translated here: #55100

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.

I see! I was still seeing English when I switched languages but that must just be because the translations haven't been added yet.

@molly-moen molly-moen left a comment

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.

Looks good, couple recommendations for constants

Comment thread apps/src/blockly/addons/cdoXml.js Outdated
// Place mutator before fields, values, and other nested blocks.
blockElement.insertBefore(mutationElement, blockElement.firstChild);
if (!mutationElement.getAttribute('name')) {
mutationElement.setAttribute('name', 'unnamed');

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.

nit: add a constant for 'unnamed'

Comment thread apps/src/blockly/addons/cdoXml.js Outdated
*/
export function addNameToBlockFunctionCallBlock(blockElement) {
const blockType = blockElement.getAttribute('type');
if (blockType !== 'procedures_callnoreturn') {

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.

nit: use BLOCK_TYPES.procedureCall constant here

Comment thread apps/src/blockly/addons/cdoXml.js Outdated
*/
export function addNameToBlockFunctionDefinitionBlock(blockElement) {
const blockType = blockElement.getAttribute('type');
if (blockType !== 'procedures_defnoreturn') {

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.

use block type constant here

@mikeharv mikeharv requested a review from a team as a code owner December 6, 2023 18:05
Comment thread apps/i18n/common/en_us.json Outdated
"unhideAndAssignHeader": "This unit is currently hidden from the section",
"unhideUnitAndAssign": "Unhide unit and assign",
"unnamedFunction": "You have a variable or function that does not have a name. Don't forget to give everything a descriptive name.",
"unnamedFunctionName": "unnamed",

@molly-moen molly-moen Dec 6, 2023

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.

does Blockly set 'unnamed' as the name if it's blank (no matter the language)? If they do we may not want a translation here

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.

Updated to use Blockly.Msg so it should be consistent with how Blockly sets the name, no matter the language.

@mikeharv mikeharv merged commit de022e2 into staging-next Dec 7, 2023
@mikeharv mikeharv deleted the mike/fix-unnamed-functions-bug branch December 7, 2023 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants