-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
refactor(generators)!: CodeGenerator
per-block-type generator function dictionary
#7150
Conversation
Add a dictionary of block generator functions, provisionally called .forBlock. Look up generator functions there first, but fall back to looking up on 'this' (with deprecation notice) for backwards compatibility. Also tweak error message generation to use template literal.
Investigating test failure, which reproduces locally. I am slightly confused because it is caused by the change to |
Turns out test in |
BREAKING CHANGE: this PR moves the generator functions we provide from their previous location directly on the CodeGenerator instances to the new .forBlock dictionary on each instance. The change in the previous commit does not oblige external developers to do the same for their custom generators, but they will need to update any code that references the generator functions we provide in generators/*/* (e.g. to reuse one of our functions for a different block type).
Have the blockToCodeTest helper function delete the block generator functions it adds to generator once the test is done.
The use of generators in insertion_marker_test.js was overlooked in the earlier commit making such updates, and some test here were failing due to lack of cleanup in cleanup in the generator_test.js.
CodeGenerator
per-block-type generator function dictionaryCodeGenerator
per-block-type generator function dictionary
Force-push (and title edit) to make commits conventional. |
Addresses various nits that escaped previous PRs: * Add TSDoc for `BlockGenerator` in `core/generator.ts` for PR google#7150. * Fix bad formating in `generators/javascript.js` from PR google#7153. * Add missing `@enum` tag that should have been included in PR google#7160. * Delete obsolete comment from `generators/python.js` for PR google#7163.
Addresses various nits that escaped previous PRs: * Add TSDoc for `BlockGenerator` in `core/generator.ts` for PR #7150. * Fix bad formating in `generators/javascript.js` from PR #7153. * Add missing `@enum` tag that should have been included in PR #7160. * Delete obsolete comment from `generators/python.js` for PR #7163.
This is mostly just chore updating to the new import names (e.g. BlocklyJavaScript -> javascript.javascriptGenerator), but the change to Code.checkAllGeneratorFunctionsDefined is a necessary fix due to the breaking change in PR google#7150, implementing the .forBlock dictionary.
This is mostly just chore updating to the new import names (e.g. BlocklyJavaScript -> javascript.javascriptGenerator), but the change to Code.checkAllGeneratorFunctionsDefined is a necessary fix due to the breaking change in PR #7150, implementing the .forBlock dictionary.
The basics
npm run format
andnpm run lint
The details
Resolves
Fixes #7084
Proposed Changes
Implements per-block-type generator function dictionary, per proposal #7084.
Behaviour Before Change
CodeGenerator.prototype.blockToCode
uses generator function atthis[block_type]
.Behaviour After Change
CodeGenerator.prototype.blockToCode
preferentially uses generator function atthis.forBlock[block_type]
, falling back tothis[block_type]
if the former does not exist (and issuing a deprecation warning if the latter does).Code in
generators/*/*
is updated to install generator functions at the new location.Reason for Changes
See #7084.
Test Coverage
No changes to manual test procedures anticipated.
Documentation
We should update our generator demos/codelabs to use the new dictionary.
Additional Information
DEPRECATION: as of this PR, adding per-block generator function directly to
CodeGenerator
instances is deprecated; they should instead be added to the generator's.forBlock
dictionary—i.e., existing code of the form:should be updated to:
although the former style will continue to be supported at least until until Blockly v11.
BREAKING CHANGE: this PR prioritises the new
.forBlock
dictionary when doing lookups, and moves the generator functions we provide from their previous location directly on theCodeGenerator
instances to the new.forBlock
dictionary on each instance.Most existing custom generator code should continue to work unchanged for the time being, but any code that references the generator functions we provide—e.g. to replace an implementation we provide or to reuse the implementation for a different block type—will need to be updated:
must be updated to: