Skip to content

refactor(generators): Restructure generator modules to contain side effects - #7173

Merged
cpcallen merged 9 commits into
RaspberryPiFoundation:developfrom
cpcallen:refactor/7086/restructure
Jun 20, 2023
Merged

refactor(generators): Restructure generator modules to contain side effects#7173
cpcallen merged 9 commits into
RaspberryPiFoundation:developfrom
cpcallen:refactor/7086/restructure

Conversation

@cpcallen

Copy link
Copy Markdown
Collaborator

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide
  • I ran npm run format and npm run lint

The details

Resolves

Fixes#7086

Proposed Changes

Implements proposal 2 (option 2A) of #7086. In particular:

  1. generators/<lang>.js are moved to generators/<lang>/<lang>_generator.js. Code creating <lang>Generator instances is removed from these file.

  2. generators/<lang>/*.js (excluding <lang>_generator.js) are modified to declare and individually export per-block generator functions, not referencing any particular CodeGenerator instance. Calls to <lang>Generator.addReservedWords() re replaced with comments noting the reserved words needed by the generator functions in that file.

  3. generators/<lang>/all.js are moved to generators/<lang>.js and modified to:

    • Reexport everything from generators/<lang>/<lang>_generator.js.
    • Create and export a <Lang>Generator instance named <lang>Generator.
    • Call <langGenerator.addReservedWords() to add reserved words mentioned in the block generator files (if any).
    • Install all of the block generator functions from generators/<lang>/*.js on <lang>Generator.forBlock.

    These file remain the entrypoint for the <lang>_compressed.js chunks.

Additionally, this PR:

  • Updates the chunks declaration in build_tasks.js as accordingly.
  • Updates scripts/migration/renamings.json5 to record these updates plus update references from Blockly.<Lang> to <lang>.<lang>Generator (only applicable, and only effective, on code that imports generators as scripts.
  • Fix two missing export statements for some class <Lang>Generators.
  • Fix some tests which imported generators improperly.

Behaviour Before Change

Generators work.

Behaviour After Change

Unchanged.

Reason for Changes

See #7086.

Test Coverage

Passes npm test. No changes to manual test procedures expected.

Documentation

No documentation updates needed. This PR is an internal-only refactor.

Additional Information

This change paves the way for better tree-shaking of generator functions, though the individual generator functions are not yet exposed in in the compiled chunks. Projects that compile Blockly from source (such as Blockly Games) should be able to take advantage of this change immediately.

Notes for Reviewer

Because of the file moves, it will probably be easier to review this PR commit-by-commit.

Move the LangGenerator definitions into their respective
subdirectories and add a _generator suffix to their filenames,
i.e. generators/javascript.js becomes
generators/javascript/javascript_generator.js.
This is to keep related code together and allow the `lang/all.js`
entrypoints to be moved to the top level generators/ directory.
No goog module IDs were changed, so playground and test code
that accesses this modules by filename does not need to be modified.
- Move the entrypoints in generators/*/all.js to correspondingly-named
files in generators/ instead—i.e., generators/javascript/all.js
becomes generators/javascript.js.
- Update build_tasks.js accordingly.
- Move declaration of <lang>Generator instance from
generators/<lang>/<lang>_generator.js to generators/<lang>.js.
- Move .addReservedWords() calls from generators/<lang>/*.js to
generators/<lang>.js
- Modify generators/<lang>/*.js to export block generator functions
individually, rather than installing on <lang>Generator instance.
- Modify generators/<lang>.js to import and install block generator
functions on <lang>Generator instance.
Where these tests needed block generator functions preinstalled
they should have been importing the Blockly.<Lang>.all module.
Where they do not need the provided block generator functions
they can now create their own empty <Lang>Generator instances.
- Fix a malformation in previous entries that was not detected by
the renaming file validator test.
- Add entries describing the work done in this and related recent
PRs.
@cpcallen
cpcallen requested a review from maribethbJune 15, 2023 20:39
@cpcallen
cpcallen requested a review from a team as a code ownerJune 15, 2023 20:39
@github-actionsgithub-actionsBot added the PR: refactor Refactors code label Jun 15, 2023
@BeksOmegaBeksOmega self-assigned this Jun 16, 2023
@BeksOmega
BeksOmega self-requested a review June 16, 2023 18:00
Comment threadgenerators/dart.js
Comment threadgenerators/dart/colour.js
# Conflicts:
#	scripts/migration/renamings.json5
Comment threadgenerators/dart.js
Comment threadgenerators/dart.js
Comment threadgenerators/dart/colour.js
Comment threadgenerators/dart/dart_generator.js
Comment threadscripts/migration/renamings.json5 Outdated
Comment threadtests/mocha/generator_test.js Outdated
- Fix a search-and-replace error in renamings.json5
- Fix an incorrect-but-usable import in generator_test.js
@cpcallen
cpcallen merged commit 1309897 into RaspberryPiFoundation:developJun 20, 2023
@cpcallen
cpcallen deleted the refactor/7086/restructure branch June 20, 2023 22:23
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: refactorRefactors code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: Library generator publication changes + supporting change to CodeGenerator

3 participants

@cpcallen@maribethb@BeksOmega