fix: Rename Generator to CodeGenerator (#6585)

Stops collisions with ES6's Generator.
The old Blockly.Generator still exists as a name, but is now deprecated.
This commit is contained in:
Neil Fraser
2022-10-28 01:59:00 +02:00
committed by GitHub
parent 2311a94b03
commit e90aba9273
46 changed files with 155 additions and 158 deletions

View File

@@ -160,7 +160,6 @@
"jsdoc/check-param-names": ["off", {"checkDestructured": false}], "jsdoc/check-param-names": ["off", {"checkDestructured": false}],
// Allow any text in the license tag. Other checks are not relevant. // Allow any text in the license tag. Other checks are not relevant.
"jsdoc/check-values": ["off"] "jsdoc/check-values": ["off"]
} }
}] }]
} }

View File

@@ -71,7 +71,7 @@ import {FlyoutButton} from './flyout_button.js';
import {HorizontalFlyout} from './flyout_horizontal.js'; import {HorizontalFlyout} from './flyout_horizontal.js';
import {FlyoutMetricsManager} from './flyout_metrics_manager.js'; import {FlyoutMetricsManager} from './flyout_metrics_manager.js';
import {VerticalFlyout} from './flyout_vertical.js'; import {VerticalFlyout} from './flyout_vertical.js';
import {Generator} from './generator.js'; import {CodeGenerator} from './generator.js';
import {Gesture} from './gesture.js'; import {Gesture} from './gesture.js';
import {Grid} from './grid.js'; import {Grid} from './grid.js';
import {Icon} from './icon.js'; import {Icon} from './icon.js';
@@ -663,7 +663,8 @@ export {FieldVariable};
export {Flyout}; export {Flyout};
export {FlyoutButton}; export {FlyoutButton};
export {FlyoutMetricsManager}; export {FlyoutMetricsManager};
export {Generator}; export {CodeGenerator};
export {CodeGenerator as Generator}; // Deprecated name, October 2022.
export {Gesture}; export {Gesture};
export {Grid}; export {Grid};
export {HorizontalFlyout}; export {HorizontalFlyout};

View File

@@ -11,7 +11,7 @@
* @class * @class
*/ */
import * as goog from '../closure/goog/goog.js'; import * as goog from '../closure/goog/goog.js';
goog.declareModuleId('Blockly.Generator'); goog.declareModuleId('Blockly.CodeGenerator');
import type {Block} from './block.js'; import type {Block} from './block.js';
import * as common from './common.js'; import * as common from './common.js';
@@ -24,14 +24,14 @@ import type {Workspace} from './workspace.js';
* Class for a code generator that translates the blocks into a language. * Class for a code generator that translates the blocks into a language.
* *
* @unrestricted * @unrestricted
* @alias Blockly.Generator * @alias Blockly.CodeGenerator
*/ */
export class Generator { export class CodeGenerator {
name_: string; name_: string;
/** /**
* This is used as a placeholder in functions defined using * This is used as a placeholder in functions defined using
* Generator.provideFunction_. It must not be legal code that could * CodeGenerator.provideFunction_. It must not be legal code that could
* legitimately appear in a function definition (or comment), and it must * legitimately appear in a function definition (or comment), and it must
* not confuse the regular expression parser. * not confuse the regular expression parser.
*/ */
@@ -205,7 +205,7 @@ export class Generator {
|[string, number] { |[string, number] {
if (this.isInitialized === false) { if (this.isInitialized === false) {
console.warn( console.warn(
'Generator init was not called before blockToCode was called.'); 'CodeGenerator init was not called before blockToCode was called.');
} }
if (!block) { if (!block) {
return ''; return '';
@@ -414,7 +414,7 @@ export class Generator {
* "listRandom", not "random"). There is no danger of colliding with reserved * "listRandom", not "random"). There is no danger of colliding with reserved
* words, or user-defined variable or procedure names. * words, or user-defined variable or procedure names.
* *
* The code gets output when Generator.finish() is called. * The code gets output when CodeGenerator.finish() is called.
* *
* @param desiredName The desired name of the function (e.g. mathIsPrime). * @param desiredName The desired name of the function (e.g. mathIsPrime).
* @param code A list of statements or one multi-line code string. Use ' ' * @param code A list of statements or one multi-line code string. Use ' '
@@ -514,23 +514,23 @@ export class Generator {
} }
} }
Object.defineProperties(Generator.prototype, { Object.defineProperties(CodeGenerator.prototype, {
/** /**
* A database of variable names. * A database of variable names.
* *
* @name Blockly.Generator.prototype.variableDB_ * @name Blockly.CodeGenerator.prototype.variableDB_
* @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021). * @deprecated 'variableDB_' was renamed to 'nameDB_' (May 2021).
* @suppress {checkTypes} * @suppress {checkTypes}
*/ */
variableDB_: ({ variableDB_: ({
/** @returns Name database. */ /** @returns Name database. */
get(this: Generator): Names | get(this: CodeGenerator): Names |
undefined { undefined {
deprecation.warn('variableDB_', 'version 9', 'version 10', 'nameDB_'); deprecation.warn('variableDB_', 'version 9', 'version 10', 'nameDB_');
return this.nameDB_; return this.nameDB_;
}, },
/** @param nameDb New name database. */ /** @param nameDb New name database. */
set(this: Generator, nameDb: Names|undefined) { set(this: CodeGenerator, nameDb: Names|undefined) {
deprecation.warn('variableDB_', 'version 9', 'version 10', 'nameDB_'); deprecation.warn('variableDB_', 'version 9', 'version 10', 'nameDB_');
this.nameDB_ = nameDb; this.nameDB_ = nameDb;
}, },

View File

@@ -194,4 +194,3 @@ BlocklyDevTools.Analytics.sendQueued = function() {
// stub // stub
this.LOG_TO_CONSOLE_ && console.log('Analytics.sendQueued'); this.LOG_TO_CONSOLE_ && console.log('Analytics.sendQueued');
}; };

View File

@@ -373,7 +373,7 @@ Code.renderContent = function() {
/** /**
* Attempt to generate the code and display it in the UI, pretty printed. * Attempt to generate the code and display it in the UI, pretty printed.
* @param generator {!Blockly.Generator} The generator to use. * @param generator {!Blockly.CodeGenerator} The generator to use.
*/ */
Code.attemptCodeGeneration = function(generator) { Code.attemptCodeGeneration = function(generator) {
var content = document.getElementById('content_' + Code.selected); var content = document.getElementById('content_' + Code.selected);
@@ -388,7 +388,7 @@ Code.attemptCodeGeneration = function(generator) {
/** /**
* Check whether all blocks in use have generator functions. * Check whether all blocks in use have generator functions.
* @param generator {!Blockly.Generator} The generator to use. * @param generator {!Blockly.CodeGenerator} The generator to use.
*/ */
Code.checkAllGeneratorFunctionsDefined = function(generator) { Code.checkAllGeneratorFunctionsDefined = function(generator) {
var blocks = Code.workspace.getAllBlocks(false); var blocks = Code.workspace.getAllBlocks(false);

View File

@@ -108,4 +108,3 @@ class ViewController: UIViewController, WKUIDelegate {
present(alert, animated: true) present(alert, animated: true)
} }
} }

View File

@@ -15,7 +15,7 @@ goog.module('Blockly.Dart');
const Variables = goog.require('Blockly.Variables'); const Variables = goog.require('Blockly.Variables');
const stringUtils = goog.require('Blockly.utils.string'); const stringUtils = goog.require('Blockly.utils.string');
const {Block} = goog.requireType('Blockly.Block'); const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator'); const {CodeGenerator} = goog.require('Blockly.CodeGenerator');
const {Names, NameType} = goog.require('Blockly.Names'); const {Names, NameType} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace'); const {Workspace} = goog.requireType('Blockly.Workspace');
const {inputTypes} = goog.require('Blockly.inputTypes'); const {inputTypes} = goog.require('Blockly.inputTypes');
@@ -23,9 +23,9 @@ const {inputTypes} = goog.require('Blockly.inputTypes');
/** /**
* Dart code generator. * Dart code generator.
* @type {!Generator} * @type {!CodeGenerator}
*/ */
const Dart = new Generator('Dart'); const Dart = new CodeGenerator('Dart');
/** /**
* List of illegal variable names. * List of illegal variable names.
@@ -86,7 +86,7 @@ Dart.isInitialized = false;
* @param {!Workspace} workspace Workspace to generate code from. * @param {!Workspace} workspace Workspace to generate code from.
*/ */
Dart.init = function(workspace) { Dart.init = function(workspace) {
// Call Blockly.Generator's init. // Call Blockly.CodeGenerator's init.
Object.getPrototypeOf(this).init.call(this); Object.getPrototypeOf(this).init.call(this);
if (!this.nameDB_) { if (!this.nameDB_) {
@@ -145,7 +145,7 @@ Dart.finish = function(code) {
definitions.push(def); definitions.push(def);
} }
} }
// Call Blockly.Generator's finish. // Call Blockly.CodeGenerator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;

View File

@@ -15,7 +15,7 @@ goog.module('Blockly.JavaScript');
const Variables = goog.require('Blockly.Variables'); const Variables = goog.require('Blockly.Variables');
const stringUtils = goog.require('Blockly.utils.string'); const stringUtils = goog.require('Blockly.utils.string');
const {Block} = goog.requireType('Blockly.Block'); const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator'); const {CodeGenerator} = goog.require('Blockly.CodeGenerator');
const {inputTypes} = goog.require('Blockly.inputTypes'); const {inputTypes} = goog.require('Blockly.inputTypes');
const {Names, NameType} = goog.require('Blockly.Names'); const {Names, NameType} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace'); const {Workspace} = goog.requireType('Blockly.Workspace');
@@ -23,9 +23,9 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
/** /**
* JavaScript code generator. * JavaScript code generator.
* @type {!Generator} * @type {!CodeGenerator}
*/ */
const JavaScript = new Generator('JavaScript'); const JavaScript = new CodeGenerator('JavaScript');
/** /**
* List of illegal variable names. * List of illegal variable names.
@@ -127,7 +127,7 @@ JavaScript.isInitialized = false;
* @param {!Workspace} workspace Workspace to generate code from. * @param {!Workspace} workspace Workspace to generate code from.
*/ */
JavaScript.init = function(workspace) { JavaScript.init = function(workspace) {
// Call Blockly.Generator's init. // Call Blockly.CodeGenerator's init.
Object.getPrototypeOf(this).init.call(this); Object.getPrototypeOf(this).init.call(this);
if (!this.nameDB_) { if (!this.nameDB_) {
@@ -169,7 +169,7 @@ JavaScript.init = function(workspace) {
JavaScript.finish = function(code) { JavaScript.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
const definitions = Object.values(this.definitions_); const definitions = Object.values(this.definitions_);
// Call Blockly.Generator's finish. // Call Blockly.CodeGenerator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;

View File

@@ -15,7 +15,7 @@ goog.module('Blockly.Lua');
const stringUtils = goog.require('Blockly.utils.string'); const stringUtils = goog.require('Blockly.utils.string');
const {Block} = goog.requireType('Blockly.Block'); const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator'); const {CodeGenerator} = goog.require('Blockly.CodeGenerator');
const {inputTypes} = goog.require('Blockly.inputTypes'); const {inputTypes} = goog.require('Blockly.inputTypes');
const {Names} = goog.require('Blockly.Names'); const {Names} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace'); const {Workspace} = goog.requireType('Blockly.Workspace');
@@ -23,9 +23,9 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
/** /**
* Lua code generator. * Lua code generator.
* @type {!Generator} * @type {!CodeGenerator}
*/ */
const Lua = new Generator('Lua'); const Lua = new CodeGenerator('Lua');
/** /**
* List of illegal variable names. * List of illegal variable names.
@@ -92,7 +92,7 @@ Lua.isInitialized = false;
* @param {!Workspace} workspace Workspace to generate code from. * @param {!Workspace} workspace Workspace to generate code from.
*/ */
Lua.init = function(workspace) { Lua.init = function(workspace) {
// Call Blockly.Generator's init. // Call Blockly.CodeGenerator's init.
Object.getPrototypeOf(this).init.call(this); Object.getPrototypeOf(this).init.call(this);
if (!this.nameDB_) { if (!this.nameDB_) {
@@ -115,7 +115,7 @@ Lua.init = function(workspace) {
Lua.finish = function(code) { Lua.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
const definitions = Object.values(this.definitions_); const definitions = Object.values(this.definitions_);
// Call Blockly.Generator's finish. // Call Blockly.CodeGenerator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;

View File

@@ -14,7 +14,7 @@ goog.module('Blockly.PHP');
const stringUtils = goog.require('Blockly.utils.string'); const stringUtils = goog.require('Blockly.utils.string');
const {Block} = goog.requireType('Blockly.Block'); const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator'); const {CodeGenerator} = goog.require('Blockly.CodeGenerator');
const {inputTypes} = goog.require('Blockly.inputTypes'); const {inputTypes} = goog.require('Blockly.inputTypes');
const {Names} = goog.require('Blockly.Names'); const {Names} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace'); const {Workspace} = goog.requireType('Blockly.Workspace');
@@ -22,9 +22,9 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
/** /**
* PHP code generator. * PHP code generator.
* @type {!Generator} * @type {!CodeGenerator}
*/ */
const PHP = new Generator('PHP'); const PHP = new CodeGenerator('PHP');
/** /**
* List of illegal variable names. * List of illegal variable names.
@@ -130,7 +130,7 @@ PHP.isInitialized = false;
* @param {!Workspace} workspace Workspace to generate code from. * @param {!Workspace} workspace Workspace to generate code from.
*/ */
PHP.init = function(workspace) { PHP.init = function(workspace) {
// Call Blockly.Generator's init. // Call Blockly.CodeGenerator's init.
Object.getPrototypeOf(this).init.call(this); Object.getPrototypeOf(this).init.call(this);
if (!this.nameDB_) { if (!this.nameDB_) {
@@ -154,7 +154,7 @@ PHP.init = function(workspace) {
PHP.finish = function(code) { PHP.finish = function(code) {
// Convert the definitions dictionary into a list. // Convert the definitions dictionary into a list.
const definitions = Object.values(this.definitions_); const definitions = Object.values(this.definitions_);
// Call Blockly.Generator's finish. // Call Blockly.CodeGenerator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;

View File

@@ -15,7 +15,7 @@ goog.module('Blockly.Python');
const stringUtils = goog.require('Blockly.utils.string'); const stringUtils = goog.require('Blockly.utils.string');
const Variables = goog.require('Blockly.Variables'); const Variables = goog.require('Blockly.Variables');
const {Block} = goog.requireType('Blockly.Block'); const {Block} = goog.requireType('Blockly.Block');
const {Generator} = goog.require('Blockly.Generator'); const {CodeGenerator} = goog.require('Blockly.CodeGenerator');
const {inputTypes} = goog.require('Blockly.inputTypes'); const {inputTypes} = goog.require('Blockly.inputTypes');
const {Names, NameType} = goog.require('Blockly.Names'); const {Names, NameType} = goog.require('Blockly.Names');
const {Workspace} = goog.requireType('Blockly.Workspace'); const {Workspace} = goog.requireType('Blockly.Workspace');
@@ -23,9 +23,9 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
/** /**
* Python code generator. * Python code generator.
* @type {!Generator} * @type {!CodeGenerator}
*/ */
const Python = new Generator('Python'); const Python = new CodeGenerator('Python');
/** /**
* List of illegal variable names. * List of illegal variable names.
@@ -137,10 +137,10 @@ Python.isInitialized = false;
/** /**
* Initialise the database of variable names. * Initialise the database of variable names.
* @param {!Workspace} workspace Workspace to generate code from. * @param {!Workspace} workspace Workspace to generate code from.
* @this {Generator} * @this {CodeGenerator}
*/ */
Python.init = function(workspace) { Python.init = function(workspace) {
// Call Blockly.Generator's init. // Call Blockly.CodeGenerator's init.
Object.getPrototypeOf(this).init.call(this); Object.getPrototypeOf(this).init.call(this);
/** /**
@@ -196,7 +196,7 @@ Python.finish = function(code) {
definitions.push(def); definitions.push(def);
} }
} }
// Call Blockly.Generator's finish. // Call Blockly.CodeGenerator's finish.
code = Object.getPrototypeOf(this).finish.call(this, code); code = Object.getPrototypeOf(this).finish.call(this, code);
this.isInitialized = false; this.isInitialized = false;

View File

@@ -166,7 +166,6 @@ suite('Dropdown Fields', function() {
this.workspace = new Blockly.Workspace(); this.workspace = new Blockly.Workspace();
defineRowBlock(); defineRowBlock();
this.assertValue = (value, field) => { this.assertValue = (value, field) => {
const block = this.workspace.newBlock('row_block'); const block = this.workspace.newBlock('row_block');
field.setValue(value); field.setValue(value);

View File

@@ -27,7 +27,7 @@ suite('Generator', function() {
suite('prefix', function() { suite('prefix', function() {
setup(function() { setup(function() {
this.generator = new Blockly.Generator('INTERCAL'); this.generator = new Blockly.CodeGenerator('INTERCAL');
}); });
test('Nothing', function() { test('Nothing', function() {

View File

@@ -55,7 +55,7 @@ export class CodeGenerationTestSuite {
*/ */
constructor() { constructor() {
/** /**
* @type {!Blockly.Generator} The generator to use for running test cases. * @type {!Blockly.CodeGenerator} The generator to use for running test cases.
*/ */
this.generator; this.generator;
} }
@@ -64,7 +64,7 @@ export class CodeGenerationTestSuite {
/** /**
* Returns mocha test callback for code generation based on provided * Returns mocha test callback for code generation based on provided
* generator. * generator.
* @param {!Blockly.Generator} generator The generator to use in test. * @param {!Blockly.CodeGenerator} generator The generator to use in test.
* @return {function(!CodeGenerationTestCase):!Function} Function that * @return {function(!CodeGenerationTestCase):!Function} Function that
* returns mocha test callback based on test case. * returns mocha test callback based on test case.
* @private * @private