From b79c582fb6fdabece77520bf1265ca9c02b05ff2 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 14 Aug 2023 16:56:20 +0000 Subject: [PATCH 1/5] feat: make quoting methods public --- core/generator.ts | 10 +++++----- generators/dart/dart_generator.js | 2 -- generators/javascript/javascript_generator.js | 2 -- generators/lua/lua_generator.js | 2 -- generators/php/php_generator.js | 2 -- generators/python/python_generator.js | 2 -- 6 files changed, 5 insertions(+), 15 deletions(-) diff --git a/core/generator.ts b/core/generator.ts index f70856367f3..e3fd05b06f9 100644 --- a/core/generator.ts +++ b/core/generator.ts @@ -37,7 +37,7 @@ export type BlockGenerator = ( /** * Class for a code generator that translates the blocks into a language. */ -export class CodeGenerator { +export abstract class CodeGenerator { name_: string; /** A dictionary of block generator functions keyed by block type. */ @@ -170,10 +170,6 @@ export class CodeGenerator { return codeString; } - // The following are some helpful functions which can be used by multiple - - // languages. - /** * Prepend a common prefix onto each line of code. * Intended for indenting code or adding comment markers. @@ -441,6 +437,10 @@ export class CodeGenerator { return msg.replace(/%1/g, "'" + id + "'"); } + abstract quote_(toQuote: string): string; + + abstract multiline_quote_(toQuote: string): string; + /** * Add one or more words to the list of reserved words for this language. * diff --git a/generators/dart/dart_generator.js b/generators/dart/dart_generator.js index 5f64e919f34..e43c025a92b 100644 --- a/generators/dart/dart_generator.js +++ b/generators/dart/dart_generator.js @@ -176,7 +176,6 @@ export class DartGenerator extends CodeGenerator { * Encode a string as a properly escaped Dart string, complete with quotes. * @param {string} string Text to encode. * @return {string} Dart string. - * @protected */ quote_(string) { // Can't use goog.string.quote since $ must also be escaped. @@ -192,7 +191,6 @@ export class DartGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} Dart string. - * @protected */ multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); diff --git a/generators/javascript/javascript_generator.js b/generators/javascript/javascript_generator.js index 18811011c8b..34e68f5aa0a 100644 --- a/generators/javascript/javascript_generator.js +++ b/generators/javascript/javascript_generator.js @@ -206,7 +206,6 @@ export class JavascriptGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} JavaScript string. - * @protected */ quote_(string) { // Can't use goog.string.quote since Google's style guide recommends @@ -222,7 +221,6 @@ export class JavascriptGenerator extends CodeGenerator { * with quotes. * @param {string} string Text to encode. * @return {string} JavaScript string. - * @protected */ multiline_quote_(string) { // Can't use goog.string.quote since Google's style guide recommends diff --git a/generators/lua/lua_generator.js b/generators/lua/lua_generator.js index 587fea90bc8..ce115963917 100644 --- a/generators/lua/lua_generator.js +++ b/generators/lua/lua_generator.js @@ -149,7 +149,6 @@ export class LuaGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} Lua string. - * @protected */ quote_(string) { string = string.replace(/\\/g, '\\\\') @@ -163,7 +162,6 @@ export class LuaGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} Lua string. - * @protected */ multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); diff --git a/generators/php/php_generator.js b/generators/php/php_generator.js index 59d595b83df..59e1e67c688 100644 --- a/generators/php/php_generator.js +++ b/generators/php/php_generator.js @@ -184,7 +184,6 @@ export class PhpGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} PHP string. - * @protected */ quote_(string) { string = string.replace(/\\/g, '\\\\') @@ -198,7 +197,6 @@ export class PhpGenerator extends CodeGenerator { * quotes. * @param {string} string Text to encode. * @return {string} PHP string. - * @protected */ multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); diff --git a/generators/python/python_generator.js b/generators/python/python_generator.js index 2f0aabe0c13..5e5836c476f 100644 --- a/generators/python/python_generator.js +++ b/generators/python/python_generator.js @@ -228,7 +228,6 @@ export class PythonGenerator extends CodeGenerator { * Encode a string as a properly escaped Python string, complete with quotes. * @param {string} string Text to encode. * @return {string} Python string. - * @protected */ quote_(string) { string = string.replace(/\\/g, '\\\\').replace(/\n/g, '\\\n'); @@ -250,7 +249,6 @@ export class PythonGenerator extends CodeGenerator { * with quotes. * @param {string} string Text to encode. * @return {string} Python string. - * @protected */ multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); From 04cd2fe682b6b5fe15bb99c363f4f89da8824a7e Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 15 Aug 2023 18:25:56 +0000 Subject: [PATCH 2/5] feat: add quote and multilineQuote methods --- core/generator.ts | 6 +----- generators/dart/dart_generator.js | 10 ++++++++-- generators/javascript/javascript_generator.js | 10 ++++++++-- generators/lua/lua_generator.js | 10 ++++++++-- generators/php/php_generator.js | 10 ++++++++-- generators/python/python_generator.js | 10 ++++++++-- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/core/generator.ts b/core/generator.ts index e3fd05b06f9..67a30a790fc 100644 --- a/core/generator.ts +++ b/core/generator.ts @@ -37,7 +37,7 @@ export type BlockGenerator = ( /** * Class for a code generator that translates the blocks into a language. */ -export abstract class CodeGenerator { +export class CodeGenerator { name_: string; /** A dictionary of block generator functions keyed by block type. */ @@ -437,10 +437,6 @@ export abstract class CodeGenerator { return msg.replace(/%1/g, "'" + id + "'"); } - abstract quote_(toQuote: string): string; - - abstract multiline_quote_(toQuote: string): string; - /** * Add one or more words to the list of reserved words for this language. * diff --git a/generators/dart/dart_generator.js b/generators/dart/dart_generator.js index e43c025a92b..6f7422d95d7 100644 --- a/generators/dart/dart_generator.js +++ b/generators/dart/dart_generator.js @@ -177,7 +177,7 @@ export class DartGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Dart string. */ - quote_(string) { + quote(string) { // Can't use goog.string.quote since $ must also be escaped. string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') @@ -186,19 +186,25 @@ export class DartGenerator extends CodeGenerator { return '\'' + string + '\''; } + /** Alias for the `quote` method. Use `quote` instead. */ + quote_ = this.quote; + /** * Encode a string as a properly escaped multiline Dart string, complete with * quotes. * @param {string} string Text to encode. * @return {string} Dart string. */ - multiline_quote_(string) { + multilineQuote(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // + '\n' + return lines.join(' + \'\\n\' + \n'); } + /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ + multiline_quote_ = this.multilineQuote; + /** * Common tasks for generating Dart from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/javascript/javascript_generator.js b/generators/javascript/javascript_generator.js index 34e68f5aa0a..fb073250ef8 100644 --- a/generators/javascript/javascript_generator.js +++ b/generators/javascript/javascript_generator.js @@ -207,7 +207,7 @@ export class JavascriptGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} JavaScript string. */ - quote_(string) { + quote(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. string = string.replace(/\\/g, '\\\\') @@ -216,19 +216,25 @@ export class JavascriptGenerator extends CodeGenerator { return '\'' + string + '\''; } + /** Alias for the `quote` method. Use `quote` instead. */ + quote_ = this.quote; + /** * Encode a string as a properly escaped multiline JavaScript string, complete * with quotes. * @param {string} string Text to encode. * @return {string} JavaScript string. */ - multiline_quote_(string) { + multilineQuote(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. const lines = string.split(/\n/g).map(this.quote_); return lines.join(' + \'\\n\' +\n'); } + /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ + multiline_quote_ = this.multilineQuote; + /** * Common tasks for generating JavaScript from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/lua/lua_generator.js b/generators/lua/lua_generator.js index ce115963917..e2c835c7d0b 100644 --- a/generators/lua/lua_generator.js +++ b/generators/lua/lua_generator.js @@ -150,26 +150,32 @@ export class LuaGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Lua string. */ - quote_(string) { + quote(string) { string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') .replace(/'/g, '\\\''); return '\'' + string + '\''; }; + /** Alias for the `quote` method. Use `quote` instead. */ + quote_ = this.quote; + /** * Encode a string as a properly escaped multiline Lua string, complete with * quotes. * @param {string} string Text to encode. * @return {string} Lua string. */ - multiline_quote_(string) { + multilineQuote(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // .. '\n' .. return lines.join(' .. \'\\n\' ..\n'); }; + /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ + multiline_quote_ = this.multilineQuote; + /** * Common tasks for generating Lua from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/php/php_generator.js b/generators/php/php_generator.js index 59e1e67c688..348763744c3 100644 --- a/generators/php/php_generator.js +++ b/generators/php/php_generator.js @@ -185,20 +185,23 @@ export class PhpGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} PHP string. */ - quote_(string) { + quote(string) { string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') .replace(/'/g, '\\\''); return '\'' + string + '\''; }; + /** Alias for the `quote` method. Use `quote` instead. */ + quote_ = this.quote; + /** * Encode a string as a properly escaped multiline PHP string, complete with * quotes. * @param {string} string Text to encode. * @return {string} PHP string. */ - multiline_quote_(string) { + multilineQuote(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // . "\n" . @@ -206,6 +209,9 @@ export class PhpGenerator extends CodeGenerator { return lines.join(' . \"\\n\" .\n'); }; + /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ + multiline_quote_ = this.multilineQuote; + /** * Common tasks for generating PHP from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/python/python_generator.js b/generators/python/python_generator.js index 5e5836c476f..670360e6057 100644 --- a/generators/python/python_generator.js +++ b/generators/python/python_generator.js @@ -229,7 +229,7 @@ export class PythonGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Python string. */ - quote_(string) { + quote(string) { string = string.replace(/\\/g, '\\\\').replace(/\n/g, '\\\n'); // Follow the CPython behaviour of repr() for a non-byte string. @@ -244,19 +244,25 @@ export class PythonGenerator extends CodeGenerator { return quote + string + quote; } + /** Alias for the `quote` method. Use `quote` instead. */ + quote_ = this.quote; + /** * Encode a string as a properly escaped multiline Python string, complete * with quotes. * @param {string} string Text to encode. * @return {string} Python string. */ - multiline_quote_(string) { + multilineQuote(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // + '\n' + return lines.join(' + \'\\n\' + \n'); } + /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ + multiline_quote_ = this.multilineQuote; + /** * Common tasks for generating Python from blocks. * Handles comments for the specified block and any connected value blocks. From 00f4bb45ad253bf80054cbc479c8d3299ffe9f2d Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 15 Aug 2023 18:32:45 +0000 Subject: [PATCH 3/5] fix: update code generators to use new methods --- generators/dart/text.js | 15 ++++++++++----- generators/javascript/text.js | 20 ++++++++++++-------- generators/lua/text.js | 15 ++++++++++----- generators/php/text.js | 15 ++++++++++----- generators/python/text.js | 15 ++++++++++----- 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/generators/dart/text.js b/generators/dart/text.js index a1dcade42b5..1c6605f877e 100644 --- a/generators/dart/text.js +++ b/generators/dart/text.js @@ -18,14 +18,19 @@ import {Order} from './dart_generator.js'; // RESERVED WORDS: 'Html,Math' export function text(block, generator) { - // Text value. - const code = generator.quote_(block.getFieldValue('TEXT')); - return [code, Order.ATOMIC]; + if (generator.quote) { + return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; + } + return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; }; export function text_multiline(block, generator) { - // Text value. - const code = generator.multiline_quote_(block.getFieldValue('TEXT')); + let code; + if (generator.multilineQuote) { + code = generator.multilineQuote(block.getFieldValue('TEXT')); + } else { + code = generator.multiline_quote_(block.getFieldValue('TEXT')); + } const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC; return [code, order]; diff --git a/generators/javascript/text.js b/generators/javascript/text.js index e3761293bc1..7c3f20856a4 100644 --- a/generators/javascript/text.js +++ b/generators/javascript/text.js @@ -54,17 +54,21 @@ const getSubstringIndex = function(stringName, where, opt_at) { }; export function text(block, generator) { - // Text value. - const code = generator.quote_(block.getFieldValue('TEXT')); - return [code, Order.ATOMIC]; + if (generator.quote) { + return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; + } + return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; }; export function text_multiline(block, generator) { - // Text value. - const code = - generator.multiline_quote_(block.getFieldValue('TEXT')); - const order = code.indexOf('+') !== -1 ? Order.ADDITION : - Order.ATOMIC; + let code; + if (generator.multilineQuote) { + code = generator.multilineQuote(block.getFieldValue('TEXT')); + } else { + code = generator.multiline_quote_(block.getFieldValue('TEXT')); + } + const order = + code.indexOf('+') !== -1 ? Order.ADDITION : Order.ATOMIC; return [code, order]; }; diff --git a/generators/lua/text.js b/generators/lua/text.js index 2cf13f7c6c8..5ba2a56611d 100644 --- a/generators/lua/text.js +++ b/generators/lua/text.js @@ -16,14 +16,19 @@ import {Order} from './lua_generator.js'; export function text(block, generator) { - // Text value. - const code = generator.quote_(block.getFieldValue('TEXT')); - return [code, Order.ATOMIC]; + if (generator.quote) { + return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; + } + return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; }; export function text_multiline(block, generator) { - // Text value. - const code = generator.multiline_quote_(block.getFieldValue('TEXT')); + let code; + if (generator.multilineQuote) { + code = generator.multilineQuote(block.getFieldValue('TEXT')); + } else { + code = generator.multiline_quote_(block.getFieldValue('TEXT')); + } const order = code.indexOf('..') !== -1 ? Order.CONCATENATION : Order.ATOMIC; return [code, order]; diff --git a/generators/php/text.js b/generators/php/text.js index 91da583a819..ac8bb96ff7d 100644 --- a/generators/php/text.js +++ b/generators/php/text.js @@ -16,14 +16,19 @@ import {Order} from './php_generator.js'; export function text(block, generator) { - // Text value. - const code = generator.quote_(block.getFieldValue('TEXT')); - return [code, Order.ATOMIC]; + if (generator.quote) { + return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; + } + return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; }; export function text_multiline(block, generator) { - // Text value. - const code = generator.multiline_quote_(block.getFieldValue('TEXT')); + let code; + if (generator.multilineQuote) { + code = generator.multilineQuote(block.getFieldValue('TEXT')); + } else { + code = generator.multiline_quote_(block.getFieldValue('TEXT')); + } const order = code.indexOf('.') !== -1 ? Order.STRING_CONCAT : Order.ATOMIC; return [code, order]; diff --git a/generators/python/text.js b/generators/python/text.js index 84fef551563..f0a7b2bdc2e 100644 --- a/generators/python/text.js +++ b/generators/python/text.js @@ -17,14 +17,19 @@ import {Order} from './python_generator.js'; export function text(block, generator) { - // Text value. - const code = generator.quote_(block.getFieldValue('TEXT')); - return [code, Order.ATOMIC]; + if (generator.quote) { + return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; + } + return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; }; export function text_multiline(block, generator) { - // Text value. - const code = generator.multiline_quote_(block.getFieldValue('TEXT')); + let code; + if (generator.multilineQuote) { + code = generator.multilineQuote(block.getFieldValue('TEXT')); + } else { + code = generator.multiline_quote_(block.getFieldValue('TEXT')); + } const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC; return [code, order]; From b4d17c47879fc04b26f6c3aa5b8dbc2610fa66d1 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Wed, 16 Aug 2023 22:35:58 +0000 Subject: [PATCH 4/5] chore: remove aliases --- generators/dart/dart_generator.js | 10 ++-------- generators/javascript/javascript_generator.js | 10 ++-------- generators/lua/lua_generator.js | 10 ++-------- generators/php/php_generator.js | 10 ++-------- generators/python/python_generator.js | 10 ++-------- 5 files changed, 10 insertions(+), 40 deletions(-) diff --git a/generators/dart/dart_generator.js b/generators/dart/dart_generator.js index 6f7422d95d7..e43c025a92b 100644 --- a/generators/dart/dart_generator.js +++ b/generators/dart/dart_generator.js @@ -177,7 +177,7 @@ export class DartGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Dart string. */ - quote(string) { + quote_(string) { // Can't use goog.string.quote since $ must also be escaped. string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') @@ -186,25 +186,19 @@ export class DartGenerator extends CodeGenerator { return '\'' + string + '\''; } - /** Alias for the `quote` method. Use `quote` instead. */ - quote_ = this.quote; - /** * Encode a string as a properly escaped multiline Dart string, complete with * quotes. * @param {string} string Text to encode. * @return {string} Dart string. */ - multilineQuote(string) { + multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // + '\n' + return lines.join(' + \'\\n\' + \n'); } - /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ - multiline_quote_ = this.multilineQuote; - /** * Common tasks for generating Dart from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/javascript/javascript_generator.js b/generators/javascript/javascript_generator.js index fb073250ef8..34e68f5aa0a 100644 --- a/generators/javascript/javascript_generator.js +++ b/generators/javascript/javascript_generator.js @@ -207,7 +207,7 @@ export class JavascriptGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} JavaScript string. */ - quote(string) { + quote_(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. string = string.replace(/\\/g, '\\\\') @@ -216,25 +216,19 @@ export class JavascriptGenerator extends CodeGenerator { return '\'' + string + '\''; } - /** Alias for the `quote` method. Use `quote` instead. */ - quote_ = this.quote; - /** * Encode a string as a properly escaped multiline JavaScript string, complete * with quotes. * @param {string} string Text to encode. * @return {string} JavaScript string. */ - multilineQuote(string) { + multiline_quote_(string) { // Can't use goog.string.quote since Google's style guide recommends // JS string literals use single quotes. const lines = string.split(/\n/g).map(this.quote_); return lines.join(' + \'\\n\' +\n'); } - /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ - multiline_quote_ = this.multilineQuote; - /** * Common tasks for generating JavaScript from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/lua/lua_generator.js b/generators/lua/lua_generator.js index e2c835c7d0b..ce115963917 100644 --- a/generators/lua/lua_generator.js +++ b/generators/lua/lua_generator.js @@ -150,32 +150,26 @@ export class LuaGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Lua string. */ - quote(string) { + quote_(string) { string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') .replace(/'/g, '\\\''); return '\'' + string + '\''; }; - /** Alias for the `quote` method. Use `quote` instead. */ - quote_ = this.quote; - /** * Encode a string as a properly escaped multiline Lua string, complete with * quotes. * @param {string} string Text to encode. * @return {string} Lua string. */ - multilineQuote(string) { + multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // .. '\n' .. return lines.join(' .. \'\\n\' ..\n'); }; - /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ - multiline_quote_ = this.multilineQuote; - /** * Common tasks for generating Lua from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/php/php_generator.js b/generators/php/php_generator.js index 348763744c3..59e1e67c688 100644 --- a/generators/php/php_generator.js +++ b/generators/php/php_generator.js @@ -185,23 +185,20 @@ export class PhpGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} PHP string. */ - quote(string) { + quote_(string) { string = string.replace(/\\/g, '\\\\') .replace(/\n/g, '\\\n') .replace(/'/g, '\\\''); return '\'' + string + '\''; }; - /** Alias for the `quote` method. Use `quote` instead. */ - quote_ = this.quote; - /** * Encode a string as a properly escaped multiline PHP string, complete with * quotes. * @param {string} string Text to encode. * @return {string} PHP string. */ - multilineQuote(string) { + multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // . "\n" . @@ -209,9 +206,6 @@ export class PhpGenerator extends CodeGenerator { return lines.join(' . \"\\n\" .\n'); }; - /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ - multiline_quote_ = this.multilineQuote; - /** * Common tasks for generating PHP from blocks. * Handles comments for the specified block and any connected value blocks. diff --git a/generators/python/python_generator.js b/generators/python/python_generator.js index 670360e6057..5e5836c476f 100644 --- a/generators/python/python_generator.js +++ b/generators/python/python_generator.js @@ -229,7 +229,7 @@ export class PythonGenerator extends CodeGenerator { * @param {string} string Text to encode. * @return {string} Python string. */ - quote(string) { + quote_(string) { string = string.replace(/\\/g, '\\\\').replace(/\n/g, '\\\n'); // Follow the CPython behaviour of repr() for a non-byte string. @@ -244,25 +244,19 @@ export class PythonGenerator extends CodeGenerator { return quote + string + quote; } - /** Alias for the `quote` method. Use `quote` instead. */ - quote_ = this.quote; - /** * Encode a string as a properly escaped multiline Python string, complete * with quotes. * @param {string} string Text to encode. * @return {string} Python string. */ - multilineQuote(string) { + multiline_quote_(string) { const lines = string.split(/\n/g).map(this.quote_); // Join with the following, plus a newline: // + '\n' + return lines.join(' + \'\\n\' + \n'); } - /** Alias for the `multilineQuote` method. Use `multilineQuote` instead. */ - multiline_quote_ = this.multilineQuote; - /** * Common tasks for generating Python from blocks. * Handles comments for the specified block and any connected value blocks. From 92fda55b51810c4bde62771d54b77f4937d48024 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Wed, 16 Aug 2023 22:38:58 +0000 Subject: [PATCH 5/5] chore: revert changes to generators --- generators/dart/text.js | 15 +++++---------- generators/javascript/text.js | 20 ++++++++------------ generators/lua/text.js | 15 +++++---------- generators/php/text.js | 15 +++++---------- generators/python/text.js | 15 +++++---------- 5 files changed, 28 insertions(+), 52 deletions(-) diff --git a/generators/dart/text.js b/generators/dart/text.js index 1c6605f877e..a1dcade42b5 100644 --- a/generators/dart/text.js +++ b/generators/dart/text.js @@ -18,19 +18,14 @@ import {Order} from './dart_generator.js'; // RESERVED WORDS: 'Html,Math' export function text(block, generator) { - if (generator.quote) { - return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; - } - return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; + // Text value. + const code = generator.quote_(block.getFieldValue('TEXT')); + return [code, Order.ATOMIC]; }; export function text_multiline(block, generator) { - let code; - if (generator.multilineQuote) { - code = generator.multilineQuote(block.getFieldValue('TEXT')); - } else { - code = generator.multiline_quote_(block.getFieldValue('TEXT')); - } + // Text value. + const code = generator.multiline_quote_(block.getFieldValue('TEXT')); const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC; return [code, order]; diff --git a/generators/javascript/text.js b/generators/javascript/text.js index 7c3f20856a4..e3761293bc1 100644 --- a/generators/javascript/text.js +++ b/generators/javascript/text.js @@ -54,21 +54,17 @@ const getSubstringIndex = function(stringName, where, opt_at) { }; export function text(block, generator) { - if (generator.quote) { - return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; - } - return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; + // Text value. + const code = generator.quote_(block.getFieldValue('TEXT')); + return [code, Order.ATOMIC]; }; export function text_multiline(block, generator) { - let code; - if (generator.multilineQuote) { - code = generator.multilineQuote(block.getFieldValue('TEXT')); - } else { - code = generator.multiline_quote_(block.getFieldValue('TEXT')); - } - const order = - code.indexOf('+') !== -1 ? Order.ADDITION : Order.ATOMIC; + // Text value. + const code = + generator.multiline_quote_(block.getFieldValue('TEXT')); + const order = code.indexOf('+') !== -1 ? Order.ADDITION : + Order.ATOMIC; return [code, order]; }; diff --git a/generators/lua/text.js b/generators/lua/text.js index 5ba2a56611d..2cf13f7c6c8 100644 --- a/generators/lua/text.js +++ b/generators/lua/text.js @@ -16,19 +16,14 @@ import {Order} from './lua_generator.js'; export function text(block, generator) { - if (generator.quote) { - return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; - } - return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; + // Text value. + const code = generator.quote_(block.getFieldValue('TEXT')); + return [code, Order.ATOMIC]; }; export function text_multiline(block, generator) { - let code; - if (generator.multilineQuote) { - code = generator.multilineQuote(block.getFieldValue('TEXT')); - } else { - code = generator.multiline_quote_(block.getFieldValue('TEXT')); - } + // Text value. + const code = generator.multiline_quote_(block.getFieldValue('TEXT')); const order = code.indexOf('..') !== -1 ? Order.CONCATENATION : Order.ATOMIC; return [code, order]; diff --git a/generators/php/text.js b/generators/php/text.js index ac8bb96ff7d..91da583a819 100644 --- a/generators/php/text.js +++ b/generators/php/text.js @@ -16,19 +16,14 @@ import {Order} from './php_generator.js'; export function text(block, generator) { - if (generator.quote) { - return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; - } - return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; + // Text value. + const code = generator.quote_(block.getFieldValue('TEXT')); + return [code, Order.ATOMIC]; }; export function text_multiline(block, generator) { - let code; - if (generator.multilineQuote) { - code = generator.multilineQuote(block.getFieldValue('TEXT')); - } else { - code = generator.multiline_quote_(block.getFieldValue('TEXT')); - } + // Text value. + const code = generator.multiline_quote_(block.getFieldValue('TEXT')); const order = code.indexOf('.') !== -1 ? Order.STRING_CONCAT : Order.ATOMIC; return [code, order]; diff --git a/generators/python/text.js b/generators/python/text.js index f0a7b2bdc2e..84fef551563 100644 --- a/generators/python/text.js +++ b/generators/python/text.js @@ -17,19 +17,14 @@ import {Order} from './python_generator.js'; export function text(block, generator) { - if (generator.quote) { - return [generator.quote(block.getFieldValue('TEXT')), Order.ATOMIC]; - } - return [generator.quote_(block.getFieldValue('TEXT')), Order.ATOMIC]; + // Text value. + const code = generator.quote_(block.getFieldValue('TEXT')); + return [code, Order.ATOMIC]; }; export function text_multiline(block, generator) { - let code; - if (generator.multilineQuote) { - code = generator.multilineQuote(block.getFieldValue('TEXT')); - } else { - code = generator.multiline_quote_(block.getFieldValue('TEXT')); - } + // Text value. + const code = generator.multiline_quote_(block.getFieldValue('TEXT')); const order = code.indexOf('+') !== -1 ? Order.ADDITIVE : Order.ATOMIC; return [code, order];