Skip to content

Babel setup for async/await #1692

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

Merged
merged 8 commits into from
Dec 19, 2017
Merged

Babel setup for async/await #1692

merged 8 commits into from
Dec 19, 2017

Conversation

hameleonka
Copy link
Contributor

@rpl this PR adds babel support for async/await with transpilation to ES2015 generators for backward compatibility with node 6 + working example of function converted to async/await syntax in linter.js. Please check it out.

…ators for backward compatibility with node 6 + working example of function converted to async/await syntax in linter.js
Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

This first set of changes looks good (besides some super small nit, the main fix needed is just the one related to the package.json changes).

None of the opened pull request are currently applying changes to the src/linter.js module, and it should be a module that does not change too much in the pull requests that add or fix the validation rules, and so it looks a good module to start this conversion from, good choice.

There are more functions in this file that will be converted, I guess that your goal was to get a preliminary feedback on the first step and then keep working on it, and so I'm not approving this pull request yet (but you got my approval on this first step ;-))

@@ -2,7 +2,7 @@
"rules":{
"no-console": 0,
// Inherited from airbnb via eslint-config-amo
'function-paren-newline': 0,
"function-paren-newline": 0
Copy link
Member

Choose a reason for hiding this comment

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

thanks!

package.json Outdated
@@ -35,15 +35,20 @@
"babel-eslint": "8.0.2",
"babel-jest": "21.2.0",
"babel-loader": "7.1.2",
"babel-plugin-syntax-async-functions": "^6.13.0",
Copy link
Member

Choose a reason for hiding this comment

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

Every dependency is tha package.json file should be an exact version (without the ^), so that the version is fixed and greenkeeper can open a pull request for us when one of our dependency released a new version on npm.

throw err;
}
}
throw invalidMessage;
Copy link
Member

Choose a reason for hiding this comment

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

👍 it looks great (I would add an empty line at line 289, but that is just a small nit)

@@ -246,15 +246,15 @@ describe('Linter', () => {
addonLinter.io = { files: { whatever: {} } };
addonLinter.io.getFile = () => Promise.resolve();
addonLinter.getScanner = sinon.stub();
class fakeScanner {
class FakeScanner {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks! (it seems a convention that it could be also nice to enforce on all our sources using an eslint rules, but we can evaluate that in a follow up because I guess that an eslintrc rule could catch more of these and they would be changes unrelated to this pull request goal).

@hameleonka
Copy link
Contributor Author

@rpl I finished rewriting linter.js. Could you please check it out when you have a chance.

Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

Hi Natalia,
this PR looks pretty good, the new async functions are so much more readable of their previous version, thanks you a lot.

follows the new round of review comments (related to a couple of very small tweaks and some optional small nits).

"jed": "1.1.1",
"pino": "^4.6.0",
"pino": "4.6.0",
Copy link
Member

Choose a reason for hiding this comment

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

@EnTeQuAk it seems that in this file there were some pre-existent dependency without a strict version, it could be related to yarnpkg/yarn#1088.

src/linter.js Outdated
scannedFiles: [filename],
});
}
// First: check that this file is under our 2MB parsing limit. Otherwise
Copy link
Member

Choose a reason for hiding this comment

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

I've been following the code that make sure that this.io.files map contains the expected file entries, and I've also been discussing about it a bit with @EnTeQuAk today, and it seems that the this.io.files is going to be there mainly because scanFile is not usually called directly, but it is called internally by the Linter class when we call scan (also confirmed by looking at the related test cases).

Nevertheless, it seems that calling this.io.getFile also ensure that the filename is going to be in the this.io.files map of file entries, and so it could be safer for now to still have a const fileData = await this.io.getFile(...) at line 320, so that we reduce chances of introducing a regression during the async/await rewrite (and look into make this "implicit dependency" more explicit in a follow up).

src/linter.js Outdated
};
} else {
const fileData = await this.io.getFile(filename, ScannerClass.fileResultType);
if (ScannerClass !== BinaryScanner && ScannerClass !== FilenameScanner) {
Copy link
Member

Choose a reason for hiding this comment

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

once we move line 339 to line 320 (near to where it used to be called in the previous version) we can merge this if with the else at line 338.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rpl I moved fileData from line 339 to line 320, but I don't understand how can I merge else on line 338 with if from line 340. It seems for me like they are both necessary.

Copy link
Member

Choose a reason for hiding this comment

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

@hameleonka yeah, don't worry about that, you are right.

Also I think that it is better that we don't change too much this function in this PR, while we are rewriting many of these functions into async functions (and it sound that making this function a bit more readable could be part of the issue mentioned at line 354).

src/linter.js Outdated
io.setScanFileCallback(this.shouldScanFile);
this.io = io;

const addonMetadata = await this.markSpecialFiles(await this.getAddonMetadata());
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we could make this two await more visible and readable, eg.:

let addonMetadata = await this.getAddonMetadata();
addonMetadata = await this.markSpecialFiles(addonMetadata);

or at least move then into two different lines:

const addonMetadata = await this.markSpecialFiles(
  await this.getAddonMetadata()
);

(I kind of prefer the first one which make it also a bit more visible which is the order of the two awaits).

src/linter.js Outdated
try {
await this.extractMetadata(deps);
const files = await this.io.getFiles();
if (this.config.scanFile) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit, let's add one empty line here (just to aid readability)

src/linter.js Outdated
this.print(deps._console);
// This is skipped in the code coverage because the
// test runs against un-instrumented code.
/* istanbul ignore if */
Copy link
Member

Choose a reason for hiding this comment

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

Nit, do you mind to remove one space from the end of this comment? (it was already there, but given that we are touching this function... ;-), same at line 492)

src/linter.js Outdated
}
return this.scan(deps).then(() => this.output);
await this.scan(deps);
Copy link
Member

Choose a reason for hiding this comment

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

Nit, do you mind to add one empty line at line 504 (only to make it a bit more visible).

Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

@hameleonka this PR seems to be almost ready, this is likely to be the last round of review comments.

The most important fix that I think we need in this PR is the ones related to Object.prototype.hasOwnProperty.call vs. ... in ....

src/linter.js Outdated
await checkMinNodeVersion();

const stats = await this.checkFileExists(this.packagePath);
let io;
Copy link
Member

Choose a reason for hiding this comment

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

Nit, it seems that we could just set this.io at lines 390, 393 and 398 and change line 401 to call this.io.setScanFileCallback(...) and remove the let io; here.

src/linter.js Outdated
await this.extractMetadata(deps);
const files = await this.io.getFiles();

if (this.config.scanFile) {
Copy link
Member

@rpl rpl Dec 15, 2017

Choose a reason for hiding this comment

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

Nit, It seems that we can merge this two nested if into a single:

if (this.config.scanFile && 
    !this.config.scanFile.some(...)) {
  ...
}

src/linter.js Outdated

// Known libraries do not need to be scanned
const filesWithoutJSLibraries = Object.keys(files).filter((file) => {
return !(file in this.addonMetadata.jsLibs);
Copy link
Member

Choose a reason for hiding this comment

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

The previous version of this line was using Object.prototype.hasOwnProperty.call instead of in, I think that we should still use Object.prototype.hasOwnProperty here to avoid new regression related to the subtle differences between in and Object.prototype.hasOwnProperty.

src/linter.js Outdated
return this.addonMetadata;
});
const files = await this.io.getFiles();
if (constants.MANIFEST_JSON in files) {
Copy link
Member

Choose a reason for hiding this comment

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

The previous version of this line was using Object.prototype.hasOwnProperty.call instead of in, I think that we should still use Object.prototype.hasOwnProperty here to avoid a new regression related to the subtle differences between in and Object.prototype.hasOwnProperty.

@hameleonka
Copy link
Contributor Author

@rpl I've made all changes that you've mentioned above. Please check it again.

rpl
rpl previously approved these changes Dec 18, 2017
Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

@hameleonka it looks great, thanks! 🎉

I added a comment related to a very small nit related to the indentation of a single line, eslint doesn't (currently) complain and it is not a big deal.

@EnTeQuAk This should be now ready to land and officially start the conversion of the addons-linter sources \o/
(also, I've built addons-linter from this PR and executed it locally on a couple of extension examples and real world extensions, I have been able to lint them successfully and I didn't notice any regression).

const files = await this.io.getFiles();

if (this.config.scanFile &&
!this.config.scanFile.some((f) => Object.keys(files).includes(f))) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit, align !this.config.scanFile... with this.config.scanFile from the previous line.

Copy link
Contributor

@EnTeQuAk EnTeQuAk left a comment

Choose a reason for hiding this comment

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

Personal "removed more lines than added"-award, let's get this landed!

@EnTeQuAk EnTeQuAk added this to the 2018.01.11 milestone Dec 19, 2017
@EnTeQuAk EnTeQuAk merged commit 2025658 into mozilla:master Dec 19, 2017
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.

3 participants