Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

This typings #40

Merged
merged 5 commits into from
Mar 24, 2018
Merged

This typings #40

merged 5 commits into from
Mar 24, 2018

Conversation

zoehneto
Copy link
Contributor

Optionally applies this types to functions. Fixes #33 .

@coveralls
Copy link

coveralls commented Mar 23, 2018

Coverage Status

Coverage increased (+0.3%) to 85.649% when pulling 641f3e5 on zoehneto:this-typings into 7d9f39a on urish:master.

@@ -8,7 +8,7 @@
"license": "MIT",
"scripts": {
"build": "rimraf dist && tsc && copyfiles -u 1 src/type-collector-snippet.ts dist",
"format": "prettier --write src/**/*.ts packages/**/src/**.ts",
"format": "prettier --write src/*.ts src/**/*.ts packages/**/src/**.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for adding src/*.ts? afaik src/**/*.ts also includes any .ts files directly under src

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least for me (on Arch Linux) it is required or the files directly under src are ignored.

@@ -81,29 +67,20 @@ export function applyTypesToFile(
replacements.push(Replacement.insert(opts.parens[0], '('));
suffix = ')';
}
if (opts && opts.comma) {
suffix = ', ';
Copy link
Collaborator

Choose a reason for hiding this comment

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

are we sure opts.comma and opts.parens are mutually exclusive? Otherwise, we need to account for the case where both opts.comma and opts.parens set suffix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

opts.parens is only used for one parameter arrow functions so this should never happen since arrow functions use the surrounding this and therefore don't need an explicit this type.

replacements.push(Replacement.insert(pos, ': ' + prefix + sortedTypes.join('|') + suffix));
if (opts && opts.thisType) {
replacements.push(Replacement.insert(pos, 'this'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we add two replacement at the same position, let's use the third priority parameter for Replacement.insert to guarantee that this will always come before the colon. Or we can just combine both into one replacement - similar to what with do with prefix and suffix, we can add another variable called thisPrefix or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a thisPrefix variable in the latest commit.

`);
});

it('should not add `this` type', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is should not add this this here? Let's change the test description to explain that (e.g. "it should not add this type in case it can be inferred")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expanded the explanation.

class NestError extends Error {}

export type ISourceLocation = [string, number]; /* filename, offset */

interface IKey {
filename: string;
pos: number;
opts: any;
opts: ICollectedTypeInfo;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! Though, I think it will make more sense to go the other way around - move the definition of ICollectedTypeInfo here, and import it in apply-types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the type to type-collector-snippet.ts.

@urish urish added the enhancement New feature or request label Mar 23, 2018
@urish
Copy link
Collaborator

urish commented Mar 23, 2018

Lovely, thank you very much! I left a few comments, but overall looks really nice :)

@zoehneto
Copy link
Contributor Author

Thanks for the quick review, I addressed your comments in the latest commit.

@urish urish merged commit 8e227d0 into mockdeep:master Mar 24, 2018
@urish
Copy link
Collaborator

urish commented Mar 24, 2018

Awesome, thanks!

Just out of curiosity - have you used TypeWiz on any of your projects?

@zoehneto
Copy link
Contributor Author

What is not yet covered by this PR (and was also not covered in #27) is how to roll out these changes to typewiz-node and typewiz-webpack. I personally don't use either since I bundle with rollup and then test the bundle but this should be considered going forward.

@zoehneto
Copy link
Contributor Author

Just saw your comment, I have experimentally added typewiz to rtf.js where I use a custom script for instrumentation.

@urish
Copy link
Collaborator

urish commented Mar 24, 2018

Very interesting - so in this case, you are basically writing an instrumented copy of your source code to the build directory, and then the tests run using the instrumented source code, right?

I couldn't find where you told the bundle / test steps to read the source files from the build directory though, can you please point me?

@zoehneto
Copy link
Contributor Author

It works as follows:

  1. typewiz-instrument.js instruments the files using the typewiz api (the key here is to pass the original filenames in the source directory to the instrumenter), compiles the individual files using the typescript api and then writes the files to the build directory.
  2. Then npm run bundle is invoked which creates three UMD / ES5 bundles in the dist directory based on the instrumented code from build
  3. The test suite is started using npm run test which runs the script test/test-rtfjs.js (this script executes a test for every directory under test/test-files and applies the collected types after all tests are finished). To execute the individual tests test/utils.js is used which converts rtf files to html. Since rtf.js requires a browser to run (because of some of the libraries it's built on) the actual test is executed using JSDOM which loads the scripts from dist (line 30 - 32) and has to be passed $_$twiz (line 105).

@urish
Copy link
Collaborator

urish commented Mar 24, 2018

Got it, thanks!

I'm currently helping migrate immer to TS using typewiz, and they are also using rollup for their build. In their case, I used rollup-plugin-typescript2. In your build, you are running TypeScript as a separate compile step. Is there any reason for preferring running TypeScript separately and not as part of rollup?

@zoehneto
Copy link
Contributor Author

In rtf.js we build three separate bundles (for rtf.js, emf.js and wmf.js) but building rtf.js requires building the other two with tsc in the same step (because of some import tricks it uses) so if it used the rollup plugin emf.js and wmf.js would be built twice.

@urish
Copy link
Collaborator

urish commented Mar 24, 2018

And how useful have you found TypeWiz so far for rtf.js?

@zoehneto
Copy link
Contributor Author

Sadly I found your blog post too late. A month ago it was just three plain ES5 files with around 9000 lines of code and then I did the entire conversion by hand and added all the typings :(

@urish
Copy link
Collaborator

urish commented Mar 24, 2018

I hear you... I did the same with several projects before coming up with TypeWiz. But the future will be brighter :)

@urish
Copy link
Collaborator

urish commented Mar 26, 2018

Released as part of 0.7.0

@urish urish mentioned this pull request Apr 1, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants