* feat(generators): Pass this CodeGenerator to generator functions
This implements option 1A of proposal 1 of #7086.
This commit is not by itself a breaking change, except in the unlikely event that
developers' custom generator functions take an (optional) second argument of a
dfferent type.
* feat(generators): Accept generator argument in block functions
Accept a CodeGenerator instance as parameter two of every
per-block-type generator function.
* fix(generators): Pass generator when calling other generator functions
Make sure to pass generator to any other block functions that are
called recursively.
* refactor(generators)!: Use generator argument in generator functions
Refactor per-block-type generator functions to use the provided
generator argument to make recursive calls, rather than depending
on the closed-over <lang>Generator instance.
This allows generator functions to be moved between CodeGenerator
instances (of the same language, at least).
This commit was created by search-and-replace and addresses most
but not all recursive references; remaining uses will require
manual attention and will be dealt with in a following commit.
BREAKING CHANGE: This commit makes the generator functions we provide
dependent on the new generator parameter. Although
CodeGenerator.prototype.blockToCode has been modified to supply this,
so this change will not affect most developers, this change will be a
breaking change where developers make direct calls to these generator
functions without supplying the generator parameter. See previous
commit for an example of the update required.
* refactor(generators): Manual fix for remaining uses of langGenerator
Manually replace remaining uses of <lang>Generator in block
generator functions.
* fix(generators): Delete duplicate procedures_callnoreturn generator
For some reason the generator function for procedures_callnoreturn
appears twice in generators/javascript/procedures.js. Delete the
first copy (since the second one overwrote it anyway).
* chore(generators): Format
* feat(generators): Add block generator function dictionary
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.
* refactor(generators)!: Update generator definitions to use dictionary
* fix(tests): Have blockToCodeTest clean up after itself
Have the blockToCodeTest helper function delete the block generator
functions it adds to generator once the test is done.
* refactor(tests): Use generator dictionary in insertion marker test
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.
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. This 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/*/*, i.e. on javascriptGenerator,
dartGenerator etc.) e.g. to replace the implementation or reuse the
implementation for a different block type.
* feat(j2ts): Add support for migrating renaming imports
Convert
const {foo: bar} = require(/*...*/);
into
import {foo as bar} from /*...*/;
^^^^^^^^^^
Also fix a bug that caused relative paths to ESM in the same
directory to be missing a leading "./".
* fix(build): Fix trivial error exports for generators
The UMD wrapper was inadvertently exporting the contents of (e.g.)
the Blockly.JavaScript closure module rather than the intended
export of Blockly.JavaScript.all module - which went unnoticed
because the latter just reexported the former - but we are
about to convert the former to ESM.
* chore(generators): Migrate language generators to ESM
Migrate the main language generators in generators/*.js to ESM.
This was done by running js2ts on the files, renaming them back
to .js, and commenting out "import type" statements, which are
legal TS but not needed in JS (at least if you are not actually
letting Closure Compiler do type checking, which we are not.)
* chore(generators): Migrate block generators to ESM
Migrate generators/*/*.js (except all.js) to ESM.
This was done by running js2ts on the files, renaming them back
to .js, and removing now-spurious @suppress {extraRequire}
directives.
* chores(generators): Migrate generator chunk entrypoints to ESM
This was done by running js2ts on the files, renaming them back
to .js, and manually fixing the export statements.
An additional change to the chunk exports configuration in
build_tasks.js was necessary in order for the UMD wrapper to
find the new module object, which is given a different name
than the old exports object.
* fix(build): Minor corrections to build_tasks.js
- Use TSC_OUTPUT_DIR to find goog/goog.js when suppressing warnings.
- Remove unnecessary trailing semicolons.
* refactor(blocks): Remove declareLegacyNamespace
Remove the call to goog.module.declareLegacyNamespace from
Blockly.libraryBlocks. This entails:
- Changes to the UMD wrapper to be able to find the exports object.
- Changes to tests/bootstrap_helper.js to save the exports object
in the libraryBlocks global variable.
- As a precaution, renaming the tests/compile/test_blocks.js module
so that goog.provide does not touch Blockly or
Blockly.libraryBlocks, which may not exist / be writable.
* feat(build): Add support named exports from chunks
We need to convert the generators to named exports. For backwards
compatibility we still want e.g. Blockly.JavaScript to point at
the generator object when the chunk is loaded using a script tag.
Modify chunkWrapper to honour a .reexportOnly property in the
chunks table and generate suitable additional code in the UMD
wrapper.
* refactor(generators): Migrate JavaScript generator to named export
- Export the JavaScript generator object as javascriptGenerator
from the Blockly.JavaScript module(generators/javascript.js).
- Modify the Blockly.JavaScript.all module
(generators/javascript/all.js) to reexport the exports from
Blockly.JavaScript.
- Update chunk configuration so the generator object remains
available as Blockly.JavaScript when loading
javascript_compressed.js via a <script> tag.
(N.B. it is otherwise necessary to destructure the require
/ import.)
- Modify bootstrap_helper.js to store that export as
window.javascriptGenerator for use in test code.
- Modify test code to use javascriptGenerator instead of
Blockly.JavaScript.
- Modify .eslintrc.json so that javascriptGenerator is allowed
as a global in test/. (Also restrict use of Blockly global
to test/.)
N.B. that demo code in demos/code/code.js uses <script> tag
loading and so will continue to access Blockly.JavaScript.
* refactor(generators): Migrate Lua generator to named export
* refactor(generators): Migrate PHP generator to named export
* refactor(generators): Migrate Python generator to named export
* refactor(generators): Remove declareLegacyNamespace calls
Remove the goog.module.declareLegacyNamespace calls from the
generators.
This turns out to have the unexpected side-effect of causing the
compiler to rename the core/blockly.js exports object from
$.Blockly to just Blockly in blockly_compressed.js - presumably
because it no longer needs to be accessed in any subsequent chunk
because they no longer add properties to it. This requires
some changes (mainly simplification) to the chunkWrapper function
in build_tasks.js.
* refactor(core): Remove declareLegacyNamespace from blockly.js
So easy to do _now_: just need to:
- Make sure the UMD wrapper for the first chunk knows where the
exports object is.
- Use that same value to set the Blockly.VERSION @define.
- Have bootstrap_helper.js set window.Blockly to the exports
object.
- Fix tests/compile/test_blocks.js to not assume a Blockly
global variable, by converting it to a goog.module so we
can use a named require.
* refactor: convert generators/python/colour.js to goog.module
* refactor: convert generators/python/colour.js to named requires
* chore: run clang-format
* refactor: convert generators/python/lists.js to goog.module
* refactor: convert generators/python/lists.js to named requires
* chore: run clang-format
* refactor: convert generators/python/logic.js to goog.module
* refactor: convert generators/python/logic.js to named requires
* chore: run clang-format
* refactor: convert generators/python/loops.js to goog.module
* refactor: convert generators/python/loops.js to named requires
* chore: run clang-format
* refactor: convert generators/python/math.js to goog.module
* refactor: convert generators/python/math.js to named requires
* chore: run clang-format
* refactor: convert generators/python/procedures.js to goog.module
* refactor: convert generators/python/procedures.js to named requires
* chore: run clang-format
* refactor: convert generators/python/text.js to goog.module
* refactor: convert generators/python/text.js to named requires
* chore: run clang-format
* refactor: convert generators/python/variables_dynamic.js to named requires
* refactor: convert generators/python/variables.js to named requires
* chore: run clang-format
* refactor: convert generators/python.js to goog.module
* refactor: convert generators/python.js to named requires
* chore: run clang-format
* chore: remove spurious @private annotations
* chore: rebuild
There are only 10 instances of ++x in our codebase, compared with over 500 instances of x++. The stlye guide has no opinion on which to use, nor do I. But the lack of consistency was making regex searches for bugs more difficult.
Our files are up to a decade old, and have churned so much, that the initial author of the file no longer has much meaning.
Furthermore, this will encourage developers to post to the developer group, rather than emailing Googlers (usually me) directly.
* Google changed from an Inc to an LLC.
This happened back in 2017 but we didn’t notice. Officially we should update files from Inc to LLC when they are changed as part of regular edits, but this is a nightmare to remember for the next decade.
* Remove project description/titles from licenses
This is no longer part of Google’s header requirements. Our existing descriptions were useless (“Visual Blocks Editor”) or grossly obselete (“Visual Blocks Language”).
* License no longer requires URL.
* Fix license regexps.
This allows generators to have more control over the placement of suffix. Needed for ‘if’ blocks and function calls which require their suffix code to be somewhere other than the end.
Also, add loop’s prefix to ‘break’ blocks, since the loop’s suffix will be the next statement hit.
Also, reuse procedures_callreturn generator for procedures_callnoreturn.
* Adds message references to message string interpolation, in the form of %{BKY_STRING}.
* Re-adding CONTROLS_IFELSE block using the new syntax, referencing to CONTROL_IF equivalents.
Adding controls_ifelse, an if/else block that is loaded from JSON and does not use mutators. This gives "else" capability to Android & iOS implementations, which don't support JavaScript mutators.
Added this block to the playground simple toolbox and all generators.