Skip to content

Conversation

ivogabe
Copy link
Contributor

@ivogabe ivogabe commented May 27, 2016

I've implemented a generator that will create the TS definitions, with full support for chaining. Writing these type definitions by hand would be a lot of work or inaccurate. See the first comment in types/generator.js for details on how these definitions are created.

Fixes #871. Tests for this would be useful too, see #870.

@jamestalmage @SamVerschueren What do you think?

@novemberborn
Copy link
Member

Haven't really looked at the generate script, but I think it'd be fine for it to use ES2015 (and thus require Node 6). It's something we'd have to run locally and then check in the results.

var base = fs.readFileSync(path.join(__dirname, 'base.d.ts')).toString();

// All suported function names
var allParts = ['serial', 'before', 'after', 'skip', 'todo', 'failing', 'only', 'beforeEach', 'afterEach', 'cb', 'always'];
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if it was exported from within the library https://github.com/avajs/ava/blob/master/lib/runner.js#L11. Then if something changes, the generate script shouldn't be adjusted.

But then again, should libraries export stuff because of "documentation" purposes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good idea, it can be really annoying that the type definitions are not up to date.

@ivogabe
Copy link
Contributor Author

ivogabe commented May 29, 2016

I've addressed most of the comments and fixed the linting issues. Can you take another look?

@@ -0,0 +1,122 @@
// TypeScript definitions are generated here.
Copy link
Contributor

Choose a reason for hiding this comment

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

'use strict'

@sindresorhus
Copy link
Member

Can you call the file generate.js => make.js instead and add a npm run script for it, like "make-ts": "node types/make.js".

And Travis is failing.

@SamVerschueren
Copy link
Contributor

I would make the file executable. './make.js'

return Promise.resolve(this.tests.build(this._bail).run()).then(this._buildStats);
};

Runner._chainableMethods = chainableMethods.chainableMethods;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap these options in a generator function? So we aren't exporting a mutable copy?

Copy link
Member

Choose a reason for hiding this comment

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

@jamestalmage Doesn't matter. It's for internal use only.

@jamestalmage
Copy link
Contributor

Are we good here? This all looks good to me. @SamVerschueren - should I merge?

@SamVerschueren
Copy link
Contributor

I did not test the output though but generally everything looks good. If you want me to test it more thoroughly, I'm ok with that. If you want to merge, that's ok as well. We can always finetune in follow up commits.

Great work @ivogabe!

@jamestalmage
Copy link
Contributor

Should we be storing generated.d.ts in the repo?

Why not add it to .gitignore and create it in a prepublish script?

@SamVerschueren
Copy link
Contributor

Good point. Don't see any downside of doing that.

@ivogabe
Copy link
Contributor Author

ivogabe commented Jun 4, 2016

I usually don't add generated files to a repo, though I had two reasons now: it's still possible to install AVA from GitHub, and you can easily see what changed when you update the generator script.

@jamestalmage
Copy link
Contributor

I'd prefer ignoring it if possible.

It would be awesome if npm had a "installed from GitHub" hook

@SamVerschueren
Copy link
Contributor

Can it be executed on postinstall maybe? This would overwrite the generated.d.ts shipped with npm though.

@ivogabe
Copy link
Contributor Author

ivogabe commented Jun 8, 2016

I've added it to .gitignore. postinstall won't work, since the generator script requires NodeJS 6. I think that TS users already know that it's usually not possible to install a package from GitHub. Do you agree?

@jamestalmage
Copy link
Contributor

I think that TS users already know that it's usually not possible to install a package from GitHub.

I say we start with it this way, and see if there are lots of complaints.

package.json Outdated
"test-win": "tap --no-cov --reporter=classic --timeout=150 test/*.js test/reporters/*.js",
"visual": "node test/visual/run-visual-tests.js"
"visual": "node test/visual/run-visual-tests.js",
"make-ts": "node types/make"
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be a prepublish script. Otherwise I am going to forget this step when publishing.

@jamestalmage
Copy link
Contributor

I think that script generation should happen in a prepublish script. Otherwise, I think we are good to go.

@ivogabe
Copy link
Contributor Author

ivogabe commented Jun 15, 2016

@jamestalmage I would agree, but I'm afraid that the package cannot be published anymore with that change... The generation script uses ES2015, thus needs Node 6, and npm publish is broken on Node 6.

@SamVerschueren
Copy link
Contributor

@ivogabe I can't find features that can only be ran on Node 6 (spread operators for instance). Node 4 supports the majority of those features as well.

@ivogabe
Copy link
Contributor Author

ivogabe commented Jun 15, 2016

That makes life easier then. I've added it in 54a8324.

@jamestalmage
Copy link
Contributor

You need to polyfill .includes. I see no reason not to just require('babel-polyfill') for this.

// `after.cb` is fully written, and `cb.after` is emitted as an alias
// using `typeof after.cb`.

const path = require('path');
Copy link
Contributor

Choose a reason for hiding this comment

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

const => var throughout. It's not supported on Node 0.12 or 0.10.

Hopefully, that's the last of it. I would like to get this merged.

Copy link
Member

Choose a reason for hiding this comment

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

Same with arrow functions, let, etc, used here. Would be much easier to just require('babel/register') this script.

@sindresorhus
Copy link
Member

@ivogabe ping :) => #884 (comment)

@ivogabe
Copy link
Contributor Author

ivogabe commented Jul 1, 2016

Sorry, I've been too busy lately and forgot about this. I've added babel/register, I thought it was easiest to do that in package.json, otherwise I would need to add another file that required babel and the generation script.

package.json Outdated
"visual": "node test/visual/run-visual-tests.js"
"visual": "node test/visual/run-visual-tests.js",
"prepublish": "npm run make-ts",
"make-ts": "node -e \"require('babel-register')({ presets: 'babel-preset-es2015' }); require('./types/make');\""
Copy link
Member

@sindresorhus sindresorhus Jul 3, 2016

Choose a reason for hiding this comment

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

Actually, we could just use babel-node here instead of node. We just need to add babel-cli as a devDependency. Then we could drop this boilerplate.

https://babeljs.io/docs/usage/cli/#babel-node

This will also fix running it on Node.js 0.10 which is currently failing and would let you remove the polyfills in https://github.com/avajs/ava/pull/884/files#diff-148e5d484548912a06d9d0dd7cd4f891R19

@ivogabe
Copy link
Contributor Author

ivogabe commented Jul 13, 2016

Does babel-node require more configuration? It still fails on node 0.10 and 0.12. The tests are failing on node 4, has anyone an idea why?

@sindresorhus
Copy link
Member

sindresorhus commented Jul 13, 2016

@ivogabe Unfortunately yes, since Babel 6 everything needs config.

Replace it with:

"make-ts": "babel-node --presets=babel-preset-es2015 --plugins=transform-runtime types/make.js"

@sindresorhus sindresorhus merged commit 1dd6d5b into avajs:master Jul 13, 2016
@sindresorhus
Copy link
Member

Awesome! Thanks for working on this @ivogabe :)

celebration

@SamVerschueren
Copy link
Contributor

Nice work @ivogabe !

@ivogabe
Copy link
Contributor Author

ivogabe commented Jul 13, 2016

🎉

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.

5 participants