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

[ES] Migrate from JS to TS #135

Closed
wants to merge 49 commits into from

Conversation

dchege711
Copy link
Owner

Better typing to catch mistakes earlier and refactor with more confidence.

@render
Copy link

render bot commented Jul 2, 2023

@gitguardian
Copy link

gitguardian bot commented Jul 2, 2023

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
Once a secret has been leaked into a git repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Our GitHub checks need improvements? Share your feedbacks!

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@dchege711
Copy link
Owner Author

Works on #67

Moved everything into a src/ folder, and added a tsconfig.json so that I
can run `tsc` from the project root. There are a bunch of errors though.

[1]: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
Used the POSIX rename utility for the move.

```zsh
brew install rename
rename "s/\.js$/\.ts/" **/*.js
```

Updates to WebPack adopted from [1]. Then ran:

npx webpack --config webpack.config.js

... which produced the changes in public/dist.

[1]: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#webpack
The app is crashing and the tests are failing though.
Fixes the "Could not find metadata document" error in tests.
This is not the proper fix as there may be data loss. Revisit this
before checking in.

```log
/Users/dchege711/study_buddy/node_modules/mongoose/lib/model.js:486
    parallelSave = new ParallelSaveError(this);
                   ^

ParallelSaveError: Can't save() the same doc multiple times in parallel. Document: 64a09467f8e97e57a99a49fb
    at Model.save (/Users/dchege711/study_buddy/node_modules/mongoose/lib/model.js:486:20)
    at updateMetadataWithCardDetails (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:545:24)
    at Object.<anonymous> (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:171:62)
    at step (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:56:23)
    at Object.next (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:37:53)
    at /Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:31:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:27:12)
    at /Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:168:70
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:168:32)
    at step (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:56:23)
    at Object.next (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:37:53)
    at fulfilled (/Users/dchege711/study_buddy/dist/models/MetadataMongoDB.js:28:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
```
Much better stack traces, e.g.:

```log
TypeError: Cannot read properties of undefined (reading 'sample_card')
      at /Users/dchege711/study_buddy/src/models/MetadataMongoDB.ts:149:55
      at Array.forEach (<anonymous>)
      at _loop_1 (src/models/MetadataMongoDB.ts:147:40)
      at Object.<anonymous> (src/models/MetadataMongoDB.ts:240:25)
      at step (src/models/MetadataMongoDB.ts:56:23)
      at Object.next (src/models/MetadataMongoDB.ts:37:53)
      at fulfilled (src/models/MetadataMongoDB.ts:28:58)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
```
* MetadataMongoDB.create fails if the metadata already exists.
* MetadataMongoDB.update calls save() on the document.
* MetadataMongoDB.updatePublicUserMetadata accounts for the first run
  when the array is empty.
For some reason, I couldn't get [1] to work, hence copy-webpack-plugin.

[1]: https://webpack.js.org/guides/asset-modules/
```log
MongoError: query requires text score metadata, but it is not available
    at Connection.<anonymous> (/Users/dchege711/study_buddy/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:453:61)
    at Connection.emit (node:events:513:28)
    at Connection.emit (node:domain:489:12)
    at processMessage (/Users/dchege711/study_buddy/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:456:10)
    at Socket.<anonymous> (/Users/dchege711/study_buddy/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:625:15)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 40218,
  codeName: 'Location40218'
}
```

[1] notes that textScore is only available when used in conjunction with
a $text query.

[1]: https://www.mongodb.com/docs/manual/reference/operator/aggregation/meta/
Configure web-pack to provide various files.
These two resources are easier to consume on the browser-side.
Couldn't get src/public/src/lib/AVLTree.d.ts and
src/public/src/lib/AVLTree.js to work on the browser, so using the node
version at [1] instead.

[1]: https://github.com/w8r/avl
Errors that are strings tend to be user-caused.
@dchege711 dchege711 force-pushed the user/dchege711/migrate-to-type-script-try-2 branch from c02e7cf to ff0b218 Compare April 14, 2024 22:07
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Obtained tip from [1]. Mac is not case-sensitive by default [2] and thus
we need an explicit `git mv` command.

```log
src/public/src/AutoComplete.test.ts:3:30 - error TS1261: Already included file name '/Users/dchege711/study_buddy/src/public/src/AutoComplete.ts' differs from file name '/Users/dchege711/study_buddy/src/public/src/Autocomplete.ts' only in casing.
  The file is in the program because:
    Imported via "./AutoComplete" from file '/Users/dchege711/study_buddy/src/public/src/AutoComplete.test.ts'
    Matched by include pattern './src/**/*' in '/Users/dchege711/study_buddy/tsconfig.json'
    Imported via "./AutoComplete" from file '/Users/dchege711/study_buddy/src/public/src/BrowseCardsPage.ts'
    Imported via "./AutoComplete" from file '/Users/dchege711/study_buddy/src/public/src/HomePage.ts'
    Imported via "./AutoComplete" from file '/Users/dchege711/study_buddy/src/public/src/TagsBarTemplate.ts'

3 import { AutoComplete } from "./AutoComplete";
                               ~~~~~~~~~~~~~~~~

  tsconfig.json:11:17
    11     "include": ["./src/**/*"]
                       ~~~~~~~~~~~~
    File is matched by include pattern specified here.
  src/public/src/BrowseCardsPage.ts:5:30
    5 import { AutoComplete } from "./AutoComplete";
                                   ~~~~~~~~~~~~~~~~
    File is included via import here.
  src/public/src/HomePage.ts:7:30
    7 import { AutoComplete } from "./AutoComplete";
                                   ~~~~~~~~~~~~~~~~
    File is included via import here.
  src/public/src/TagsBarTemplate.ts:1:30
    1 import { AutoComplete } from "./AutoComplete";
                                   ~~~~~~~~~~~~~~~~
    File is included via import here.

Found 1 error in src/public/src/AutoComplete.test.ts:3
```

[1]: https://stackoverflow.com/questions/17683458/how-do-i-commit-case-sensitive-only-filename-changes-in-git
[2]: https://discussions.apple.com/thread/251191099?sortBy=best
CI is failing with:

```log
Error: src/tests/mocha_tests/functionality_tests/TestLogIn.ts(8,43): error TS2345: Argument of type '(this: Mocha.Context) => void' is not assignable to parameter of type '(this: Suite) => void'.
  The 'this' types of each signature are incompatible.
    Type 'Suite' is missing the following properties from type 'Context': _runnable, runnable, skip
```
@dchege711
Copy link
Owner Author

Superseded by #152

@dchege711 dchege711 closed this Jun 8, 2024
@dchege711 dchege711 deleted the user/dchege711/migrate-to-type-script-try-2 branch June 9, 2024 19:21
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.

1 participant