Skip to content

Conversation

@sdthaker
Copy link
Contributor

@sdthaker sdthaker commented Nov 7, 2023

The basics

The details

Resolves

Fixes #1978

Proposed Changes

Fixes these warnings (some of them were repeated warnings):

  • Unexpected any. Specify a different type - @typescript-eslint/no-explicit-any
  • Missing JSDoc @param "type" type - jsdoc/require-param-type
  • Missing JSDoc comment - jsdoc/require-jsdoc
  • JSDoc @returns declaration present but return expression not available in function - jsdoc/require-returns-check
  • Syntax error in type: MigrationData[]
  • Missing JSDoc @returns type
  • Missing JSDoc @param "identifier" description - jsdoc/require-param-description
  • Missing JSDoc @param "identifier" type
  • 'Minimap' is defined but never used - @typescript-eslint/no-unused-vars
  • JSDoc @returns declaration present but return expression not available in function - jsdoc/require-returns-check

Reason for Changes

These changes are done because aligning the code with the linting rules allows for the static analysis tool to catch any uncaught bugs at compile time.

Test Coverage

npm run test results:

image

npm run build results:

image

Documentation

Additional Information

The only warning left to be fixed is,

image

@sdthaker sdthaker requested a review from a team as a code owner November 7, 2023 20:00
@sdthaker sdthaker requested review from maribethb and removed request for a team November 7, 2023 20:00
@sdthaker sdthaker force-pushed the Fix-lint-warnings-in-blockly-samples-#1978 branch from 9b9369b to 0f7ff79 Compare November 7, 2023 20:07
@sdthaker sdthaker changed the title Resolved 15 ESLint Warnings: fix: resolved 15 ESLint warnings Nov 7, 2023
Copy link
Contributor

@maribethb maribethb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! Unfortunately getting the types correct is a bit trickier than it looks, but I've left some pointers below. Let me know if you have questions!


// TODO (#1211): Make this database format more robust.
/** @type {MigrationData[]} */
/** @type {Array.<MigrationData>} */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the . after Array so this should be Array<MigrationData>.

loadExtraState: function (
this: DynamicListCreateBlock,
state: {[x: string]: any} | string,
state: {[x: string]: number} | string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite right for the state types, here and below. The extraState could have any number of properties on it (we don't know) and it's not correct to assume all of those properties will be numbers (or strings, or whatever). We don't know the types. To be strict, we should type that as unknown rather than any.

I think the most correct would be

state: { itemCount?: number, [x: string]: unknown} | string,

This says the state can either be a string (when it's xml), or it can be an object with any number of properties whose type is unknown, and also it might have a property called itemCount that's a number. Then you'll get an error later because unknown is a stricter type than any. You'll need to change this.itemCount = state['itemCount']; to this.itemCount = state['itemCount'] ?? 0; to properly account for the case where itemCount does not exist in the extraState.

This might seem like a lot of work, but now the code will be truly type-safe instead of the current state which uses any to basically turn off type checking.

You would need a similar fix for the others below, but instead of itemCount it would be whatever property is actually used in the body of the function.

createPlayground(
document.getElementById('root'),
createWorkspace as (
blocklyDiv: HTMLElement,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this cast - createWorkspace already matches that type as far as I can tell. I think the original cast as any was maybe just unnecessary.

if (!demoConfig) {
done();
return;
return done();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct; we don't want to return the value of this function (I don't even know what that value would be, it's just arcane gulp magic).

If we're returning early then we actually shouldn't return a gulp task. The types here are super unimportant. I think you can change the return type to be {*} or maybe {Function | undefined}

* added explicit type as number to state parameter in loadExtraState()
* added explicit type as number to state parameter in loadExtraState()
* positioned variable declaration in jsdoc to its correct place
* added jsdoc comment for getScaledBBox() function
* added explicit false return statement instead of returning undefined
* jsdoc: updated type declaraion, added missing returns type, added missing identifier and its type
* slienced the warning based on jsdoc comment for object's class declaration
* slienced the warning based on jsdoc comment for object's class declaration
* slienced the warning based on jsdoc comment for object's class declaration
* removed unused import, updated function declaration in a function's arguments
* removed empty return statment, now returning result of done function call
* removed dot after Array declaration
* removed the explicit cast when passing createWorkspace as an argument
* restored done() invocation, updated return statement in jsdoc to return Function or undefined
* updated state type to include itemCount:number prop, added nullish coalescing for proper assignment
* updated state type to include itemCount:number prop, added nullish coalescing for proper assignment
@sdthaker sdthaker force-pushed the Fix-lint-warnings-in-blockly-samples-#1978 branch from 0f7ff79 to e2e3073 Compare November 11, 2023 02:39
@sdthaker
Copy link
Contributor Author

Thanks for this PR! Unfortunately getting the types correct is a bit trickier than it looks, but I've left some pointers below. Let me know if you have questions!

@maribethb Wow! This is amazing feedback! I've never received detailed feedback of this sort, considering I'm quite early in this career and new to open source! Thanks so much for taking the time to explain in detail what should be the code changes and the impact of those changes. I've made changes and pushed the updated code! Please take a look and let me know if anything else needs to be added/removed from this PR.

Copy link
Contributor

@maribethb maribethb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, this looks great. Thanks for your work on this!

@maribethb maribethb merged commit 2436337 into RaspberryPiFoundation:master Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix lint warnings in blockly-samples

2 participants