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
This commit is contained in:
Rachel Fenichel
2021-12-01 17:59:20 -08:00
committed by GitHub
parent a939fec53b
commit c0517ea360
12 changed files with 809 additions and 961 deletions

View File

@@ -10,22 +10,23 @@
*/
'use strict';
goog.provide('Blockly.PHP');
goog.module('Blockly.PHP');
goog.module.declareLegacyNamespace();
goog.require('Blockly.Generator');
goog.require('Blockly.Names');
goog.require('Blockly.inputTypes');
goog.require('Blockly.utils.object');
goog.require('Blockly.utils.string');
goog.requireType('Blockly.Block');
goog.requireType('Blockly.Workspace');
const objectUtils = goog.require('Blockly.utils.object');
const stringUtils = goog.require('Blockly.utils.string');
const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator');
const {inputTypes} = goog.require('Blockly.inputTypes');
const {Names} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace');
/**
* PHP code generator.
* @type {!Blockly.Generator}
* @type {!Generator}
*/
Blockly.PHP = new Blockly.Generator('PHP');
const PHP = new Generator('PHP');
/**
* List of illegal variable names.
@@ -34,8 +35,8 @@ Blockly.PHP = new Blockly.Generator('PHP');
* accidentally clobbering a built-in object or function.
* @private
*/
Blockly.PHP.addReservedWords(
// http://php.net/manual/en/reserved.keywords.php
PHP.addReservedWords(
// http://php.net/manual/en/reserved.keywords.php
'__halt_compiler,abstract,and,array,as,break,callable,case,catch,class,' +
'clone,const,continue,declare,default,die,do,echo,else,elseif,empty,' +
'enddeclare,endfor,endforeach,endif,endswitch,endwhile,eval,exit,extends,' +
@@ -43,7 +44,7 @@ Blockly.PHP.addReservedWords(
'include_once,instanceof,insteadof,interface,isset,list,namespace,new,or,' +
'print,private,protected,public,require,require_once,return,static,' +
'switch,throw,trait,try,unset,use,var,while,xor,' +
// http://php.net/manual/en/reserved.constants.php
// http://php.net/manual/en/reserved.constants.php
'PHP_VERSION,PHP_MAJOR_VERSION,PHP_MINOR_VERSION,PHP_RELEASE_VERSION,' +
'PHP_VERSION_ID,PHP_EXTRA_VERSION,PHP_ZTS,PHP_DEBUG,PHP_MAXPATHLEN,' +
'PHP_OS,PHP_SAPI,PHP_EOL,PHP_INT_MAX,PHP_INT_SIZE,DEFAULT_INCLUDE_PATH,' +
@@ -54,90 +55,89 @@ Blockly.PHP.addReservedWords(
'E_CORE_WARNING,E_COMPILE_ERROR,E_COMPILE_WARNING,E_USER_ERROR,' +
'E_USER_WARNING,E_USER_NOTICE,E_DEPRECATED,E_USER_DEPRECATED,E_ALL,' +
'E_STRICT,__COMPILER_HALT_OFFSET__,TRUE,FALSE,NULL,__CLASS__,__DIR__,' +
'__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__'
);
'__FILE__,__FUNCTION__,__LINE__,__METHOD__,__NAMESPACE__,__TRAIT__');
/**
* Order of operation ENUMs.
* http://php.net/manual/en/language.operators.precedence.php
*/
Blockly.PHP.ORDER_ATOMIC = 0; // 0 "" ...
Blockly.PHP.ORDER_CLONE = 1; // clone
Blockly.PHP.ORDER_NEW = 1; // new
Blockly.PHP.ORDER_MEMBER = 2.1; // []
Blockly.PHP.ORDER_FUNCTION_CALL = 2.2; // ()
Blockly.PHP.ORDER_POWER = 3; // **
Blockly.PHP.ORDER_INCREMENT = 4; // ++
Blockly.PHP.ORDER_DECREMENT = 4; // --
Blockly.PHP.ORDER_BITWISE_NOT = 4; // ~
Blockly.PHP.ORDER_CAST = 4; // (int) (float) (string) (array) ...
Blockly.PHP.ORDER_SUPPRESS_ERROR = 4; // @
Blockly.PHP.ORDER_INSTANCEOF = 5; // instanceof
Blockly.PHP.ORDER_LOGICAL_NOT = 6; // !
Blockly.PHP.ORDER_UNARY_PLUS = 7.1; // +
Blockly.PHP.ORDER_UNARY_NEGATION = 7.2; // -
Blockly.PHP.ORDER_MULTIPLICATION = 8.1; // *
Blockly.PHP.ORDER_DIVISION = 8.2; // /
Blockly.PHP.ORDER_MODULUS = 8.3; // %
Blockly.PHP.ORDER_ADDITION = 9.1; // +
Blockly.PHP.ORDER_SUBTRACTION = 9.2; // -
Blockly.PHP.ORDER_STRING_CONCAT = 9.3; // .
Blockly.PHP.ORDER_BITWISE_SHIFT = 10; // << >>
Blockly.PHP.ORDER_RELATIONAL = 11; // < <= > >=
Blockly.PHP.ORDER_EQUALITY = 12; // == != === !== <> <=>
Blockly.PHP.ORDER_REFERENCE = 13; // &
Blockly.PHP.ORDER_BITWISE_AND = 13; // &
Blockly.PHP.ORDER_BITWISE_XOR = 14; // ^
Blockly.PHP.ORDER_BITWISE_OR = 15; // |
Blockly.PHP.ORDER_LOGICAL_AND = 16; // &&
Blockly.PHP.ORDER_LOGICAL_OR = 17; // ||
Blockly.PHP.ORDER_IF_NULL = 18; // ??
Blockly.PHP.ORDER_CONDITIONAL = 19; // ?:
Blockly.PHP.ORDER_ASSIGNMENT = 20; // = += -= *= /= %= <<= >>= ...
Blockly.PHP.ORDER_LOGICAL_AND_WEAK = 21; // and
Blockly.PHP.ORDER_LOGICAL_XOR = 22; // xor
Blockly.PHP.ORDER_LOGICAL_OR_WEAK = 23; // or
Blockly.PHP.ORDER_NONE = 99; // (...)
PHP.ORDER_ATOMIC = 0; // 0 "" ...
PHP.ORDER_CLONE = 1; // clone
PHP.ORDER_NEW = 1; // new
PHP.ORDER_MEMBER = 2.1; // []
PHP.ORDER_FUNCTION_CALL = 2.2; // ()
PHP.ORDER_POWER = 3; // **
PHP.ORDER_INCREMENT = 4; // ++
PHP.ORDER_DECREMENT = 4; // --
PHP.ORDER_BITWISE_NOT = 4; // ~
PHP.ORDER_CAST = 4; // (int) (float) (string) (array) ...
PHP.ORDER_SUPPRESS_ERROR = 4; // @
PHP.ORDER_INSTANCEOF = 5; // instanceof
PHP.ORDER_LOGICAL_NOT = 6; // !
PHP.ORDER_UNARY_PLUS = 7.1; // +
PHP.ORDER_UNARY_NEGATION = 7.2; // -
PHP.ORDER_MULTIPLICATION = 8.1; // *
PHP.ORDER_DIVISION = 8.2; // /
PHP.ORDER_MODULUS = 8.3; // %
PHP.ORDER_ADDITION = 9.1; // +
PHP.ORDER_SUBTRACTION = 9.2; // -
PHP.ORDER_STRING_CONCAT = 9.3; // .
PHP.ORDER_BITWISE_SHIFT = 10; // << >>
PHP.ORDER_RELATIONAL = 11; // < <= > >=
PHP.ORDER_EQUALITY = 12; // == != === !== <> <=>
PHP.ORDER_REFERENCE = 13; // &
PHP.ORDER_BITWISE_AND = 13; // &
PHP.ORDER_BITWISE_XOR = 14; // ^
PHP.ORDER_BITWISE_OR = 15; // |
PHP.ORDER_LOGICAL_AND = 16; // &&
PHP.ORDER_LOGICAL_OR = 17; // ||
PHP.ORDER_IF_NULL = 18; // ??
PHP.ORDER_CONDITIONAL = 19; // ?:
PHP.ORDER_ASSIGNMENT = 20; // = += -= *= /= %= <<= >>= ...
PHP.ORDER_LOGICAL_AND_WEAK = 21; // and
PHP.ORDER_LOGICAL_XOR = 22; // xor
PHP.ORDER_LOGICAL_OR_WEAK = 23; // or
PHP.ORDER_NONE = 99; // (...)
/**
* List of outer-inner pairings that do NOT require parentheses.
* @type {!Array<!Array<number>>}
*/
Blockly.PHP.ORDER_OVERRIDES = [
PHP.ORDER_OVERRIDES = [
// (foo()).bar() -> foo().bar()
// (foo())[0] -> foo()[0]
[Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_FUNCTION_CALL],
[PHP.ORDER_MEMBER, PHP.ORDER_FUNCTION_CALL],
// (foo[0])[1] -> foo[0][1]
// (foo.bar).baz -> foo.bar.baz
[Blockly.PHP.ORDER_MEMBER, Blockly.PHP.ORDER_MEMBER],
[PHP.ORDER_MEMBER, PHP.ORDER_MEMBER],
// !(!foo) -> !!foo
[Blockly.PHP.ORDER_LOGICAL_NOT, Blockly.PHP.ORDER_LOGICAL_NOT],
[PHP.ORDER_LOGICAL_NOT, PHP.ORDER_LOGICAL_NOT],
// a * (b * c) -> a * b * c
[Blockly.PHP.ORDER_MULTIPLICATION, Blockly.PHP.ORDER_MULTIPLICATION],
[PHP.ORDER_MULTIPLICATION, PHP.ORDER_MULTIPLICATION],
// a + (b + c) -> a + b + c
[Blockly.PHP.ORDER_ADDITION, Blockly.PHP.ORDER_ADDITION],
[PHP.ORDER_ADDITION, PHP.ORDER_ADDITION],
// a && (b && c) -> a && b && c
[Blockly.PHP.ORDER_LOGICAL_AND, Blockly.PHP.ORDER_LOGICAL_AND],
[PHP.ORDER_LOGICAL_AND, PHP.ORDER_LOGICAL_AND],
// a || (b || c) -> a || b || c
[Blockly.PHP.ORDER_LOGICAL_OR, Blockly.PHP.ORDER_LOGICAL_OR]
[PHP.ORDER_LOGICAL_OR, PHP.ORDER_LOGICAL_OR]
];
/**
* Whether the init method has been called.
* @type {?boolean}
*/
Blockly.PHP.isInitialized = false;
PHP.isInitialized = false;
/**
* Initialise the database of variable names.
* @param {!Blockly.Workspace} workspace Workspace to generate code from.
* @param {!Workspace} workspace Workspace to generate code from.
*/
Blockly.PHP.init = function(workspace) {
PHP.init = function(workspace) {
// Call Blockly.Generator's init.
Object.getPrototypeOf(this).init.call(this);
if (!this.nameDB_) {
this.nameDB_ = new Blockly.Names(this.RESERVED_WORDS_, '$');
this.nameDB_ = new Names(this.RESERVED_WORDS_, '$');
} else {
this.nameDB_.reset();
}
@@ -154,9 +154,9 @@ Blockly.PHP.init = function(workspace) {
* @param {string} code Generated code.
* @return {string} Completed code.
*/
Blockly.PHP.finish = function(code) {
PHP.finish = function(code) {
// Convert the definitions dictionary into a list.
const definitions = Blockly.utils.object.values(this.definitions_);
const definitions = objectUtils.values(this.definitions_);
// Call Blockly.Generator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false;
@@ -171,7 +171,7 @@ Blockly.PHP.finish = function(code) {
* @param {string} line Line of generated code.
* @return {string} Legal line of code.
*/
Blockly.PHP.scrubNakedValue = function(line) {
PHP.scrubNakedValue = function(line) {
return line + ';\n';
};
@@ -182,10 +182,10 @@ Blockly.PHP.scrubNakedValue = function(line) {
* @return {string} PHP string.
* @protected
*/
Blockly.PHP.quote_ = function(string) {
PHP.quote_ = function(string) {
string = string.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\\n')
.replace(/'/g, '\\\'');
.replace(/\n/g, '\\\n')
.replace(/'/g, '\\\'');
return '\'' + string + '\'';
};
@@ -196,7 +196,7 @@ Blockly.PHP.quote_ = function(string) {
* @return {string} PHP string.
* @protected
*/
Blockly.PHP.multiline_quote_ = function (string) {
PHP.multiline_quote_ = function(string) {
const lines = string.split(/\n/g).map(this.quote_);
// Join with the following, plus a newline:
// . "\n" .
@@ -208,26 +208,26 @@ Blockly.PHP.multiline_quote_ = function (string) {
* Common tasks for generating PHP from blocks.
* Handles comments for the specified block and any connected value blocks.
* Calls any statements following this block.
* @param {!Blockly.Block} block The current block.
* @param {!Block} block The current block.
* @param {string} code The PHP code created for this block.
* @param {boolean=} opt_thisOnly True to generate code for only this statement.
* @return {string} PHP code with comments and subsequent blocks added.
* @protected
*/
Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
PHP.scrub_ = function(block, code, opt_thisOnly) {
let commentCode = '';
// Only collect comments for blocks that aren't inline.
if (!block.outputConnection || !block.outputConnection.targetConnection) {
// Collect comment for this block.
let comment = block.getCommentText();
if (comment) {
comment = Blockly.utils.string.wrap(comment, this.COMMENT_WRAP - 3);
comment = stringUtils.wrap(comment, this.COMMENT_WRAP - 3);
commentCode += this.prefixLines(comment, '// ') + '\n';
}
// Collect comments for all value arguments.
// Don't collect comments for nested statements.
for (let i = 0; i < block.inputList.length; i++) {
if (block.inputList[i].type === Blockly.inputTypes.VALUE) {
if (block.inputList[i].type === inputTypes.VALUE) {
const childBlock = block.inputList[i].connection.targetBlock();
if (childBlock) {
comment = this.allNestedComments(childBlock);
@@ -245,15 +245,14 @@ Blockly.PHP.scrub_ = function(block, code, opt_thisOnly) {
/**
* Gets a property and adjusts the value while taking into account indexing.
* @param {!Blockly.Block} block The block.
* @param {!Block} block The block.
* @param {string} atId The property ID of the element to get.
* @param {number=} opt_delta Value to add.
* @param {boolean=} opt_negate Whether to negate the value.
* @param {number=} opt_order The highest order acting on this value.
* @return {string|number}
*/
Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
opt_order) {
PHP.getAdjusted = function(block, atId, opt_delta, opt_negate, opt_order) {
let delta = opt_delta || 0;
let order = opt_order || this.ORDER_NONE;
if (block.workspace.options.oneBasedIndex) {
@@ -274,7 +273,7 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
}
let at = this.valueToCode(block, atId, outerOrder) || defaultAtIndex;
if (Blockly.utils.string.isNumber(at)) {
if (stringUtils.isNumber(at)) {
// If the index is a naked number, adjust it right now.
at = Number(at) + delta;
if (opt_negate) {
@@ -302,3 +301,5 @@ Blockly.PHP.getAdjusted = function(block, atId, opt_delta, opt_negate,
}
return at;
};
exports = PHP;