Skip to content

Commit 8ce8611

Browse files
authored
Merge pull request wpilibsuite#98 from lizlooney/pr_warnings
Fixed all warnings except one
2 parents f829a0e + a31fd46 commit 8ce8611

23 files changed

+240
-206
lines changed

src/App.tsx

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
Button,
66
ConfigProvider,
77
Flex,
8-
Input,
98
message,
109
Popconfirm,
1110
Space,
@@ -16,7 +15,6 @@ import {
1615
theme,
1716
Upload
1817
} from 'antd';
19-
import type { InputRef } from 'antd';
2018
import type { TreeDataNode, TreeProps } from 'antd';
2119
import type { UploadProps } from 'antd';
2220
import {
@@ -55,8 +53,6 @@ import * as editor from './editor/editor';
5553
import { extendedPythonGenerator } from './editor/extended_python_generator';
5654
import { createGeneratorContext, GeneratorContext } from './editor/generator_context';
5755

58-
import * as toolboxItems from './toolbox/items';
59-
import * as toolbox from './toolbox/toolbox';
6056
//import { testAllBlocksInToolbox } from './toolbox/toolbox_tests';
6157
import ToolboxSettingsModal from './toolbox/settings';
6258

@@ -173,6 +169,9 @@ const App: React.FC = () => {
173169
}, [storage]);
174170

175171
const fetchMostRecentModulePath = async () => {
172+
if (!storage) {
173+
return;
174+
}
176175
try {
177176
const value = await storage.fetchEntry('mostRecentModulePath', '');
178177
setMostRecentModulePath(value);
@@ -183,6 +182,9 @@ const App: React.FC = () => {
183182
};
184183

185184
const initializeShownPythonToolboxCategories = async () => {
185+
if (!storage) {
186+
return;
187+
}
186188
try {
187189
const value = await storage.fetchEntry('shownPythonToolboxCategories', '[]');
188190
const shownCategories: string[] = JSON.parse(value);
@@ -206,6 +208,10 @@ const App: React.FC = () => {
206208

207209
const fetchListOfModules = async (): Promise<commonStorage.Project[]> => {
208210
return new Promise(async (resolve, reject) => {
211+
if (!storage) {
212+
reject(new Error());
213+
return;
214+
}
209215
try {
210216
const array = await storage.listModules();
211217
setModules(array)
@@ -348,7 +354,7 @@ const App: React.FC = () => {
348354
}
349355
if (currentModule && blocklyComponent.current && generatorContext.current) {
350356
const blocklyWorkspace = blocklyComponent.current.getBlocklyWorkspace();
351-
setGeneratedCode(extendedPythonGenerator.workspaceToCode(
357+
setGeneratedCode(extendedPythonGenerator.mrcWorkspaceToCode(
352358
blocklyWorkspace, generatorContext.current));
353359
} else {
354360
setGeneratedCode('');
@@ -462,6 +468,9 @@ const App: React.FC = () => {
462468
};
463469

464470
const handleNewProjectNameOk = async (newProjectClassName: string) => {
471+
if (!storage || !currentModule) {
472+
return;
473+
}
465474
const newProjectName = commonStorage.classNameToModuleName(newProjectClassName);
466475
const newProjectPath = commonStorage.makeProjectPath(newProjectName);
467476
if (newProjectNameModalPurpose === PURPOSE_NEW_PROJECT) {
@@ -553,7 +562,10 @@ const App: React.FC = () => {
553562
};
554563

555564
const handleNewModuleNameOk = async (newModuleClassName: string) => {
556-
const newModuleName = commonStorage.classNameToModuleName(newModuleClassName);
565+
if (!storage || !currentModule) {
566+
return;
567+
}
568+
const newModuleName = commonStorage.classNameToModuleName(newModuleClassName);
557569
const newModulePath = commonStorage.makeModulePath(currentModule.projectName, newModuleName);
558570
if (newModuleNameModalPurpose === PURPOSE_NEW_MECHANISM) {
559571
const mechanismContent = commonStorage.newMechanismContent(
@@ -612,26 +624,30 @@ const App: React.FC = () => {
612624
};
613625

614626
const handleSaveClicked = async () => {
615-
saveBlocks((success) => {});
627+
saveBlocks();
616628
};
617629

618-
const saveBlocks = async (): boolean => {
619-
if (blocksEditor.current && currentModulePath) {
630+
const saveBlocks = async (): Promise<boolean> => {
631+
return new Promise(async (resolve, reject) => {
632+
if (!blocksEditor.current || !currentModulePath) {
633+
reject(new Error());
634+
return;
635+
}
620636
try {
621637
await blocksEditor.current.saveBlocks();
622638
messageApi.open({
623639
type: 'success',
624640
content: 'Save completed successfully.',
625641
});
626-
return true;
627-
} catch (e: Error) {
642+
resolve(true);
643+
} catch (e) {
628644
console.log('Failed to save the blocks. Caught the following error...');
629645
console.log(e);
630646
setAlertErrorMessage('Failed to save the blocks.');
631647
setAlertErrorVisible(true);
648+
reject(new Error('Failed to save the blocks.'));
632649
}
633-
}
634-
return false;
650+
});
635651
};
636652

637653
const handleRenameClicked = () => {
@@ -715,7 +731,7 @@ const App: React.FC = () => {
715731
afterPopconfirmOk.current = () => {
716732
setOpenPopconfirm(false);
717733
checkIfBlocksWereModified(async () => {
718-
if (!currentModule) {
734+
if (!storage || !currentModule) {
719735
return;
720736
}
721737
if (currentModule.moduleType == commonStorage.MODULE_TYPE_PROJECT) {
@@ -779,40 +795,48 @@ const App: React.FC = () => {
779795
}
780796
return isBlocks || Upload.LIST_IGNORE;
781797
},
782-
onChange: (info) => {
783-
},
784798
customRequest: ({ file, onSuccess, onError }) => {
799+
if (!onSuccess || !onError) {
800+
return;
801+
}
802+
const fileObject = file as File;
785803
const reader = new FileReader();
786804
reader.onload = async (event) => {
787-
const dataUrl = event.target.result;
788-
const uploadProjectName = commonStorage.makeUploadProjectName(file.name, getProjectNames());
805+
const dataUrl = event.target?.result;
806+
if (!storage || !dataUrl) {
807+
return;
808+
}
809+
const uploadProjectName = commonStorage.makeUploadProjectName(fileObject.name, getProjectNames());
789810
try {
790-
await storage.uploadProject(uploadProjectName, dataUrl);
811+
await storage.uploadProject(uploadProjectName, dataUrl as string);
791812
onSuccess('Upload successful');
792813
await fetchListOfModules();
793814
const uploadProjectPath = commonStorage.makeProjectPath(uploadProjectName);
794815
setCurrentModulePath(uploadProjectPath);
795816
} catch (e) {
796817
console.log('Failed to upload the project. Caught the following error...');
797818
console.log(e);
798-
onError('Failed to upload the project.');
819+
onError(new Error('Failed to upload the project.'));
799820
setAlertErrorMessage('Failed to upload the project');
800821
setAlertErrorVisible(true);
801822
}
802823
};
803824
reader.onerror = (error) => {
804825
console.log('Failed to upload the project. reader.onerror called with the following error...');
805826
console.log(error);
806-
onError('Failed to upload the project.');
827+
onError(new Error('Failed to upload the project.'));
807828
setAlertErrorMessage('Failed to upload the project');
808829
setAlertErrorVisible(true);
809830
};
810-
reader.readAsDataURL(file);
831+
reader.readAsDataURL(fileObject);
811832
},
812833
};
813834

814835
const handleDownloadClicked = () => {
815836
checkIfBlocksWereModified(async () => {
837+
if (!storage || !currentModule) {
838+
return;
839+
}
816840
try {
817841
const url = await storage.downloadProject(currentModule.projectName);
818842
const link = document.createElement('a');
@@ -833,13 +857,16 @@ const App: React.FC = () => {
833857
};
834858

835859
const handleToolboxSettingsOk = async (updatedShownCategories: Set<string>) => {
860+
if (!storage) {
861+
return;
862+
}
836863
setShownPythonToolboxCategories(updatedShownCategories);
837864
const array = Array.from(updatedShownCategories);
838865
array.sort();
839866
storage.saveEntry('shownPythonToolboxCategories', JSON.stringify(array));
840867
};
841868

842-
const handleModuleSelected: TreeProps['onSelect'] = (a: React.Key[], e) => {
869+
const handleModuleSelected: TreeProps['onSelect'] = (a: React.Key[]) => {
843870
if (a.length === 1) {
844871
checkIfBlocksWereModified(() => {
845872
setTreeSelectedKey(a[0]);

src/blocks/mrc_call_python_function.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
import * as Blockly from 'blockly';
24-
import { Order, PythonGenerator } from 'blockly/python';
24+
import { Order } from 'blockly/python';
2525

2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
@@ -58,7 +58,7 @@ interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
5858
mrcImportModule: string,
5959
mrcActualFunctionName: string,
6060
mrcExportedFunction: boolean,
61-
renameMethod(this: CallPythonFunctionBlock, oldName: string, newName: string): void;
61+
renameMethod(this: CallPythonFunctionBlock, newName: string): void;
6262
mutateMethod(this: CallPythonFunctionBlock, defBlockExtraState: ClassMethodDefExtraState): void;
6363
}
6464
type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
@@ -139,7 +139,7 @@ const CALL_PYTHON_FUNCTION = {
139139
break;
140140
}
141141
default:
142-
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
142+
throw new Error('mrcFunctionKind has unexpected value: ' + this.mrcFunctionKind)
143143
}
144144
const funcTooltip = this.mrcTooltip;
145145
if (funcTooltip) {
@@ -261,7 +261,7 @@ const CALL_PYTHON_FUNCTION = {
261261
break;
262262
}
263263
default:
264-
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
264+
throw new Error('mrcFunctionKind has unexpected value: ' + this.mrcFunctionKind)
265265
}
266266
}
267267

@@ -305,7 +305,7 @@ const CALL_PYTHON_FUNCTION = {
305305
defBlockExtraState.params.forEach((param) => {
306306
this.mrcArgs.push({
307307
'name': param.name,
308-
'type': param.type,
308+
'type': param.type ?? '',
309309
});
310310
});
311311
this.updateBlock_();
@@ -345,7 +345,6 @@ export const pythonFromBlock = function(
345345
break;
346346
}
347347
case FunctionKind.CONSTRUCTOR: {
348-
const callPythonFunctionBlock = block as CallPythonFunctionBlock;
349348
const className = block.getFieldValue(pythonUtils.FIELD_MODULE_OR_CLASS_NAME);
350349
code = className;
351350
break;
@@ -367,7 +366,7 @@ export const pythonFromBlock = function(
367366
break;
368367
}
369368
default:
370-
throw new Error('mrcFunctionKind has unexpected value: ' + mrcFunctionKind)
369+
throw new Error('mrcFunctionKind has unexpected value: ' + callPythonFunctionBlock.mrcFunctionKind)
371370
}
372371
code += '(' + generateCodeForArguments(callPythonFunctionBlock, generator, argStartIndex) + ')';
373372
if (block.outputConnection) {

src/blocks/mrc_class_method_def.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,12 @@ export const BLOCK_NAME = 'mrc_class_method_def';
3333
const MUTATOR_BLOCK_NAME = 'methods_mutatorarg';
3434
const PARAM_CONTAINER_BLOCK_NAME = 'method_param_container';
3535

36-
class MethodInput extends Blockly.inputs.Input {
37-
readonly type = Blockly.inputs.inputTypes.CUSTOM;
38-
constructor(name: string, block: Blockly.Block) {
39-
super(name, block);
40-
}
41-
}
42-
4336
export type Parameter = {
4437
name: string,
4538
type?: string,
4639
};
4740

48-
type ClassMethodDefBlock = Blockly.Block & ClassMethodDefMixin & Blockly.BlockSvg;
41+
export type ClassMethodDefBlock = Blockly.Block & ClassMethodDefMixin & Blockly.BlockSvg;
4942
interface ClassMethodDefMixin extends ClassMethodDefMixinType {
5043
mrcCanChangeSignature: boolean,
5144
mrcCanBeCalledWithinClass: boolean,
@@ -57,7 +50,7 @@ interface ClassMethodDefMixin extends ClassMethodDefMixinType {
5750
type ClassMethodDefMixinType = typeof CLASS_METHOD_DEF;
5851

5952
/** Extra state for serialising call_python_* blocks. */
60-
type ClassMethodDefExtraState = {
53+
export type ClassMethodDefExtraState = {
6154
/**
6255
* Can change name and parameters and return type
6356
*/
@@ -178,7 +171,7 @@ const CLASS_METHOD_DEF = {
178171

179172
let paramBlock = containerBlock.getInputTargetBlock('STACK');
180173
while (paramBlock && !paramBlock.isInsertionMarker()) {
181-
let param : Parameter = {
174+
const param : Parameter = {
182175
name : paramBlock.getFieldValue('NAME'),
183176
type : ''
184177
}
@@ -192,13 +185,13 @@ const CLASS_METHOD_DEF = {
192185
decompose: function (this: ClassMethodDefBlock, workspace: Blockly.Workspace) {
193186
// This is a special sub-block that only gets created in the mutator UI.
194187
// It acts as our "top block"
195-
let topBlock = workspace.newBlock(PARAM_CONTAINER_BLOCK_NAME);
188+
const topBlock = workspace.newBlock(PARAM_CONTAINER_BLOCK_NAME);
196189
(topBlock as Blockly.BlockSvg).initSvg();
197190

198191
// Then we add one sub-block for each item in the list.
199-
var connection = topBlock!.getInput('STACK')!.connection;
192+
let connection = topBlock!.getInput('STACK')!.connection;
200193

201-
for (var i = 0; i < this.mrcParameters.length; i++) {
194+
for (let i = 0; i < this.mrcParameters.length; i++) {
202195
let itemBlock = workspace.newBlock(MUTATOR_BLOCK_NAME);
203196
(itemBlock as Blockly.BlockSvg).initSvg();
204197
itemBlock.setFieldValue(this.mrcParameters[i].name, 'NAME')
@@ -237,7 +230,7 @@ const CLASS_METHOD_DEF = {
237230

238231
const legalName = findLegalMethodName(name, this);
239232
const oldName = nameField.getValue();
240-
if (oldName !== name && oldName !== legalName) {
233+
if (oldName && oldName !== name && oldName !== legalName) {
241234
// Rename any callers.
242235
renameMethodCallers(this.workspace, oldName, legalName);
243236
}
@@ -282,7 +275,7 @@ function findLegalMethodName(name: string, block: ClassMethodDefBlock): string {
282275
* @returns True if the name is used, otherwise return false.
283276
*/
284277
function isMethodNameUsed(
285-
name: string, workspace: Workspace, opt_exclude?: Block): boolean {
278+
name: string, workspace: Blockly.Workspace, opt_exclude?: Blockly.Block): boolean {
286279
const nameLowerCase = name.toLowerCase();
287280
for (const block of workspace.getBlocksByType('mrc_class_method_def')) {
288281
if (block === opt_exclude) {
@@ -291,8 +284,9 @@ function isMethodNameUsed(
291284
if (nameLowerCase === block.getFieldValue('NAME').toLowerCase()) {
292285
return true;
293286
}
294-
if (block.mrcPythonMethodName &&
295-
nameLowerCase === block.mrcPythonMethodName.toLowerCase()) {
287+
const classMethodDefBlock = block as ClassMethodDefBlock;
288+
if (classMethodDefBlock.mrcPythonMethodName &&
289+
nameLowerCase === classMethodDefBlock.mrcPythonMethodName.toLowerCase()) {
296290
return true;
297291
}
298292
}
@@ -315,11 +309,10 @@ interface MethodMutatorArgMixin extends MethodMutatorArgMixinType{
315309
type MethodMutatorArgMixinType = typeof METHODS_MUTATORARG;
316310

317311
function setName(block: Blockly.BlockSvg){
318-
let parentBlock = ChangeFramework.getParentOfType(block, PARAM_CONTAINER_BLOCK_NAME);
312+
const parentBlock = ChangeFramework.getParentOfType(block, PARAM_CONTAINER_BLOCK_NAME);
319313
if (parentBlock) {
320-
let done = false;
321-
let variableBlocks = parentBlock!.getDescendants(true)
322-
let otherNames: string[] = []
314+
const variableBlocks = parentBlock!.getDescendants(true)
315+
const otherNames: string[] = []
323316
variableBlocks?.forEach(function (variableBlock) {
324317
if (variableBlock != block) {
325318
otherNames.push(variableBlock.getFieldValue('NAME'));
@@ -465,7 +458,7 @@ export const pythonFromBlock = function (
465458
xfix2 = xfix1;
466459
}
467460
if(block.mrcPythonMethodName == '__init__'){
468-
branch = generator.defineClassVariables(block.workspace) + branch;
461+
branch = generator.defineClassVariables() + branch;
469462
}
470463
if (returnValue) {
471464
returnValue = generator.INDENT + 'return ' + returnValue + '\n';

src/blocks/mrc_get_python_enum_value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
import * as Blockly from 'blockly';
24-
import { Order, PythonGenerator } from 'blockly/python';
24+
import { Order } from 'blockly/python';
2525

2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldDropdown } from '../fields/FieldDropdown';

src/blocks/mrc_get_python_variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
import * as Blockly from 'blockly';
24-
import { Order, PythonGenerator } from 'blockly/python';
24+
import { Order } from 'blockly/python';
2525

2626
import * as pythonUtils from './utils/generated/python';
2727
import { createFieldDropdown } from '../fields/FieldDropdown';

0 commit comments

Comments
 (0)