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

♻️ 🏗 🚀 Begin adding type checking to the build-system directory. #32531

Merged
merged 9 commits into from Feb 22, 2021

Conversation

rileyajones
Copy link
Contributor

By using typescript to compile the existing javascript we can catch a lot before they are introduced to the codebase without having to fully switch to typescript.

Unfortunately the existing code contains a significant number of errors so prior to adding this as a build step they there will need to be several pull requests filled with just typing changes.

For reference without this change there are 280 errors.
With this change there are 109
With this change and @types/mocha installed the number falls to 44.

Those 44 errors almost all actually errors that should be fixed and that we likely would not have found without this check.

#28387

@amp-owners-bot
Copy link

amp-owners-bot bot commented Feb 9, 2021

Hey @rsimha! These files were changed:

build-system/compile/compile.js
build-system/server/lazy-build.js

Hey @danielrozenberg! These files were changed:

build-system/compile/internal-version.js

Hey @alanorozco! These files were changed:

build-system/server/app-index/amphtml-helpers.js
build-system/server/app-index/file-list.js
build-system/server/app-index/html.js
build-system/server/app-index/template.js
build-system/server/app-index/test/helpers.js
build-system/server/app-index/test/test-amphtml-helpers.js
build-system/server/app-index/test/test-html.js

@rileyajones
Copy link
Contributor Author

I am aware that this is a large change containing a lot of files, so, given that it is currently nonfunctional, I'm happy to split it apart further if anyone would prefer.

Copy link
Contributor

@rsimha rsimha left a comment

Choose a reason for hiding this comment

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

Nice PR! I've added a few preliminary comments for the CI and task code. Can we split them off into one PR, and fix the server code in a separate PR? (They are fairly isolated from each other.)

build-system/common/check-package-manager.js Outdated Show resolved Hide resolved
@@ -31,7 +31,7 @@ const shellCmd = process.platform == 'win32' ? 'cmd' : '/bin/bash';
* object.
*
* @param {string} cmd
* @param {?Object} options
* @param {?Object=} options
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious: If this is being changed, would it make sense to change the signature as follows?

function exec(cmd, options = {'stdio': 'inherit'}) {

(Same question applies to other instances)

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 think that would make sense in this instance, I'm not sure about all the others.

build-system/compile/check-for-unknown-deps.js Outdated Show resolved Hide resolved
build-system/compile/compile.js Outdated Show resolved Hide resolved
build-system/compile/internal-version.js Outdated Show resolved Hide resolved
build-system/compile/compile.js Outdated Show resolved Hide resolved
build-system/global.d.ts Outdated Show resolved Hide resolved
build-system/compile/closure-compile.d.ts Outdated Show resolved Hide resolved
@@ -0,0 +1,13 @@
declare global {
interface Error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe @ types/node will help here too instead of defining a custom interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately neither showStack nor status is not an attribute of Error so we either need this or a custom Error type.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah I see.. looks like it's a gulp type. I don't know about status but I found showStack here https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/plugin-error/index.d.ts#L13

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, showStack is for gulp, and should go away when we adopt a more lightweight task runner.

status is something we set in exec.js, so it might be possible to use a different mechanism:

/**
* Executes the provided command, piping the parent process' stderr, throwing
* an error with the provided message the command fails, and returns the
* process object.
* @param {string} cmd
* @param {string} msg
* @return {!Object}
*/
function execOrThrow(cmd, msg) {
const p = exec(cmd, {'stdio': ['inherit', 'inherit', 'pipe']});
if (p.status && p.status != 0) {
log(yellow('ERROR:'), msg);
const error = new Error(p.stderr);
error.status = p.status;
throw error;
}
return p;
}

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 would rather not alter the existing error message handling system for now. Given that those are both properties that we do actually use this type extension makes sense to me, but should remove these properties once the code using them is gone.

Copy link
Collaborator

@estherkim estherkim Feb 18, 2021

Choose a reason for hiding this comment

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

Ok that's fair, how about adding a TODO in global.d.ts to delete it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

build-system/tasks/css/index.js Outdated Show resolved Hide resolved
@rileyajones rileyajones merged commit 18baf35 into ampproject:master Feb 22, 2021
caroqliu pushed a commit to caroqliu/amphtml that referenced this pull request Feb 23, 2021
…project#32531)

Begin adding type checking to the build-system directory. 
* create tsconfig and fix lots of type errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants