* chore(deps): Add pretter-plugin-organize-imports
* chore: Remove insignificant blank lines in import sections
Since prettier-plugin-organize-imports sorts imports within
sections separated by blank lines, but preserves the section
divisions, remove any blank lines that are not dividing imports
into meaningful sections.
Do not remove blank lines separating side-effect-only imports
from main imports.
* chore: Remove unneded eslint-disable directives
* chore: Organise imports
Previously generators could generate code from inputs that didn't exist and get back the empty string. This silent failure was causing problems for diagnosing issues. This PR changes the behaviour so that an error is thrown.
This will break generators which rely on the previous behaviour. Several of our demo blocks needed editing to accomodate this change.
Resolves#7665
* chore: delete mocha tests for angle field
* feat! : delete angle field
* chore(tests): delete colour tests from generator tests and golden files
* chore: delete colour blocks and associated generators
* chore: remove colour blocks from playgrounds
* chore: delete mocha tests for colour fields
* chore: fix incorrect comment
* chore: delete colour field from core
* chore: delete multiline input tests from generators tests and golden files
* chore: delete multiline text block and associated generators
* chore: remove multiline text block from playgrounds
* chore: delete mocha tests for multiline input field
* chore: delete multiline input field from core
* refactor(generators): Migrate lua_generator.js to TypeScript
* refactor(generators): Migrate generators/lua/* to TypeScript
* fix(generators): Fix type errors in generator functions
* refactor(generators): Migrate generators/lua.js to TypeScript
* chore(generator): Format
* chore(generators): JSDoc and formatting tweaks for PR #7654
---------
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
* feat: add getVariableName and getProcedureName to CodeGenerator
* feat: make nameDB_ public
* feat: update block code generators to use new APIs
* chore: fix build
* chore: remove unused imports
* fix(build): Restore erroneously-deleted filter function
This was deleted in PR #7406 as it was mainly being used to
filter core/ vs. test/mocha/ deps into separate deps files -
but it turns out also to be used for filtering error
messages too. Oops.
* refactor(tests): Migrate advanced compilation test to ES Modules
* refactor(build): Migrate main.js to TypeScript
This turns out to be pretty straight forward, even if it would
cause crashing if one actually tried to import this module
instead of just feeding it to Closure Compiler.
* chore(build): Remove goog.declareModuleId calls
Replace goog.declareModuleId calls with a comment recording the
former module ID for posterity (or at least until we decide
how to reformat the renamings file.
* chore(tests): Delete closure/goog/*
For the moment we still need something to serve as base.js for
the benefit of closure-make-deps, so we keep a vestigial
base.js around, containing only the @provideGoog declaration.
* refactor(build): Remove vestigial base.js
By changing slightly the command line arguments to
closure-make-deps and closure-calculate-chunks the need to have
any base.js is eliminated.
* chore: Typo fix for PR #7415
* feat: make quoting methods public
* feat: add quote and multilineQuote methods
* fix: update code generators to use new methods
* chore: remove aliases
* chore: revert changes to generators
* refactor(generators): Move lang.js -> lang/lang_gernator.js
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.
* refactor(generators) Move lang/all.js -> lang.js
- 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.
* fix(generators): Add missing exports for LuaGenerator, PhpGenerator
These were inadvertently omitted from #7161 and #7162, respectively.
* refactor(generators): Make block generator modules side-effect free
- 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.
* fix(tests): Fix tests broken by restructuring of generators
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.
* chore: Update renamings file
- 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.
* fix: Correct minor errors in PR #7173
- Fix a search-and-replace error in renamings.json5
- Fix an incorrect-but-usable import in generator_test.js
* 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/lua/colour.js to goog.module
* refactor: convert generators/lua/colour.js to named requires
* chore: run clang-format
* refactor: convert generators/lua/lists.js to goog.module
* refactor: convert generators/lua/lists.js to named requires
* chore: run clang-format
* fix: use getListIndex helper function in lua list generators
* refactor: convert generators/lua/logic.js to goog.module
* refactor: convert generators/lua/logic.js to named requires
* chore: run clang-format
* refactor: convert generators/lua/loops.js to goog.module
* refactor: convert generators/lua/loops.js to named requires
* chore: run clang-format
* refactor: convert generators/lua/math.js to goog.module
* refactor: convert generators/lua/math.js to named requires
* chore: run clang-format
* refcator: convert generators/lua/procedures.js to goog.module
* refactor: convert generators/lua/procedures.js to named requires
* chore: run clang-format
* chore: rebuild deps.js
* refactor: convert generators/lua/text.js to goog.module
* refactor: convert generators/lua/text.js to named requires
* refactor: convert generators/lua/variables_dynamic.js to goog.module
* refactor: convert generators/lua/variables_dynamic.js to named requires
* chore: run clang-format on text.js
* refactor: convert generators/lua/variables.js to goog.module
* refactor: convert generators/lua/variables.js to named requires
* chore: run clang-format
* chore: make a lua generator function internal
* chore: rebuild deps.js
* refactor: Turn .getSurroundLoop into a (Block) method
This considerably simplifies the code in the genrators, obviating
the need for generators to import Blockly.Constants.Loops, and
allowing the compiler to remove blocks/loops.js from the first
(Blockly) chunk of the compilation.
(The latter could and should have been arranged by making the
generator chunks depend on the blocks chunk, but that is no longer
necessary.)
* refactor: Migrate blocks/loops.js to goog.module
* refactor: Migrate blocks/loops.js named requires
* chore: clang-format blocks/loops.js
* chore: revert #5730
* fix: Fix compile failure due to incorrect visibility
The CONTROL_FLOW_IN_LOOP_CHECK_MIXIN is intended to be used outside of
blocks/ (it is, in particular, used in generators/) so it should have
been marked @public from the beginning.
* chore: fix some missing requires in generators
* chore: fix missing requires in dart block generators
* chore: replace Blockly.isNumber with Blockly.utils.string.isNumber in generators
* chore: fix more missing requires in block generators
* chore(build): Add "all" modules for blocks & generators
These modules (Blockly.blocks.all and Blockly.<Generator>.all) will
be the entry points for the corresponding chunks.
They also make it easier to pull in all the modules in each package
(e.g. for playground and tests).
It is necessary to set the Closure Compiler dependency_mode to
SORT_ONLY as otherwise it tries to compile the "all" modules before
their dependencies, which fails.
The only impact on the _compressed.js files is the addition of a short
string to the very end of each file, e.g.:
var module$exports$Blockly$JavaScript$all={};
* chore(deps): Add devDependency on closure-calculate-chunks
* feat(build): First pass at chunked complation
Add a new buildCompiled gulp target (npm run build:compiled) that
uses closure-calculate-chunks to do chunked compliation of core/,
blocks/ and generators/ all in a single pass.
This work is incomplete: the resulting *_compressed.js files don't
(yet) have UMD wrappers.
* chore(build): Generate chunk wrappers
A first pass; this does not have support for a namespace object yet.
* refactor(build): Use chunked compilation by default
Remove old "compressed" gulp tasks in favour of new "compiled" task.
* chore(build): Remove cruft from buildCompiled
Remove unneeded `done` parameter and commented-out options that had
been cargo-culted from the old build pipeline.
* fix(build): Fix test failures caused by new build pipeline
- Exclude closure/goog/base.js from compiler input; use
externs/goog-externs.js instead.
- Have the build:debug and build:strict targets only build the first
chunk (blockly_compressed.js).
- Fix namespace entries for blocks and generators.
* fix(build): Fix build failures on node v12
closure-calculate-chunks requires node.js v14 or later.
When running on node.js v14 or later have getChunkOptions save
the output of closure-calculate-chunks to
scripts/gulpfiles/chunks.json. When running on older versions of
node.js have it use this checked-in, cached output instead of
attempting to run closure-calculate-chunks.
* chore(build): enable --rename_prefix_namespace
This will allow modules in blocks/ and generators/ to use
goog.require to obtain the exports object of goog.modules from
core/.
* fix(build): Always build all chunks
The previous commit enabled --rename_prefix_namespace option to
Closure Compiler, and this causes the buildCompressed target to
work fine when run without --debug or --strict, but adding either
of those flags (as for example when `npm test` runs
`npm run build:debug`) causes an issue:
- Because of many compiler errors in blocks/ and generators/,
a previous commit added a hack to only build the first chunk
when doing debug/strict builds.
- When asked to build only one chunk, Closure Compiler ignores the
--rename_prefix_namespace flag, because it 'correctly' infers
that there are no later chunks that will need to access global
variables from the first chunk.
- This causes a test failure, because `npm test` first runs
`npm run build`, which generates a valid blockly_compressed.js,
but this is then overrwritten by an invalid one when it next runs
`npm run build:debug`.
(The invalid one is missing all `$.` prefixes on 'global' variables,
including on Blockly, so the wrapper's last two lines -
"$.Blockly.internal_ = $;" and "return $.Blockly" - fail.)
The fix is to add appropriate @suppress annotations to blocks/*.js and
generators/**/*.js and then remove the first-chunk-only hack.
* refactor(build): Just build once
Since the previous commit caused `npm run build:debug` to do
everything that `... build:compressed` does - and to produce
byte-for-byte identical output - it doesn't make sense to run
both when testing. To that end:
- Replace the build:debug and build:strict package scripts that
did `gulp buildCompressed --...` with new scripts build-debug
and build-strict that do `gulp build --...` instead.
(The target names are changed so as to extend our existing naming
convention as follows: a target named "foo:bar" does some sub-part
of the job done by target "foo", but a target named "foo-bar" does
all the work of the target "foo" with some extra options.)
- build:debug:log and build:strict:log are similarly replaced with
build-debug-log and build-strict-log.
- Modify run_all_tests.js to just do `npm run build-debug` instead of
doing both `npm run build` and `npm run build:debug`.
- Also remove the 'build:blocks' script that should have been removed
when the buildBlocks gulp task was deleted previously.
* refactor(build): Compile with base_minimal.js instead of base.js
Introduce a (very!) cut-down version of closure/goog/base.js named
base_minimal.js that is used as input to the compiler as an
alternative to using externs/goog-externs.js (which will be deleted
once the buildAdvancedCompilationTest target has been updated).
This will allow use of goog.setTestOnly since it will now exist in
compiled mode, and allows the changes made in 5b112db to filter
base.js out of the files for the first chunk to be reverted.
(It also obliges a change to the compiled-mode check in blockly.js.)
* fix(build): Fix buildAdvanceCompilationTest
- In build_tasks.js:
- Replace the old compile() function with a new one factored out of
buildCompiled().
- Update buildAdvancedCompilationTest to use the new compile()
and other helpers created in the meantime.
- Remove no-longer-used maybeAddClosureLibrary().
- Remove externs/{block,generator,goog}-externs.js, which are no longer
used by any compile pipeline.
- Update core/blockly.js to fix issue with detection of compiled mode
when using ADVANCED_OPTIMISATIONS.
- Update only other use of globalThis, in core/utils/xml.js, to
consistently treat it as a dictionary object.
- Update instructions in tests/compile/index.html.
This commit is sort-of-a-prerequisite to #5602; test:compile:advanced
was previously working but the generated `main_compresed.js` would
throw errors upon loading.
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.
There is significant confusion in names and comments with regards to variables and procedures. `Blockly.Generator.prototype.variableDB_` is a Blockly.Names database, not a variable map. This rename introduces a getter and setter so deprecated references still work. This commit also fixes some comments which are either outright wrong or misleading regarding variable and procedure names.
* Fix dart text generator logic
* Fix Javascript text generator logic.
* Fix Lua text generator logic
* Fix php text generator logic
* Fix python text generator logic
* Fix error in substring logic
* Fix unit test block generators
* Update from ORDER_COMMA to ORDER_NONE
* Update golden files
* 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.
Number() is a bit less forgiving than parseFloat() and is more likely to generate NaN rather than some random number. An audit of each case shows nowhere that parseFloat()’s features are needed.
Previously a function call with no return value generated:
prefix();
suffix();
function();
The intent was so the prefix and suffix calls in the function body wouldn’t overlap with the prefix and suffix calls of the function call. However, this is doomed to be inconsistent with a function call with a return value:
prefix();
print(function());
suffix();
Thus since overlaping must exist, both types of function calls should have consistent suffix locations.
Visit (with prefix and suffix) function block when executed. This commit adds a revisit at the end of the function body when evaluating the return value.
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.
Also remove need to manually indent INFINITE_LOOP_TRAP. This is a breaking change for Python users of this property. However, very few users of this property exist, given that the existing code breaks if the block ID has a $ in it (also fixed in this PR).