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
This commit is contained in:
Christopher Allen
2023-06-20 23:22:44 +01:00
committed by GitHub
parent 021a560ef0
commit 130989763c
64 changed files with 2120 additions and 2046 deletions

View File

@@ -12,10 +12,10 @@ import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.PHP.math');
import {NameType} from '../../core/names.js';
import {phpGenerator, Order} from '../php.js';
import {Order} from './php_generator.js';
phpGenerator.forBlock['math_number'] = function(block, generator) {
export function math_number(block, generator) {
// Numeric value.
let code = Number(block.getFieldValue('NUM'));
const order = code >= 0 ? Order.ATOMIC : Order.UNARY_NEGATION;
@@ -27,7 +27,7 @@ phpGenerator.forBlock['math_number'] = function(block, generator) {
return [code, order];
};
phpGenerator.forBlock['math_arithmetic'] = function(block, generator) {
export function math_arithmetic(block, generator) {
// Basic arithmetic operators, and power.
const OPERATORS = {
'ADD': [' + ', Order.ADDITION],
@@ -45,7 +45,7 @@ phpGenerator.forBlock['math_arithmetic'] = function(block, generator) {
return [code, order];
};
phpGenerator.forBlock['math_single'] = function(block, generator) {
export function math_single(block, generator) {
// Math operators with single operand.
const operator = block.getFieldValue('OP');
let code;
@@ -126,7 +126,7 @@ phpGenerator.forBlock['math_single'] = function(block, generator) {
return [code, Order.DIVISION];
};
phpGenerator.forBlock['math_constant'] = function(block, generator) {
export function math_constant(block, generator) {
// Constants: PI, E, the Golden Ratio, sqrt(2), 1/sqrt(2), INFINITY.
const CONSTANTS = {
'PI': ['M_PI', Order.ATOMIC],
@@ -139,7 +139,7 @@ phpGenerator.forBlock['math_constant'] = function(block, generator) {
return CONSTANTS[block.getFieldValue('CONSTANT')];
};
phpGenerator.forBlock['math_number_property'] = function(block, generator) {
export function math_number_property(block, generator) {
// Check if a number is even, odd, prime, whole, positive, or negative
// or if it is divisible by certain number. Returns true or false.
const PROPERTIES = {
@@ -194,7 +194,7 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($n) {
return [code, outputOrder];
};
phpGenerator.forBlock['math_change'] = function(block, generator) {
export function math_change(block, generator) {
// Add to a variable in place.
const argument0 =
generator.valueToCode(block, 'DELTA', Order.ADDITION) || '0';
@@ -205,11 +205,11 @@ phpGenerator.forBlock['math_change'] = function(block, generator) {
};
// Rounding functions have a single operand.
phpGenerator.forBlock['math_round'] = phpGenerator.forBlock['math_single'];
export const math_round = math_single;
// Trigonometry functions have a single operand.
phpGenerator.forBlock['math_trig'] = phpGenerator.forBlock['math_single'];
export const math_trig = math_single;
phpGenerator.forBlock['math_on_list'] = function(block, generator) {
export function math_on_list(block, generator) {
// Math functions for lists.
const func = block.getFieldValue('OP');
let list;
@@ -304,7 +304,7 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($list) {
return [code, Order.FUNCTION_CALL];
};
phpGenerator.forBlock['math_modulo'] = function(block, generator) {
export function math_modulo(block, generator) {
// Remainder computation.
const argument0 =
generator.valueToCode(block, 'DIVIDEND', Order.MODULUS) || '0';
@@ -314,7 +314,7 @@ phpGenerator.forBlock['math_modulo'] = function(block, generator) {
return [code, Order.MODULUS];
};
phpGenerator.forBlock['math_constrain'] = function(block, generator) {
export function math_constrain(block, generator) {
// Constrain a number between two limits.
const argument0 = generator.valueToCode(block, 'VALUE', Order.NONE) || '0';
const argument1 = generator.valueToCode(block, 'LOW', Order.NONE) || '0';
@@ -325,7 +325,7 @@ phpGenerator.forBlock['math_constrain'] = function(block, generator) {
return [code, Order.FUNCTION_CALL];
};
phpGenerator.forBlock['math_random_int'] = function(block, generator) {
export function math_random_int(block, generator) {
// Random integer between [X] and [Y].
const argument0 = generator.valueToCode(block, 'FROM', Order.NONE) || '0';
const argument1 = generator.valueToCode(block, 'TO', Order.NONE) || '0';
@@ -341,12 +341,12 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($a, $b) {
return [code, Order.FUNCTION_CALL];
};
phpGenerator.forBlock['math_random_float'] = function(block, generator) {
export function math_random_float(block, generator) {
// Random fraction between 0 and 1.
return ['(float)rand()/(float)getrandmax()', Order.FUNCTION_CALL];
};
phpGenerator.forBlock['math_atan2'] = function(block, generator) {
export function math_atan2(block, generator) {
// Arctangent of point (X, Y) in degrees from -180 to 180.
const argument0 = generator.valueToCode(block, 'X', Order.NONE) || '0';
const argument1 = generator.valueToCode(block, 'Y', Order.NONE) || '0';