mirror of
https://github.com/google/blockly.git
synced 2026-01-06 08:30:13 +01:00
* 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.
124 lines
3.8 KiB
JavaScript
124 lines
3.8 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2014 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Generating Dart for logic blocks.
|
|
*/
|
|
|
|
import * as goog from '../../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.Dart.logic');
|
|
|
|
import {dartGenerator as Dart} from '../dart.js';
|
|
|
|
|
|
Dart['controls_if'] = function(block) {
|
|
// If/elseif/else condition.
|
|
let n = 0;
|
|
let code = '', branchCode, conditionCode;
|
|
if (Dart.STATEMENT_PREFIX) {
|
|
// Automatic prefix insertion is switched off for this block. Add manually.
|
|
code += Dart.injectId(Dart.STATEMENT_PREFIX, block);
|
|
}
|
|
do {
|
|
conditionCode =
|
|
Dart.valueToCode(block, 'IF' + n, Dart.ORDER_NONE) || 'false';
|
|
branchCode = Dart.statementToCode(block, 'DO' + n);
|
|
if (Dart.STATEMENT_SUFFIX) {
|
|
branchCode =
|
|
Dart.prefixLines(
|
|
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) +
|
|
branchCode;
|
|
}
|
|
code += (n > 0 ? 'else ' : '') + 'if (' + conditionCode + ') {\n' +
|
|
branchCode + '}';
|
|
n++;
|
|
} while (block.getInput('IF' + n));
|
|
|
|
if (block.getInput('ELSE') || Dart.STATEMENT_SUFFIX) {
|
|
branchCode = Dart.statementToCode(block, 'ELSE');
|
|
if (Dart.STATEMENT_SUFFIX) {
|
|
branchCode =
|
|
Dart.prefixLines(
|
|
Dart.injectId(Dart.STATEMENT_SUFFIX, block), Dart.INDENT) +
|
|
branchCode;
|
|
}
|
|
code += ' else {\n' + branchCode + '}';
|
|
}
|
|
return code + '\n';
|
|
};
|
|
|
|
Dart['controls_ifelse'] = Dart['controls_if'];
|
|
|
|
Dart['logic_compare'] = function(block) {
|
|
// Comparison operator.
|
|
const OPERATORS =
|
|
{'EQ': '==', 'NEQ': '!=', 'LT': '<', 'LTE': '<=', 'GT': '>', 'GTE': '>='};
|
|
const operator = OPERATORS[block.getFieldValue('OP')];
|
|
const order = (operator === '==' || operator === '!=') ?
|
|
Dart.ORDER_EQUALITY :
|
|
Dart.ORDER_RELATIONAL;
|
|
const argument0 = Dart.valueToCode(block, 'A', order) || '0';
|
|
const argument1 = Dart.valueToCode(block, 'B', order) || '0';
|
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
|
return [code, order];
|
|
};
|
|
|
|
Dart['logic_operation'] = function(block) {
|
|
// Operations 'and', 'or'.
|
|
const operator = (block.getFieldValue('OP') === 'AND') ? '&&' : '||';
|
|
const order =
|
|
(operator === '&&') ? Dart.ORDER_LOGICAL_AND : Dart.ORDER_LOGICAL_OR;
|
|
let argument0 = Dart.valueToCode(block, 'A', order);
|
|
let argument1 = Dart.valueToCode(block, 'B', order);
|
|
if (!argument0 && !argument1) {
|
|
// If there are no arguments, then the return value is false.
|
|
argument0 = 'false';
|
|
argument1 = 'false';
|
|
} else {
|
|
// Single missing arguments have no effect on the return value.
|
|
const defaultArgument = (operator === '&&') ? 'true' : 'false';
|
|
if (!argument0) {
|
|
argument0 = defaultArgument;
|
|
}
|
|
if (!argument1) {
|
|
argument1 = defaultArgument;
|
|
}
|
|
}
|
|
const code = argument0 + ' ' + operator + ' ' + argument1;
|
|
return [code, order];
|
|
};
|
|
|
|
Dart['logic_negate'] = function(block) {
|
|
// Negation.
|
|
const order = Dart.ORDER_UNARY_PREFIX;
|
|
const argument0 = Dart.valueToCode(block, 'BOOL', order) || 'true';
|
|
const code = '!' + argument0;
|
|
return [code, order];
|
|
};
|
|
|
|
Dart['logic_boolean'] = function(block) {
|
|
// Boolean values true and false.
|
|
const code = (block.getFieldValue('BOOL') === 'TRUE') ? 'true' : 'false';
|
|
return [code, Dart.ORDER_ATOMIC];
|
|
};
|
|
|
|
Dart['logic_null'] = function(block) {
|
|
// Null data type.
|
|
return ['null', Dart.ORDER_ATOMIC];
|
|
};
|
|
|
|
Dart['logic_ternary'] = function(block) {
|
|
// Ternary operator.
|
|
const value_if =
|
|
Dart.valueToCode(block, 'IF', Dart.ORDER_CONDITIONAL) || 'false';
|
|
const value_then =
|
|
Dart.valueToCode(block, 'THEN', Dart.ORDER_CONDITIONAL) || 'null';
|
|
const value_else =
|
|
Dart.valueToCode(block, 'ELSE', Dart.ORDER_CONDITIONAL) || 'null';
|
|
const code = value_if + ' ? ' + value_then + ' : ' + value_else;
|
|
return [code, Dart.ORDER_CONDITIONAL];
|
|
};
|