Skip to content

Commit 7bda309

Browse files
authored
refactor(generators): Migrate PHP generators to TypeScript (#7647)
* refactor(generators): Migrate dart_generator.js to TypeScript * refactor(generators): Simplify getAdjusted Slightly simplify the implementation of getAdjusted, in part to make it more readable. Also improve its JSDoc comment. * refactor(generators): Migrate generators/php/* to TypeScript First pass doing very mechanistic migration, not attempting to fix all the resulting type errors. * fix(generators): Fix type errors in generator functions This consists almost entirely of adding casts, so the code output by tsc should be as similar as possible to the pre-migration .js source files. * fix(generators): Fix more minor inconsistencies in JS and Python The migration of the JavaScript and Python generators inadvertently introduced some inconsistencies in the code, e.g. putting executable code before the initial comment line that most generator functions begin with. This fixes another instance of this (but n.b. that these inline comments should have been JSDocs and a task has been added to #7600 to convert them). Additionally, I noticed while doing the PHP migration that ORDER_OVERRIDES was not typed as specifically as it could be, in previous migrations, so this is fixed here (along with the formatting of the associated JSDoc, which can fit on one line now.) * refactor(generators): Migrate generators/php.js to TypeScript The way the generator functions are added to phpGenerator.forBlock has been modified so that incorrect generator function signatures will cause tsc to generate a type error. * chore(generator): Format One block protected with // prettier-ignore to preserve careful comment formatting. Where there are repeated concatenations prettier has made a pretty mess of things, but the correct fix is probably to use template literals instead (rather than just locally disabling prettier). This is one of the items in the to-do list in #7600. * docs(generators): @fileoverview -> @file With an update to the wording for generators/php.ts. * fix(generators): Fixes for PR #7647. - Don't declare unused wherePascalCase dictionary. - Don't allow null in OPERATOR dictionary when not needed. - Fix return type (and documentation thereof) of getAdjusted.
1 parent b198e2f commit 7bda309

16 files changed

Lines changed: 1107 additions & 756 deletions

generators/javascript/javascript_generator.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ export enum Order {
6666
* JavaScript code generator class.
6767
*/
6868
export class JavascriptGenerator extends CodeGenerator {
69-
/**
70-
* List of outer-inner pairings that do NOT require parentheses.
71-
*/
72-
ORDER_OVERRIDES: number[][] = [
69+
/** List of outer-inner pairings that do NOT require parentheses. */
70+
ORDER_OVERRIDES: [Order, Order][] = [
7371
// (foo()).bar -> foo().bar
7472
// (foo())[0] -> foo()[0]
7573
[Order.FUNCTION_CALL, Order.MEMBER],

generators/javascript/lists.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ export function lists_getSublist(
302302
block: Block,
303303
generator: JavascriptGenerator,
304304
): [string, Order] {
305+
// Get sublist.
305306
// Dictionary of WHEREn field choices and their CamelCase equivalents.
306307
const wherePascalCase = {
307308
'FIRST': 'First',
@@ -310,7 +311,6 @@ export function lists_getSublist(
310311
'FROM_END': 'FromEnd',
311312
};
312313
type WhereOption = keyof typeof wherePascalCase;
313-
// Get sublist.
314314
const list = generator.valueToCode(block, 'LIST', Order.MEMBER) || '[]';
315315
const where1 = block.getFieldValue('WHERE1') as WhereOption;
316316
const where2 = block.getFieldValue('WHERE2') as WhereOption;
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
/**
8-
* @fileoverview Complete helper functions for generating PHP for
9-
* blocks. This is the entrypoint for php_compressed.js.
10-
* @suppress {extraRequire}
8+
* @file Instantiate a PhpGenerator and populate it with the complete
9+
* set of block generator functions for PHP. This is the entrypoint
10+
* for php_compressed.js.
1111
*/
1212

1313
// Former goog.module ID: Blockly.PHP.all
@@ -32,8 +32,17 @@ export * from './php/php_generator.js';
3232
export const phpGenerator = new PhpGenerator();
3333

3434
// Install per-block-type generator functions:
35-
Object.assign(
36-
phpGenerator.forBlock,
37-
colour, lists, logic, loops, math, procedures,
38-
text, variables, variablesDynamic
39-
);
35+
const generators: typeof phpGenerator.forBlock = {
36+
...colour,
37+
...lists,
38+
...logic,
39+
...loops,
40+
...math,
41+
...procedures,
42+
...text,
43+
...variables,
44+
...variablesDynamic,
45+
};
46+
for (const name in generators) {
47+
phpGenerator.forBlock[name] = generators[name];
48+
}
Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,52 @@
55
*/
66

77
/**
8-
* @fileoverview Generating PHP for colour blocks.
8+
* @file Generating PHP for colour blocks.
99
*/
1010

1111
// Former goog.module ID: Blockly.PHP.colour
1212

13+
import type {Block} from '../../core/block.js';
1314
import {Order} from './php_generator.js';
15+
import type {PhpGenerator} from './php_generator.js';
1416

15-
16-
export function colour_picker(block, generator) {
17+
export function colour_picker(
18+
block: Block,
19+
generator: PhpGenerator,
20+
): [string, Order] {
1721
// Colour picker.
1822
const code = generator.quote_(block.getFieldValue('COLOUR'));
1923
return [code, Order.ATOMIC];
20-
};
24+
}
2125

22-
export function colour_random(block, generator) {
26+
export function colour_random(
27+
block: Block,
28+
generator: PhpGenerator,
29+
): [string, Order] {
2330
// Generate a random colour.
24-
const functionName = generator.provideFunction_('colour_random', `
31+
const functionName = generator.provideFunction_(
32+
'colour_random',
33+
`
2534
function ${generator.FUNCTION_NAME_PLACEHOLDER_}() {
2635
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
2736
}
28-
`);
37+
`,
38+
);
2939
const code = functionName + '()';
3040
return [code, Order.FUNCTION_CALL];
31-
};
41+
}
3242

33-
export function colour_rgb(block, generator) {
43+
export function colour_rgb(
44+
block: Block,
45+
generator: PhpGenerator,
46+
): [string, Order] {
3447
// Compose a colour from RGB components expressed as percentages.
3548
const red = generator.valueToCode(block, 'RED', Order.NONE) || 0;
3649
const green = generator.valueToCode(block, 'GREEN', Order.NONE) || 0;
3750
const blue = generator.valueToCode(block, 'BLUE', Order.NONE) || 0;
38-
const functionName = generator.provideFunction_('colour_rgb', `
51+
const functionName = generator.provideFunction_(
52+
'colour_rgb',
53+
`
3954
function ${generator.FUNCTION_NAME_PLACEHOLDER_}($r, $g, $b) {
4055
$r = round(max(min($r, 100), 0) * 2.55);
4156
$g = round(max(min($g, 100), 0) * 2.55);
@@ -46,19 +61,23 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($r, $g, $b) {
4661
$hex .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
4762
return $hex;
4863
}
49-
`);
64+
`,
65+
);
5066
const code = functionName + '(' + red + ', ' + green + ', ' + blue + ')';
5167
return [code, Order.FUNCTION_CALL];
52-
};
68+
}
5369

54-
export function colour_blend(block, generator) {
70+
export function colour_blend(
71+
block: Block,
72+
generator: PhpGenerator,
73+
): [string, Order] {
5574
// Blend two colours together.
56-
const c1 =
57-
generator.valueToCode(block, 'COLOUR1', Order.NONE) || "'#000000'";
58-
const c2 =
59-
generator.valueToCode(block, 'COLOUR2', Order.NONE) || "'#000000'";
75+
const c1 = generator.valueToCode(block, 'COLOUR1', Order.NONE) || "'#000000'";
76+
const c2 = generator.valueToCode(block, 'COLOUR2', Order.NONE) || "'#000000'";
6077
const ratio = generator.valueToCode(block, 'RATIO', Order.NONE) || 0.5;
61-
const functionName = generator.provideFunction_('colour_blend', `
78+
const functionName = generator.provideFunction_(
79+
'colour_blend',
80+
`
6281
function ${generator.FUNCTION_NAME_PLACEHOLDER_}($c1, $c2, $ratio) {
6382
$ratio = max(min($ratio, 1), 0);
6483
$r1 = hexdec(substr($c1, 1, 2));
@@ -76,7 +95,8 @@ function ${generator.FUNCTION_NAME_PLACEHOLDER_}($c1, $c2, $ratio) {
7695
$hex .= str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
7796
return $hex;
7897
}
79-
`);
98+
`,
99+
);
80100
const code = functionName + '(' + c1 + ', ' + c2 + ', ' + ratio + ')';
81101
return [code, Order.FUNCTION_CALL];
82-
};
102+
}

0 commit comments

Comments
 (0)