chore: use prettier instead of clang-format (#7014)

* chore: add and configure prettier

* chore: remove clang-format

* chore: remove clang-format config

* chore: lint additional ts files

* chore: fix lint errors in blocks

* chore: add prettier-ignore where needed

* chore: ignore js blocks when formatting

* chore: fix playground html syntax

* chore: fix yaml spacing from merge

* chore: convert text blocks to use arrow functions

* chore: format everything with prettier

* chore: fix lint unused imports in blocks
This commit is contained in:
Maribeth Bottorff
2023-05-10 16:01:39 -07:00
committed by GitHub
parent af991f5e1b
commit 88ff901a72
425 changed files with 29170 additions and 21169 deletions

View File

@@ -18,11 +18,15 @@ import * as utilsXml from '../utils/xml.js';
import type {Workspace} from '../workspace.js';
import * as Xml from '../xml.js';
import {BadConnectionCheck, MissingBlockType, MissingConnection, RealChildOfShadow} from './exceptions.js';
import {
BadConnectionCheck,
MissingBlockType,
MissingConnection,
RealChildOfShadow,
} from './exceptions.js';
import * as priorities from './priorities.js';
import * as serializationRegistry from './registry.js';
// TODO(#5160): Remove this once lint is fixed.
/* eslint-disable no-use-before-define */
@@ -30,8 +34,8 @@ import * as serializationRegistry from './registry.js';
* Represents the state of a connection.
*/
export interface ConnectionState {
shadow: State|undefined;
block: State|undefined;
shadow: State | undefined;
block: State | undefined;
}
/**
@@ -73,17 +77,20 @@ export interface State {
* @returns The serialized state of the block, or null if the block could not be
* serialied (eg it was an insertion marker).
*/
export function save(block: Block, {
addCoordinates = false,
addInputBlocks = true,
addNextBlocks = true,
doFullSerialization = true,
}: {
addCoordinates?: boolean,
addInputBlocks?: boolean,
addNextBlocks?: boolean,
doFullSerialization?: boolean
} = {}): State|null {
export function save(
block: Block,
{
addCoordinates = false,
addInputBlocks = true,
addNextBlocks = true,
doFullSerialization = true,
}: {
addCoordinates?: boolean;
addInputBlocks?: boolean;
addNextBlocks?: boolean;
doFullSerialization?: boolean;
} = {}
): State | null {
if (block.isInsertionMarker()) {
return null;
}
@@ -149,8 +156,10 @@ function saveAttributes(block: Block, state: State) {
if (!block.isOwnEditable()) {
state['editable'] = false;
}
if (block.inputsInline !== undefined &&
block.inputsInline !== block.inputsInlineDefault) {
if (
block.inputsInline !== undefined &&
block.inputsInline !== block.inputsInlineDefault
) {
state['inline'] = block.inputsInline;
}
// Data is a nullable string, so we don't need to worry about falsy values.
@@ -186,10 +195,10 @@ function saveExtraState(block: Block, state: State) {
} else if (block.mutationToDom) {
const extraState = block.mutationToDom();
if (extraState !== null) {
state['extraState'] =
Xml.domToText(extraState)
.replace(
' xmlns="https://developers.google.com/blockly/xml"', '');
state['extraState'] = Xml.domToText(extraState).replace(
' xmlns="https://developers.google.com/blockly/xml"',
''
);
}
}
}
@@ -247,13 +256,18 @@ function saveFields(block: Block, state: State, doFullSerialization: boolean) {
* @param doFullSerialization Whether or not to do full serialization.
*/
function saveInputBlocks(
block: Block, state: State, doFullSerialization: boolean) {
block: Block,
state: State,
doFullSerialization: boolean
) {
const inputs = Object.create(null);
for (let i = 0; i < block.inputList.length; i++) {
const input = block.inputList[i];
if (!input.connection) continue;
const connectionState =
saveConnection(input.connection as Connection, doFullSerialization);
const connectionState = saveConnection(
input.connection as Connection,
doFullSerialization
);
if (connectionState) {
inputs[input.name] = connectionState;
}
@@ -273,12 +287,17 @@ function saveInputBlocks(
* @param doFullSerialization Whether or not to do full serialization.
*/
function saveNextBlocks(
block: Block, state: State, doFullSerialization: boolean) {
block: Block,
state: State,
doFullSerialization: boolean
) {
if (!block.nextConnection) {
return;
}
const connectionState =
saveConnection(block.nextConnection, doFullSerialization);
const connectionState = saveConnection(
block.nextConnection,
doFullSerialization
);
if (connectionState) {
state['next'] = connectionState;
}
@@ -293,8 +312,10 @@ function saveNextBlocks(
* connected real block.
* @param doFullSerialization Whether or not to do full serialization.
*/
function saveConnection(connection: Connection, doFullSerialization: boolean):
ConnectionState|null {
function saveConnection(
connection: Connection,
doFullSerialization: boolean
): ConnectionState | null {
const shadow = connection.getShadowState(true);
const child = connection.targetBlock();
if (!shadow && !child) {
@@ -320,8 +341,10 @@ function saveConnection(connection: Connection, doFullSerialization: boolean):
* @returns The block that was just loaded.
*/
export function append(
state: State, workspace: Workspace,
{recordUndo = false}: {recordUndo?: boolean} = {}): Block {
state: State,
workspace: Workspace,
{recordUndo = false}: {recordUndo?: boolean} = {}
): Block {
return appendInternal(state, workspace, {recordUndo});
}
@@ -343,12 +366,18 @@ export function append(
* @internal
*/
export function appendInternal(
state: State, workspace: Workspace,
{parentConnection = undefined, isShadow = false, recordUndo = false}: {
parentConnection?: Connection,
isShadow?: boolean,
recordUndo?: boolean
} = {}): Block {
state: State,
workspace: Workspace,
{
parentConnection = undefined,
isShadow = false,
recordUndo = false,
}: {
parentConnection?: Connection;
isShadow?: boolean;
recordUndo?: boolean;
} = {}
): Block {
const prevRecordUndo = eventUtils.getRecordUndo();
eventUtils.setRecordUndo(recordUndo);
const existingGroup = eventUtils.getGroup();
@@ -393,9 +422,13 @@ export function appendInternal(
* @returns The block that was just appended.
*/
function appendPrivate(
state: State, workspace: Workspace,
{parentConnection = undefined, isShadow = false}:
{parentConnection?: Connection, isShadow?: boolean} = {}): Block {
state: State,
workspace: Workspace,
{
parentConnection = undefined,
isShadow = false,
}: {parentConnection?: Connection; isShadow?: boolean} = {}
): Block {
if (!state['type']) {
throw new MissingBlockType(state);
}
@@ -488,7 +521,10 @@ function loadExtraState(block: Block, state: State) {
* @param state The state which defines the given block
*/
function tryToConnectParent(
parentConnection: Connection|undefined, child: Block, state: State) {
parentConnection: Connection | undefined,
child: Block,
state: State
) {
if (!parentConnection) {
return;
}
@@ -505,7 +541,8 @@ function tryToConnectParent(
throw new MissingConnection('output', child, state);
}
connected = parentConnection.connect(childConnection);
} else { // Statement type.
} else {
// Statement type.
childConnection = child.previousConnection;
if (!childConnection) {
throw new MissingConnection('previous', child, state);
@@ -516,13 +553,17 @@ function tryToConnectParent(
if (!connected) {
const checker = child.workspace.connectionChecker;
throw new BadConnectionCheck(
checker.getErrorMessage(
checker.canConnectWithReason(
childConnection, parentConnection, false),
childConnection, parentConnection),
parentConnection.type === inputTypes.VALUE ? 'output connection' :
'previous connection',
child, state);
checker.getErrorMessage(
checker.canConnectWithReason(childConnection, parentConnection, false),
childConnection,
parentConnection
),
parentConnection.type === inputTypes.VALUE
? 'output connection'
: 'previous connection',
child,
state
);
}
}
@@ -573,7 +614,8 @@ function loadFields(block: Block, state: State) {
const field = block.getField(fieldName);
if (!field) {
console.warn(
`Ignoring non-existant field ${fieldName} in block ${block.type}`);
`Ignoring non-existant field ${fieldName} in block ${block.type}`
);
continue;
}
field.loadState(fieldState);
@@ -627,14 +669,18 @@ function loadNextBlocks(block: Block, state: State) {
* shadow block, or any connected real block.
*/
function loadConnection(
connection: Connection, connectionState: ConnectionState) {
connection: Connection,
connectionState: ConnectionState
) {
if (connectionState['shadow']) {
connection.setShadowState(connectionState['shadow']);
}
if (connectionState['block']) {
appendPrivate(
connectionState['block'], connection.getSourceBlock().workspace,
{parentConnection: connection});
connectionState['block'],
connection.getSourceBlock().workspace,
{parentConnection: connection}
);
}
}
@@ -687,18 +733,22 @@ export class BlockSerializer implements ISerializer {
* @returns The state of the workspace's blocks, or null if there are no
* blocks.
*/
save(workspace: Workspace): {languageVersion: number, blocks: State[]}|null {
save(
workspace: Workspace
): {languageVersion: number; blocks: State[]} | null {
const blockStates = [];
for (const block of workspace.getTopBlocks(false)) {
const state =
saveBlock(block, {addCoordinates: true, doFullSerialization: false});
const state = saveBlock(block, {
addCoordinates: true,
doFullSerialization: false,
});
if (state) {
blockStates.push(state);
}
}
if (blockStates.length) {
return {
'languageVersion': 0, // Currently unused.
'languageVersion': 0, // Currently unused.
'blocks': blockStates,
};
}
@@ -713,7 +763,9 @@ export class BlockSerializer implements ISerializer {
* @param workspace The workspace to deserialize into.
*/
load(
state: {languageVersion: number, blocks: State[]}, workspace: Workspace) {
state: {languageVersion: number; blocks: State[]},
workspace: Workspace
) {
const blockStates = state['blocks'];
for (const state of blockStates) {
append(state, workspace, {recordUndo: eventUtils.getRecordUndo()});

View File

@@ -10,7 +10,6 @@ goog.declareModuleId('Blockly.serialization.exceptions');
import type {Block} from '../block.js';
import type {State} from './blocks.js';
export class DeserializationError extends Error {}
/**
@@ -60,8 +59,11 @@ export class BadConnectionCheck extends DeserializationError {
* @internal
*/
constructor(
reason: string, childConnection: string, public childBlock: Block,
public childState: State) {
reason: string,
childConnection: string,
public childBlock: Block,
public childState: State
) {
super(`The block ${childBlock.toDevString()} could not connect its
${childConnection} to its parent, because: ${reason}`);
}

View File

@@ -7,7 +7,6 @@
import * as goog from '../../closure/goog/goog.js';
goog.declareModuleId('Blockly.serialization.priorities');
/**
* The priority for deserializing variables.
*/

View File

@@ -10,21 +10,24 @@ import type {ISerializer} from '../interfaces/i_serializer.js';
import * as priorities from './priorities.js';
import type {Workspace} from '../workspace.js';
/**
* Representation of a procedure data model.
*/
export interface State {
// TODO: This should also handle enabled.
id: string, name: string, returnTypes: string[]|null,
parameters?: ParameterState[],
id: string;
name: string;
returnTypes: string[] | null;
parameters?: ParameterState[];
}
/**
* Representation of a parameter data model.
*/
export interface ParameterState {
id: string, name: string, types?: string[],
id: string;
name: string;
types?: string[];
}
/**
@@ -34,8 +37,11 @@ export interface ParameterState {
* https://www.typescriptlang.org/docs/handbook/2/generics.html#using-class-types-in-generics
* for what is going on with this.
*/
type ProcedureModelConstructor<ProcedureModel extends IProcedureModel> =
new (workspace: Workspace, name: string, id: string) => ProcedureModel;
type ProcedureModelConstructor<ProcedureModel extends IProcedureModel> = new (
workspace: Workspace,
name: string,
id: string
) => ProcedureModel;
/**
* A newable signature for an IParameterModel.
@@ -44,9 +50,11 @@ type ProcedureModelConstructor<ProcedureModel extends IProcedureModel> =
* https://www.typescriptlang.org/docs/handbook/2/generics.html#using-class-types-in-generics
* for what is going on with this.
*/
type ParameterModelConstructor<ParameterModel extends IParameterModel> =
new (workspace: Workspace, name: string, id: string) => ParameterModel;
type ParameterModelConstructor<ParameterModel extends IParameterModel> = new (
workspace: Workspace,
name: string,
id: string
) => ParameterModel;
/**
* Serializes the given IProcedureModel to JSON.
@@ -84,18 +92,26 @@ export function saveParameter(param: IParameterModel): ParameterState {
*
* @internal
*/
export function
loadProcedure<ProcedureModel extends IProcedureModel,
ParameterModel extends IParameterModel>(
procedureModelClass: ProcedureModelConstructor<ProcedureModel>,
parameterModelClass: ParameterModelConstructor<ParameterModel>,
state: State, workspace: Workspace): ProcedureModel {
const proc = new procedureModelClass(workspace, state.name, state.id)
.setReturnTypes(state.returnTypes);
export function loadProcedure<
ProcedureModel extends IProcedureModel,
ParameterModel extends IParameterModel
>(
procedureModelClass: ProcedureModelConstructor<ProcedureModel>,
parameterModelClass: ParameterModelConstructor<ParameterModel>,
state: State,
workspace: Workspace
): ProcedureModel {
const proc = new procedureModelClass(
workspace,
state.name,
state.id
).setReturnTypes(state.returnTypes);
if (!state.parameters) return proc;
for (const [index, param] of state.parameters.entries()) {
proc.insertParameter(
loadParameter(parameterModelClass, param, workspace), index);
loadParameter(parameterModelClass, param, workspace),
index
);
}
return proc;
}
@@ -106,17 +122,21 @@ loadProcedure<ProcedureModel extends IProcedureModel,
* @internal
*/
export function loadParameter<ParameterModel extends IParameterModel>(
parameterModelClass: ParameterModelConstructor<ParameterModel>,
state: ParameterState, workspace: Workspace): ParameterModel {
parameterModelClass: ParameterModelConstructor<ParameterModel>,
state: ParameterState,
workspace: Workspace
): ParameterModel {
const model = new parameterModelClass(workspace, state.name, state.id);
if (state.types) model.setTypes(state.types);
return model;
}
/** Serializer for saving and loading procedure state. */
export class ProcedureSerializer<ProcedureModel extends IProcedureModel,
ParameterModel extends
IParameterModel> implements ISerializer {
export class ProcedureSerializer<
ProcedureModel extends IProcedureModel,
ParameterModel extends IParameterModel
> implements ISerializer
{
public priority = priorities.PROCEDURES;
/**
@@ -131,15 +151,16 @@ export class ProcedureSerializer<ProcedureModel extends IProcedureModel,
* you want this serializer to deserialize.
*/
constructor(
private readonly procedureModelClass:
ProcedureModelConstructor<ProcedureModel>,
private readonly parameterModelClass:
ParameterModelConstructor<ParameterModel>) {}
private readonly procedureModelClass: ProcedureModelConstructor<ProcedureModel>,
private readonly parameterModelClass: ParameterModelConstructor<ParameterModel>
) {}
/** Serializes the procedure models of the given workspace. */
save(workspace: Workspace): State[]|null {
const save = workspace.getProcedureMap().getProcedures().map(
(proc) => saveProcedure(proc));
save(workspace: Workspace): State[] | null {
const save = workspace
.getProcedureMap()
.getProcedures()
.map((proc) => saveProcedure(proc));
return save.length ? save : null;
}
@@ -150,9 +171,14 @@ export class ProcedureSerializer<ProcedureModel extends IProcedureModel,
load(state: State[], workspace: Workspace) {
const map = workspace.getProcedureMap();
for (const procState of state) {
map.add(loadProcedure(
this.procedureModelClass, this.parameterModelClass, procState,
workspace));
map.add(
loadProcedure(
this.procedureModelClass,
this.parameterModelClass,
procState,
workspace
)
);
}
}

View File

@@ -11,7 +11,6 @@ goog.declareModuleId('Blockly.serialization.registry');
import type {ISerializer} from '../interfaces/i_serializer.js';
import * as registry from '../registry.js';
/**
* Registers the given serializer so that it can be used for serialization and
* deserialization.

View File

@@ -13,14 +13,13 @@ import type {Workspace} from '../workspace.js';
import * as priorities from './priorities.js';
import * as serializationRegistry from './registry.js';
/**
* Represents the state of a given variable.
*/
export interface State {
name: string;
id: string;
type: string|undefined;
type: string | undefined;
}
/**
@@ -42,7 +41,7 @@ export class VariableSerializer implements ISerializer {
* @returns The state of the workspace's variables, or null if there are no
* variables.
*/
save(workspace: Workspace): State[]|null {
save(workspace: Workspace): State[] | null {
const variableStates = [];
for (const variable of workspace.getAllVariables()) {
const state = {
@@ -56,8 +55,9 @@ export class VariableSerializer implements ISerializer {
}
// AnyDuringMigration because: Type '{ name: string; id: string; }[] |
// null' is not assignable to type 'State[] | null'.
return (variableStates.length ? variableStates : null) as
AnyDuringMigration;
return (
variableStates.length ? variableStates : null
) as AnyDuringMigration;
}
/**
@@ -70,7 +70,10 @@ export class VariableSerializer implements ISerializer {
load(state: State[], workspace: Workspace) {
for (const varState of state) {
workspace.createVariable(
varState['name'], varState['type'], varState['id']);
varState['name'],
varState['type'],
varState['id']
);
}
}

View File

@@ -15,15 +15,15 @@ import * as dom from '../utils/dom.js';
import type {Workspace} from '../workspace.js';
import {WorkspaceSvg} from '../workspace_svg.js';
/**
* Returns the state of the workspace as a plain JavaScript object.
*
* @param workspace The workspace to serialize.
* @returns The serialized state of the workspace.
*/
export function save(workspace: Workspace):
{[key: string]: AnyDuringMigration} {
export function save(workspace: Workspace): {
[key: string]: AnyDuringMigration;
} {
const state = Object.create(null);
const serializerMap = registry.getAllItems(registry.Type.SERIALIZER, true);
for (const key in serializerMap) {
@@ -44,17 +44,18 @@ export function save(workspace: Workspace):
* undo-able by the user. False by default.
*/
export function load(
state: {[key: string]: AnyDuringMigration}, workspace: Workspace,
{recordUndo = false}: {recordUndo?: boolean} = {}) {
state: {[key: string]: AnyDuringMigration},
workspace: Workspace,
{recordUndo = false}: {recordUndo?: boolean} = {}
) {
const serializerMap = registry.getAllItems(registry.Type.SERIALIZER, true);
if (!serializerMap) {
return;
}
const deserializers = Object.entries(serializerMap)
.sort(
(a, b) => (b[1] as ISerializer)!.priority -
(a[1] as ISerializer)!.priority);
const deserializers = Object.entries(serializerMap).sort(
(a, b) => (b[1] as ISerializer)!.priority - (a[1] as ISerializer)!.priority
);
const prevRecordUndo = eventUtils.getRecordUndo();
eventUtils.setRecordUndo(recordUndo);