Commit Graph

204 Commits

Author SHA1 Message Date
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
931499295e refactor: convert python block generators to goog.module (#5771)
* 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
2021-12-01 20:43:08 -08:00
Rachel Fenichel
c0517ea360 refactor: convert some block generators to goog.module (#5770)
* refactor: convert generators/lua.js to goog.module

* refactor: convert generator/lua.js to named requires

* chore: run clang-format

* refactor: convert generators/php/colour.js to goog.module

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

* chore: run clang-format

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

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

* chore: run clang-format

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

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

* chore: run clang-format

* refactor: convert generators/php/loops.js to goog.module

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

* chore: run clang-format

* refactor: convert generators/php/math.js to goog.module

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

* chore: run clang-format

* refactor: convert generators/php/procedures.js to goog.module

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

* chore: run clang-format

* refactor: convert generators/php/text.js to goog.module

* refactor: convert generators/php/text.js to named requires

* chore: run clang-format

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

* refactor: convert generators/php/variables.js to named requires

* chore: run clang-format

* refactor: convert generators/php/variables_dynamic.js to goog.module

* refactor: convert generators/php/variables_dynamic.js to named requires

* refactor: convert generators/php.js to goog.module

* refactor: convert generators/php.js to named requires

* chore: run clang-format

* chore: rebuild deps.js
2021-12-01 17:59:20 -08:00
Rachel Fenichel
a939fec53b refactor: convert some block generators to goog.module (#5769)
* 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
2021-12-01 14:57:21 -08:00
Rachel Fenichel
37a1e9725c refactor: convert some block generators to goog.module (#5762)
* refactor: convert generators/javascript.js to goog.module

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

* chore: run clang-format

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

* refactor: convert generators/dart/variables.js to named requires

* chore: run clang-format

* refactor: convert generators/dart/variables_dynamic.js to goog.module

* refactor: convert generators/dart/variables_dynamic.js to named requires

* refactor: convert generators/dart/text.js to goog.module

* refactor: convert generators/dart/text.js to named requires

* chore: run clang-format

* refactor: convert generators/dart/procedures.js to goog.module

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

* chore: run clang-format

* refactor: convert generators/dart/math.js to goog.module

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

* chore: run clang-format

* refactor: convert generators/dart/loops.js to goog.module

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

* chore: run clang-format

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

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

* chore: run clang-format

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

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

* chore: run clang-format

* refactor: convert generators/dart/colour.js to goog.module

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

* chore: rebuild deps.js

* chore: lint
2021-12-01 10:42:08 -08: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
af2bf583af refactor: migrate generators/dart.js to goog.module syntax (#5749)
* chore: migrate generators/dart.js to goog.module

* chore: migrate generators/dart.js to named requires

* chore: Update generators/dart.js to alphabetize imports

Co-authored-by: Christopher Allen <cpcallen+github@gmail.com>

Co-authored-by: Christopher Allen <cpcallen+github@gmail.com>
2021-11-30 11:11:18 -08: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
3746688ebd chore: update lua block generators to const and let (#5662) 2021-11-05 09:57:59 -07:00
Rachel Fenichel
1b29a00267 chore: update php block generators to const and let (#5663) 2021-11-03 10:34:27 -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
Rachel Fenichel
f4e7249b1a chore: update var to const and let in dart block generators (#5654) 2021-10-29 12:35:15 -07:00
Rachel Fenichel
6c49d6c7e9 chore: update language generators to const and let (#5647)
* chore: update generators/javascript.js to const and let

* chore: update generators/lua.js to const and let

* chore: update generators/php.js to const and let

* chore: update generators/python.js to const and let
2021-10-29 11:05:16 -07:00
Rachel Fenichel
db78d9f280 chore: replace var with const and let in python block generators (#5650)
* chore: replace var with const and let in python block generators

* chore: update test deps

* chore: update bracket usage in switch statements
2021-10-29 11:02:58 -07:00
Rachel Fenichel
a366daacd1 chore: update generators/dart.js to const and let (#5646) 2021-10-27 12:44:09 -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
Christopher Allen
f9d0caa112 Migrate core.global.js to named exports (#5451)
This is part of #5153 but is being prioritised because we want remove
the declareLegacyNamespace calls from the core/utils/*.js modules and
then reexport them explicitly via utils.js, and it turns out that
doing so results in the exports object of this module being passed to
Object.freeze - which fails on the global object, which can't be made
non-extensible!

The new name chosen for the former default export is globalThis, since
it is intended to have the same value as the global variable of that
name; see:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
2021-09-14 14:39:29 +01:00
Neil Fraser
861a981cb6 Remove extra else clauses 2021-06-09 11:41:42 -07:00
Neil Fraser
cff7b359f4 Make comments more consistent. 2021-06-09 11:41:42 -07:00
Neil Fraser
76b5517008 Use null-prototype objects for maps
A {} has a bunch of names already defined on it (like ‘toString’).  When using an object as a map with arbitrary keys, it should not inherit from Object.prototype.
2021-06-09 11:41:42 -07:00
Neil Fraser
37a1ee55b8 Remove useless variable declarations in PHP. (#4849) 2021-05-27 21:40:04 -07:00
Neil Fraser
43b65d2aa1 Factor out procedure population from generators
Also fix bug in Lua generator where variables were not populated in the nameDB (only language with no variable initialization).
2021-05-27 21:30:26 -07:00
Neil Fraser
9869269ce2 Add procedures to name DB in init
This enables the generator for any block to see all variable names and procedure names in the whole program, including those that haven’t generated yet.
2021-05-27 21:30:26 -07:00
Neil Fraser
ecca5bf274 Use Object.values rather than loop
Only for JS, Lua, and PHP.  Dart and Python need extra logic to split out import statements.

Also use ‘this’ instead of fully qualified names in generators.
2021-05-27 21:30:26 -07:00
Neil Fraser
d919b0af21 Update JSDoc from Array.<> to Array<>
Same with Object.
2021-05-27 21:30:26 -07:00
Neil Fraser
b50cb5779f Set generator’s isInitialized to false in finish
Previously isInitialized would remain true after first generation.
Also move common init/finish code to parent class.
2021-05-27 21:30:26 -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
Rachel Fenichel
750b62030b First pass at creating connectionTypes and inputTYpes 2021-03-04 10:22:24 -08:00
Monica Kozbial
6510e49162 Add warning to blockToCode (#4492)
* Add warning to blockToCode

* Address PR comments
2020-12-07 10:38:32 -08:00
Monica Kozbial
9ef5c853b3 Remove ORDER_COMMA from PHP generator. (#4357) 2020-10-08 14:23:09 -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
0b423abd4d Merge pull request #4294 from rachel-fenichel/bugfix/dart_multiline
Fix dart multiline strings
2020-09-18 13:51:38 -07:00
Rachel Fenichel
1350604e93 Fix dart multiline strings 2020-09-18 13:37:54 -07:00
Rachel Fenichel
7da8d4abfe Fix PHP multiline string 2020-09-18 13:27:04 -07:00
Rachel Fenichel
afcbb7036e Fix python multiline string 2020-09-18 12:11:48 -07:00
Rachel Fenichel
773a4430e7 Fix lua multiline quote 2020-09-18 11:02:17 -07:00