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

chore: enable strict mode for workspace-minimap #2078

Merged
merged 13 commits into from
Nov 29, 2023

Conversation

Namatuzio
Copy link
Contributor

@Namatuzio Namatuzio commented Nov 18, 2023

The basics

The details

Resolves

Fixes #2031

Proposed Changes

  • Updates the workspace-minimap plugin's type checks in accordance with TypeScript's strict-mode
  • Updated the following files:
    • plugins\workspace-minimap\src\minimap.ts
    • plugins\workspace-minimap\src\focus_region.ts
    • plugins\workspace-minimap\test\index.ts
    • plugins\workspace-minimap\test\minimap_tests.mocha.js

Reason for Changes

Strict mode is a great way to ensure strong code consistency and maintain high code standards. It allows for fewer faults within code to be possible by patching potential oversights.

Test Coverage

npm run test results:
image

Documentation

Additional Information

There are 2 warnings when running npm run test from a change I added. For some reason when I ran the tests, the tests wouldn't have access to the document object part of the DOM, so I added the following to the top of the tester file which seemed to have fixed it:

const jsdom = require('jsdom');
const { JSDOM } = jsdom;

const { document } = (new JSDOM('')).window;
global.document = document;

Namatuzio and others added 8 commits November 16, 2023 18:49
Added various type checks and changes to focus_region.ts which follows proper standards for the TypeScript "strict-mode" compiler checks.
Fixed all type error in ordinance with TypeScript's "strict-mode"
Fully updated and fixed all type errors after enabling strict-mode.
@Namatuzio Namatuzio requested a review from a team as a code owner November 18, 2023 22:43
@Namatuzio Namatuzio requested review from NeilFraser and removed request for a team November 18, 2023 22:43
Copy link

google-cla bot commented Nov 18, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@BeksOmega BeksOmega assigned BeksOmega and unassigned NeilFraser Nov 20, 2023
@BeksOmega BeksOmega requested review from BeksOmega and removed request for NeilFraser November 20, 2023 16:54
Copy link
Contributor

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

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

Thank you for your work on this! This looks like a great start :D

plugins/workspace-minimap/src/focus_region.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/focus_region.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/focus_region.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/focus_region.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/minimap.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/minimap.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/positioned_minimap.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/test/minimap_tests.mocha.js Outdated Show resolved Hide resolved
@Namatuzio
Copy link
Contributor Author

Amazing feedback, thank you! I'll get right on the changes needed to be made!

@Namatuzio
Copy link
Contributor Author

I updated the PR as per the requested changes! As always, let me know if anything else needs to be further updated, thanks!

plugins/workspace-minimap/src/focus_region.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/positioned_minimap.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/minimap.ts Outdated Show resolved Hide resolved
plugins/workspace-minimap/src/minimap.ts Outdated Show resolved Hide resolved
@Namatuzio
Copy link
Contributor Author

@BeksOmega I added the requested changes!

  • primaryInjectParentDiv's initialization was added to the top of the init() method along with the null check
  • minimapWorkspace and focusRegion were made nullable and null checks were added where necessary
  • Formatting updates and newlines where needed

@BeksOmega
Copy link
Contributor

There are 2 warnings when running npm run test from a change I added. For some reason when I ran the tests, the tests wouldn't have access to the document object part of the DOM, so I added the following to the top of the tester file which seemed to have fixed it:

When I tested removing the JSDOM code locally, I didn't receive any errors :/ Could you post back with the errors you're receiving? Hopefully with a bit more info we can figure out what's up!

@Namatuzio
Copy link
Contributor Author

There are 2 warnings when running npm run test from a change I added. For some reason when I ran the tests, the tests wouldn't have access to the document object part of the DOM, so I added the following to the top of the tester file which seemed to have fixed it:

When I tested removing the JSDOM code locally, I didn't receive any errors :/ Could you post back with the errors you're receiving? Hopefully with a bit more info we can figure out what's up!

Of course! So funnily enough, yesterday when I tested it, it was working perfectly fine without the code, which confused me.

I decided to make another local branch on my machine to test it out. I added some of the changes from the PR and luckily I was able to reproduce it:

  1) Positioning the minimap in the primary workspace
       LTR Vertical Start:
     ReferenceError: document is not defined
      at measureFontMetrics$$module$build$src$core$utils$dom (webpack-internal:///./node_modules/blockly/blockly_compressed.js:98:79)
      at ConstantProvider$$module$build$src$core$renderers$geras$constants.setFontConstants_ (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1001:478)
      at ConstantProvider$$module$build$src$core$renderers$geras$constants.setDynamicProperties_ (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1001:93)
      at ConstantProvider$$module$build$src$core$renderers$geras$constants.setTheme (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1001:38)
      at Renderer$$module$build$src$core$renderers$geras$renderer.init (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1181:684)
      at Renderer$$module$build$src$core$renderers$geras$renderer.init (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1511:379)
      at init$$module$build$src$core$renderers$common$block_rendering (webpack-internal:///./node_modules/blockly/blockly_compressed.js:334:181)
      at new WorkspaceSvg$$module$build$src$core$workspace_svg (webpack-internal:///./node_modules/blockly/blockly_compressed.js:1367:131)
      at new Minimap (webpack-internal:///./src/minimap.ts:45:33)
      at new PositionedMinimap (webpack-internal:///./src/positioned_minimap.ts:30:9)

I took a look at the errors that were being thrown and realized it was stemming from minimap.ts so I went to the line that was causing it and found the culprit!

  protected primaryWorkspace: Blockly.WorkspaceSvg;
  protected minimapWorkspace: Blockly.WorkspaceSvg;
  protected focusRegion: FocusRegion;
  protected onMouseMoveWrapper: Blockly.browserEvents.Data | null = null;
  protected onMouseDownWrapper: Blockly.browserEvents.Data | null = null;
  protected onMouseUpWrapper: Blockly.browserEvents.Data | null = null;
  protected minimapWrapper: HTMLDivElement;

  /**
   * Constructor for a minimap.
   *
   * @param workspace The workspace to mirror.
   */
  constructor(workspace: Blockly.WorkspaceSvg) {
    this.primaryWorkspace = workspace;
    this.minimapWorkspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
    this.focusRegion = new FocusRegion(this.primaryWorkspace, this.minimapWorkspace);
    this.minimapWrapper = document.createElement('div');
  }

It was all caused by the old way I handled the constructor! This makes sense as to why in my initial PR, I noted the issue.

Of course with the new changes to minimap.ts this error does not exist!

  protected primaryWorkspace: Blockly.WorkspaceSvg;
  protected minimapWorkspace: Blockly.WorkspaceSvg | null = null;
  protected focusRegion: FocusRegion | null = null;
  protected onMouseMoveWrapper: Blockly.browserEvents.Data | null = null;
  protected onMouseDownWrapper: Blockly.browserEvents.Data | null = null;
  protected onMouseUpWrapper: Blockly.browserEvents.Data | null = null;
  protected minimapWrapper: HTMLDivElement | null = null;

  /**
   * Constructor for a minimap.
   *
   * @param workspace The workspace to mirror.
   */
  constructor(workspace: Blockly.WorkspaceSvg) {
    this.primaryWorkspace = workspace;
  }

I'll make some changes to the PR once more, removing the tester file code that I added so we can get this merged. Thanks again @BeksOmega for the opportunity to work on this!

Removed the JSDOM code found in `plugins/workspace-minimap/test/minimap-test.mocha.js` as it was unnecessary due to updates to the code.
@BeksOmega
Copy link
Contributor

Sweet this looks great @Namatuzio Thank you for your hard work on this :D You just need to run npm run format and commit the changes, then this should be good to go!

@Namatuzio
Copy link
Contributor Author

Sweet this looks great @Namatuzio Thank you for your hard work on this :D You just need to run npm run format and commit the changes, then this should be good to go!

I formatted the specific files that were causing errors in the build so everything should work just fine now. Thanks a ton!

@BeksOmega BeksOmega merged commit 4bfb6d4 into google:master Nov 29, 2023
8 checks passed
shivalipatel6 pushed a commit to shivalipatel6/blockly-samples that referenced this pull request Dec 4, 2023
* Fixed workscpace-minimap/focus_region.ts type errors

Added various type checks and changes to focus_region.ts which follows proper standards for the TypeScript "strict-mode" compiler checks.

* fixed type errors in workspace-minimap/minimap.ts

Fixed all type error in ordinance with TypeScript's "strict-mode"

* Updated workspace-minimap to utilize TS strict-mode

Fully updated and fixed all type errors after enabling strict-mode.

* Updated index.ts to follow strict-mode rules

* fix lint errors in index.ts

* Updated testing file to coordinate with strict-mode

* Added requested changes

Added the changes request under the PR

* Added feedback changes

* Removed JSDOM test code

Removed the JSDOM code found in `plugins/workspace-minimap/test/minimap-test.mocha.js` as it was unnecessary due to updates to the code.

* format files using prettier
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.

Enable strict mode for workspace-minimap
3 participants