Commit Graph

105 Commits

Author SHA1 Message Date
michaela-mm
d016801089 fix: Fix bug in IF block generators. (#8780) 2025-02-24 14:35:33 +00:00
Christopher Allen
ce22f42868 chore: Organise imports (#8527)
* 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
2024-08-15 03:16:14 +01:00
Maribeth Bottorff
b7cdeffccd chore: merge develop into rc/v11 2024-04-01 14:57:19 -07:00
Neil Fraser
5a6f22f0f3 feat!: Throw errors on missing inputs (#7969)
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
2024-03-28 23:53:57 +01:00
Neil Fraser
2ea9e21e6d chore: Clean up variable assignment. (#7962)
* chore: Clean up variable assignment.

* fix: variable now const as a result of prev commit
2024-03-26 19:13:52 +01:00
Christopher Allen
10ffcb706d release: Merge branch 'develop' into rc/v11.0.0 2024-03-18 19:57:04 +00:00
Christopher Allen
a6208d78b7 fix(generators): Make scrub_ public (#7940)
Fixes #2156.

In PRs #7602, #7616, #7646, #7647 and #7654 the @protected
access modifier on scrub_ on the CodeGenerator subclasses
was not transcribed to the new typescript signature.  I was
going to re-add it, but this breaks some of the procedure
block generator functions which rely on it, and then
@BeksOmega pointed out that this might be one of the
CodeGenerator API functions which we had already decided
should be public—and lo and behold I found #2156.

Per discussion amongst team, I am not renaming it to
scrub at this time.
2024-03-15 22:01:46 +00:00
Neil Fraser
0ecbcde9fc chore: Use .includes and .startsWith, not .indexOf (#7936)
Easier to read than the diverse collection of `=== 0` and `!== -1` and `> -1` tests.
2024-03-15 00:03:55 +01:00
Rachel Fenichel
67c3aae76c feat!: delete angle, colour, and multiline input fields and blocks (#7932)
* 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
2024-03-13 13:28:26 -07:00
Christopher Allen
7bda30924a refactor(generators): Migrate PHP generators to TypeScript (#7647)
* refactor(generators): Migrate dart_generator.js to TypeScript

* refactor(generators): Simplify getAdjusted

  Slightly simplify the implementation of getAdjusted, in part to
  make it more readable.  Also improve its JSDoc comment.

* refactor(generators): Migrate generators/php/* to TypeScript

  First pass doing very mechanistic migration, not attempting to fix
  all the resulting type errors.

* fix(generators): Fix type errors in generator functions

  This consists almost entirely of adding casts, so the code output
  by tsc should be as similar as possible to the pre-migration .js
  source files.

* fix(generators): Fix more minor inconsistencies in JS and Python

  The migration of the JavaScript and Python generators
  inadvertently introduced some inconsistencies in the code,
  e.g. putting executable code before the initial comment line that
  most generator functions begin with.  This fixes another instance
  of this (but n.b. that these inline comments should have been
  JSDocs and a task has been added to #7600 to convert them).

  Additionally, I noticed while doing the PHP migration that
  ORDER_OVERRIDES was not typed as specifically as it could be,
  in previous migrations, so this is fixed here (along with the
  formatting of the associated JSDoc, which can fit on one line
  now.)

* refactor(generators): Migrate generators/php.js to TypeScript

  The way the generator functions are added to
  phpGenerator.forBlock has been modified so that incorrect
  generator function signatures will cause tsc to generate a type
  error.

* chore(generator): Format

  One block protected with // prettier-ignore to preserve careful
  comment formatting.

  Where there are repeated concatenations prettier has made a pretty
  mess of things, but the correct fix is probably to use template
  literals instead (rather than just locally disabling prettier).
  This is one of the items in the to-do list in #7600.

* docs(generators): @fileoverview -> @file

  With an update to the wording for generators/php.ts.

* fix(generators): Fixes for PR #7647.

  - Don't declare unused wherePascalCase dictionary.
  - Don't allow null in OPERATOR dictionary when not needed.
  - Fix return type (and documentation thereof) of getAdjusted.
2023-11-28 16:59:19 +00:00
Christopher Allen
b198e2f4ae fix(generators): Improve consistency of migrated language generators (#7662)
* docs(generators): @fileoverview -> @file; delete @suppress

* fix(generators): Fix return type of getAdjusted

  And improve its documentation too.

  Fix the @returns doc for PythonGenerator's getAdjustedInt but
  (for now) don't change the type because it does actually
  return numbers in some circumstances.
2023-11-28 16:57:31 +00:00
Christopher Allen
81b427f4ad refactor(generators): Migrate Dart generators to TypeScript (#7646)
* refactor(generators): Migrate dart_generator.js to TypeScript

* refactor(generators): Simplify getAdjusted

  Slightly simplify the implementation of getAdjusted, in part to
  make it more readable.  Also improve its JSDoc comment.

* refactor(generators): Migrate generators/dart/* to TypeScript

  First pass doing very mechanistic migration, not attempting to fix
  all the resulting type errors.

* fix(generators): Fix type errors in generator functions

  This consists almost entirely of adding casts, so the code output
  by tsc should be as similar as possible to the pre-migration .js
  source files.

* fix(generators): Fix minor inconsistencies in JS and Python

  The migration of the JavaScript and Python generators
  inadvertently introduced some inconsistencies in the code,
  e.g.:

  - Incorrect import ordering.
  - Putting executable code before the initial comment line that
    most generator functions begin with, and/or deleting or
    replacing these comments.
    -  N.B. however that these inline comments should have been
       JSDocs; a task to convert them has been added to #7600.

* refactor(generators): Migrate generators/dart.js to TypeScript

  The way the generator functions are added to
  dartGenerator.forBlock has been modified so that incorrect
  generator function signatures will cause tsc to generate a type
  error.

* chore(generator): Format

  One block protected with // prettier-ignore to preserve careful
  comment formatting.

  Where there are repeated concatenations prettier has made a pretty
  mess of things, but the correct fix is probably to use template
  literals instead (rather than just locally disabling prettier).
  This is one of the items in the to-do list in #7600.

* fix(generators): Fix for PR #7646
2023-11-14 16:13:50 +00:00
Christopher Allen
4ab8d00099 refactor(generators): Migrate JavaScript generators to TypeScript (#7602)
* refactor(generators): Migrate javascript_generator.js to TypeScript

* refactor(generators): Simplify getAdjusted

  Slightly simplify the implementation of getAdjusted, in part to
  make it more readable.  Also improve its JSDoc comment.

* refactor(generators): Migrate generators/javascript/* to TypeScript

  First pass doing very mechanistic migration, not attempting to fix
  all the resulting type errors.

* fix(generators): Fix type errors in generator functions

  This consists almost entirely of adding casts, so the code output
  by tsc should be as similar as possible to the pre-migration .js
  source files.

* refactor(generators): Migrate generators/javascript.js to TypeScript

  The way the generator functions are added to
  javascriptGenerator.forBlock has been modified so that incorrect
  generator function signatures will cause tsc to generate a type
  error.

* chore(generator): Format

  One block protected with // prettier-ignore to preserve careful
  comment formatting.

  Where there are repeated concatenations prettier has made a pretty
  mess of things, but the correct fix is probably to use template
  literals instead (rather than just locally disabling prettier).
  This has been added to the to-do list in #7600.

* fix(generators): Fixes for PR #7602

* fix(generators): Fix syntax error
2023-10-30 08:16:13 +00:00
Maribeth Bottorff
68261e5dd9 feat: add getVariableName and getProcedureName to CodeGenerator (#7445)
* 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
2023-08-31 10:49:17 -07:00
Christopher Allen
b0a7c004a9 refactor(build): Delete Closure Library (#7415)
* 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
2023-08-31 00:24:47 +01:00
Beka Westberg
a0301a217a feat: make generator quoting methods public (#7392)
* 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
2023-08-21 09:05:23 -07:00
Christopher Allen
130989763c refactor(generators): Restructure generator modules to contain side effects (#7173)
* 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
2023-06-20 23:22:44 +01:00
Christopher Allen
a3458871db refactor(generators)!: Pass this CodeGenerator to individual generator functions (#7168)
* 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
2023-06-14 23:25:36 +01:00
Christopher Allen
306eb80216 refactor(generators): Introduce JavascriptGenerator class, Order enum (#7153)
* refactor(generators): Introduce class JavascriptGenerator

  Also fix an import ordering error.

* refactor(generators): Use Order.* instead of .ORDER_*

* refactor(generators): Don't rename javascriptGenerator
2023-06-13 21:30:37 +01:00
Christopher Allen
f9c865b1b3 refactor(generators)!: CodeGenerator per-block-type generator function dictionary (#7150)
* 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.
2023-06-13 20:41:14 +01:00
Christopher Allen
4d2201a427 chore(generators): Migrate generators to ES Modules (#7103)
* 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.
2023-05-19 23:09:37 +01:00
Neil Fraser
42fde0f81b chore: Reduce delta on ports to blockly-samples (#6886)
* Reduce usage of obsolete .keyCode property.
* Rename private properties/methods which violate eslint rules.
* Use arrays of bound events rather than individual properties.
* Improve typing info.
* Also fix O(n^2) recursive performance issue in theme's getComponentStyle function.
* And replace String(...) with '${...}' (smaller, faster).
* .toString() is considered harmful.
2023-03-15 13:28:57 -07:00
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