Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct enum formatting, use merged namespaces for types that are class static members #6246

Merged
merged 7 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 66 additions & 39 deletions core/contextmenu_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,13 @@ import {BlockSvg} from './block_svg.js';
import {WorkspaceSvg} from './workspace_svg.js';


enum ScopeType {
BLOCK = 'block',
WORKSPACE = 'workspace',
}

/**
* Class for the registry of context menu items. This is intended to be a
* singleton. You should not create a new instance, and only access this class
* from ContextMenuRegistry.registry.
* @alias Blockly.ContextMenuRegistry
*/
export class ContextMenuRegistry {
/**
* Where this menu item should be rendered. If the menu item should be
* rendered in multiple scopes, e.g. on both a block and a workspace, it
* should be registered for each scope.
*/
static ScopeType = ScopeType;
static registry: ContextMenuRegistry;
// TODO(b/109816955): remove '!', see go/strict-prop-init-fix.
private registry_!: {[key: string]: RegistryItem};
Expand Down Expand Up @@ -125,33 +114,71 @@ export class ContextMenuRegistry {
return menuOptions;
}
}
export interface Scope {
block: BlockSvg|undefined;
workspace: WorkspaceSvg|undefined;
}
export interface RegistryItem {
callback: (p1: Scope) => AnyDuringMigration;
scopeType: ScopeType;
displayText: ((p1: Scope) => string)|string;
preconditionFn: (p1: Scope) => string;
weight: number;
id: string;
}
export interface ContextMenuOption {
text: string;
enabled: boolean;
callback: (p1: Scope) => AnyDuringMigration;
scope: Scope;
weight: number;
}
export interface LegacyContextMenuOption {
text: string;
enabled: boolean;
callback: (p1: Scope) => AnyDuringMigration;

export namespace ContextMenuRegistry {
/**
* Where this menu item should be rendered. If the menu item should be
* rendered in multiple scopes, e.g. on both a block and a workspace, it
* should be registered for each scope.
*/
export enum ScopeType {
BLOCK = 'block',
WORKSPACE = 'workspace',
}

/**
* The actual workspace/block where the menu is being rendered. This is passed
* to callback and displayText functions that depend on this information.
*/
export interface Scope {
block: BlockSvg|undefined;
workspace: WorkspaceSvg|undefined;
}

/**
* A menu item as entered in the registry.
*/
export interface RegistryItem {
callback: (p1: Scope) => AnyDuringMigration;
scopeType: ScopeType;
displayText: ((p1: Scope) => string)|string;
preconditionFn: (p1: Scope) => string;
weight: number;
id: string;
}

/**
* A menu item as presented to contextmenu.js.
*/
export interface ContextMenuOption {
text: string;
enabled: boolean;
callback: (p1: Scope) => AnyDuringMigration;
scope: Scope;
weight: number;
}

/**
* A subset of ContextMenuOption corresponding to what was publicly
* documented. ContextMenuOption should be preferred for new code.
*/
export interface LegacyContextMenuOption {
text: string;
enabled: boolean;
callback: (p1: Scope) => AnyDuringMigration;
}

/**
* Singleton instance of this class. All interactions with this class should
* be done on this object.
*/
ContextMenuRegistry.registry = new ContextMenuRegistry();
}

/**
* Singleton instance of this class. All interactions with this class should be
* done on this object.
*/
ContextMenuRegistry.registry = new ContextMenuRegistry();
export type ScopeType = ContextMenuRegistry.ScopeType;
cpcallen marked this conversation as resolved.
Show resolved Hide resolved
export const ScopeType = ContextMenuRegistry.ScopeType;
export type Scope = ContextMenuRegistry.Scope;
export type RegistryItem = ContextMenuRegistry.RegistryItem;
export type ContextMenuOption = ContextMenuRegistry.ContextMenuOption;
export type LegacyContextMenuOption =
ContextMenuRegistry.LegacyContextMenuOption;
31 changes: 16 additions & 15 deletions core/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ import {RenderedConnection} from './rendered_connection.js';
* @alias Blockly.Input
*/
export class Input {
static Align: AnyDuringMigration;
private sourceBlock_: Block;
fieldRow: Field[] = [];
align: number;
cpcallen marked this conversation as resolved.
Show resolved Hide resolved
align: Align;

/** Is the input visible? */
private visible_ = true;
Expand Down Expand Up @@ -238,11 +237,11 @@ export class Input {

/**
* Change the alignment of the connection's field(s).
* @param align One of the values of Align In RTL mode directions are
* reversed, and Align.RIGHT aligns to the left.
* @param align One of the values of Align. In RTL mode directions
* are reversed, and Align.RIGHT aligns to the left.
* @return The input being modified (to allow chaining).
*/
setAlign(align: number): Input {
setAlign(align: Align): Input {
this.align = align;
if (this.sourceBlock_.rendered) {
const sourceBlock = this.sourceBlock_ as BlockSvg;
Expand Down Expand Up @@ -303,15 +302,17 @@ export class Input {
}
}

/**
* Enum for alignment of inputs.
* @alias Blockly.Input.Align
*/
export enum Align {
LEFT = -1,
CENTRE,
RIGHT
export namespace Input {
/**
* Enum for alignment of inputs.
* @alias Blockly.Input.Align
*/
export enum Align {
LEFT = -1,
CENTRE = 0,
RIGHT = 1,
}
}

// Add Align to Input so that `Blockly.Input.Align` is publicly accessible.
Input.Align = Align;
export type Align = Input.Align;
export const Align = Input.Align;
40 changes: 20 additions & 20 deletions core/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {Workspace} from './workspace.js';
* @alias Blockly.Names
*/
export class Names {
static NameType: AnyDuringMigration;
static DEVELOPER_VARIABLE_TYPE: AnyDuringMigration;
static DEVELOPER_VARIABLE_TYPE: NameType;
cpcallen marked this conversation as resolved.
Show resolved Hide resolved
private readonly variablePrefix_: string;
private readonly reservedDict_: AnyDuringMigration;
private db_: {[key: string]: {[key: string]: string}};
Expand Down Expand Up @@ -250,26 +249,27 @@ export class Names {
}
}

/**
* Enum for the type of a name. Different name types may have different rules
* about collisions.
* When JavaScript (or most other languages) is generated, variable 'foo' and
* procedure 'foo' would collide. However, Blockly has no such problems since
* variable get 'foo' and procedure call 'foo' are unambiguous.
* Therefore, Blockly keeps a separate name type to disambiguate.
* getName('foo', 'VARIABLE') -> 'foo'
* getName('foo', 'PROCEDURE') -> 'foo2'
* @alias Blockly.Names.NameType
*/
export enum NameType {
DEVELOPER_VARIABLE = 'DEVELOPER_VARIABLE',
VARIABLE = 'VARIABLE',
PROCEDURE = 'PROCEDURE'
export namespace Names {
/**
* Enum for the type of a name. Different name types may have different rules
* about collisions.
* When JavaScript (or most other languages) is generated, variable 'foo' and
* procedure 'foo' would collide. However, Blockly has no such problems since
* variable get 'foo' and procedure call 'foo' are unambiguous.
* Therefore, Blockly keeps a separate name type to disambiguate.
* getName('foo', 'VARIABLE') -> 'foo'
* getName('foo', 'PROCEDURE') -> 'foo2'
* @alias Blockly.Names.NameType
*/
export enum NameType {
DEVELOPER_VARIABLE = 'DEVELOPER_VARIABLE',
VARIABLE = 'VARIABLE',
PROCEDURE = 'PROCEDURE',
}
}

// We have to export NameType here so that it is accessible under the old name
// `Blockly.Names.NameType`
Names.NameType = NameType;
export type NameType = Names.NameType;
export const NameType = Names.NameType;

/**
* Constant to separate developer variable names from user-defined variable
Expand Down
Loading