Commit Graph

83 Commits

Author SHA1 Message Date
Christopher Allen
f947b3f4f6 refactor!: Remove remaining use of goog.module.declareLegacyNamespace. (#6254)
* 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.
2022-06-30 19:53:32 +01:00
gregdyke
706c2bfd41 fix: provide initial values to reduce functions in generated js (#6178) 2022-05-31 09:22:34 -07:00
Christopher Allen
3e19aded39 chore(generators): Don't add unnecessary escapes (#6161)
Closure Compiler was complaining about unnecessary backslashes in
template literals, so remove them.
2022-05-12 16:50:01 +01:00
Neil Fraser
1f6a1bd8d9 chore: Use ES6 template strings in CSS and code generators (#5902)
* Unindent CSS, save 3 kb of code.
* Convert generator functions to template strings. 
This resolves #5761.
2022-01-28 17:58:43 -08:00
Richard Doherty
a31003fab9 fix(generators): Fix an operator precedence issue in the math_number_property generators to remove extra parentheses (#5685)
* Fixed issue where the mathIsPrime function inserted extra parenthesis around certain blocks.
* Added tests to generated JS
* Updated generated code in Lua
* Updated Generated code for tests in Dart
* Updated generated code for tests in PHP
* Updated generated code for tests in Python
* Also changed var to const and let.

Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com>
Co-authored-by: LouisCatala <86700310+LouisCatala@users.noreply.github.com>
Co-authored-by: jeremyjacob123 <43049656+jeremyjacob123@users.noreply.github.com>
2022-01-27 15:54:05 +00:00
Rachel Fenichel
93a1bce1ac refactor: convert generators/javascript/loops.js to goog.module (#5758)
* refactor: convert generators/javascript/loops.js to goog.module

* refactor: convert generators/javascript/loops.js to named requires

* chore: run clang-format

* chore: rebuild deps.js
2021-11-30 18:51:11 -08:00
Rachel Fenichel
e11fe348b2 refactor: convert some js block generators to goog.module (#5756)
* refactor: convert generators/javascript/colour.js to goog.module

* refactor: convert generators/javascript/colour.js to named requires

* chore: run clang-format

* refactor: convert generators/javascript/lists.js to goog.module

* refactor: convert generators/javascript/lists.js to named requires

* chore: run clang-format

* refactor: convert generators/javascript/logic.js to goog.module

* refactor: convert generators/javascript/logic.js to named requires

* chore: run clang-format

* refactor: convert genereators/javascript/math.js to goog.module

* refactor: convert generators/javascript/math.js to named requires

* chore: rebuild deps.js
2021-11-30 17:31:55 -08:00
Rachel Fenichel
d9991516cc refactor: convert generators/javascript/procedures.js to goog.module (#5754)
* refactor: convert generators/javascript/procedures.js to goog.module

* refactor: convert generators/javascript/procedures.js to named requires

* chore: run clang-format
2021-11-30 17:25:03 -08:00
Christopher Allen
208d2008a3 refactor: Migrate blocks/loops.js to goog.module syntax (#5755)
* 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
2021-11-30 23:51:27 +00:00
Rachel Fenichel
874bbc13c9 refactor: convert some js block generators to goog.module (#5750)
* chore: migrate generators/javascript/text.js to goog.module

* chore: convert generators/javascript/text.js to named requires

* refactor: convert generators/javascript/variables.js to goog.module

* refactor: convert generators/javascript/variables_dynamic to goog.module
2021-11-30 15:09:29 -08:00
alschmiedt
780198e0c0 chore: revert #5730 (#5740)
* 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.
2021-11-30 19:27:19 +00:00
Rachel Fenichel
4db047ff32 chore: fix missing requires in generators (#5744)
* 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
2021-11-30 08:51:21 -08:00
Christopher Allen
985af10f6e chore(build): Use chunked compilation (#5721)
* 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.
2021-11-29 17:50:17 +00:00
alschmiedt
308253bfdf chore: moves the public loop mixin into its own file (#5730) 2021-11-24 15:57:28 -08:00
Neil Fraser
1c3f4f0524 fix: isPrime block should cast str as num (#5680)
Fixes #5678
2021-11-05 11:12:25 -07:00
Rachel Fenichel
de95ffd133 chore: update javascript block generators to const and let (#5661) 2021-11-03 10:27:13 -07:00
Neil Fraser
fa47c3c4a8 Normalize ++x to x++. (#5660)
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.
2021-11-02 09:22:11 -07:00
Neil Fraser
90b3f75d82 Remove @author tags (#5601)
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.
2021-10-15 09:50:46 -07:00
Neil Fraser
c929b3015b chore: Convert == to === and != to !== where possible. (#5599) 2021-10-15 09:17:04 -07:00
alschmiedt
5b1586ee1b test: update mocha tests to use goog_module (#5440)
* Use goog.module in mocha tests

* Fix compiler warnings

* Make test helpers a module

* Name test modules Blockly.test.*

This is to be more consistent with how non-test modules are named.

Also remove top-level goog.require of TestHelpers (now
Blockly.test.helpers) since requiring a side-effect-less module does
nothing.

* Convert block_test.js and comment_test.js to goog.module syntax

* Address PR comments

* Goog modulify tests

* Goog modulify toolbox helpers

* Fixes imports and moves common tests from workspace_test.js to a helper file.

* Update test deps after rebase

Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2021-09-16 13:00:38 -07:00
Neil Fraser
2f2252f588 Rename variableDB_ to nameDB_
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.
2021-05-27 21:30:26 -07:00
Monica Kozbial
3f88a3f193 Replace incorrect usage of ORDER_COMMA with ORDER_NONE (#4354) 2020-10-07 11:22:26 -07:00
Monica Kozbial
ef6f3ebe8c Clean up some text generator logic (#4342) 2020-09-30 15:18:40 -07:00
Monica Kozbial
81d355f5fa Fix order errors in text generators (#4329)
* 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
2020-09-30 14:08:45 -07:00
Rachel Fenichel
99dc14c323 Avoid .includes 2020-06-24 17:43:47 -07:00
Monica Kozbial
df1508b547 Use getter for variables in procedure generator. (#3971) 2020-06-15 09:47:51 -07:00
Neil Fraser
4e2f8e6e02 Use SPDX licences.
This is a followup to #3127.
At the time, SPDX licenses were pending approval by Google.
2020-02-11 13:27:20 -08:00
Sam El-Husseini
72c6aa8699 Block generator warnings (#3353) 2019-10-28 16:20:25 -07:00
Sam El-Husseini
64c52efb5b Resolve warnings in generator code (#3350) 2019-10-28 09:35:40 -07:00
alschmiedt
b19e6a5fbf Remove deprectaed use of Blockly.Procedures.NAME_TYPE (#3301) 2019-10-22 04:33:13 -05:00
Neil Fraser
b46a4fe286 Bring our license format up to date (#3127)
* 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.
2019-10-02 14:46:56 -07:00
Rachel Fenichel
6f4476c962 Textarea Field, Multiline Block (from acbart) (#2663)
* Field Textarea, text_multiline block, generators
2019-09-11 17:57:46 -07:00
Neil Fraser
e77a9b7e81 Change parseFloat() to Number()
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.
2019-08-01 11:36:58 -07:00
Neil Fraser
92c4d6d3a3 Fix colour injection vulnerability.
The new validators already solve this problem, but as a second layer of defence, the generators should also be secured.  Issue #2637
2019-07-15 16:03:55 -07:00
Neil Fraser
05253d0766 Add prefix/suffix to orphaned value blocks.
Also respect suppressPrefixSuffix on loops when generating prefix/suffix with continue/break blocks.
2019-05-17 15:04:39 -07:00
Neil Fraser
e642a77f5d Use conventional prefix/suffix for function calls.
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.
2019-05-17 15:04:39 -07:00
Neil Fraser
04e39c20f8 Fix indentation bug if no xfix exists in function 2019-05-14 17:19:31 -07:00
Neil Fraser
585866a1b6 Revisit function block for return.
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.
2019-05-14 17:19:31 -07:00
Neil Fraser
c0e14c3a7c Add method to suppress prefix/suffix from blocks.
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.
2019-05-14 17:19:31 -07:00
Neil Fraser
25adb40e66 Prefix and suffix edge cases for flow statements.
Call suffix code on break/continue before executing the break/continue.
Call prefix code for enclosing loop before executing continue.
2019-05-14 17:19:31 -07:00
Neil Fraser
0259f8bb48 Add extra suffix when generating if/return block.
Also fix loops in Lua.
2019-05-14 17:19:31 -07:00
Neil Fraser
ba18ae2159 Add STATEMENT_SUFFIX
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).
2019-05-14 17:19:31 -07:00
Nadya Febiana Djojosantoso
23bb0e00d9 Add math_atan2 JavaScript generator 2018-10-11 14:18:25 -07:00
Neil Fraser
f3a76cc288 Remove unneeded String() and str() calls.
String literals in JS and Python don’t need to be coerced to strings.
2018-09-25 14:00:00 -07:00
Neil Fraser
c8bfc888c8 Throw errors instead of strings. (#1974)
Blocks and generators
2018-07-16 16:33:44 -07:00
Rachel Fenichel
bce24fef6b Add generators for dynamic variable blocks. 2018-01-26 17:10:45 -08:00
jschanker
f0b68fad66 Fixed JS/PHP generators for math_number
It was returning ORDER_ATOMIC for the block for any number; changed it so that it now returns ORDER_UNARY_NEGATION for negative numbers.
2018-01-25 09:02:38 -08:00
Nicole Parrot
97ce5b3e4f Fix indentation for function blocks with return statements (#1362)
* fix indentation for the return statement in function blocks that return a value

* Fix hard-coded autoindent, and swap it for Blockly.PHP.INDENT

* Fix hard-coded 2 spaces indent and use language-appropriate global indent
2017-10-17 15:20:11 -07:00
Neil Fraser
f34a2c2ed4 Properly escape IDs in procedure generators. (#1305)
Issue #251 and #1304.
2017-09-04 08:51:16 -07:00
Tim Dawborn
46316c7cea Add a block to reverse a list (#844) 2017-01-21 10:48:42 -08:00