Skip to content

refactor(schematics): enable strict null checks #12983

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

Conversation

devversion
Copy link
Member

  • Enables strict null checks for the schematics and fixes a few potential unsafe accesses.
  • Supports specifying a project for ng-add.

@devversion devversion added pr: merge safe target: major This PR is targeted for the next major release labels Sep 5, 2018
@devversion devversion requested a review from amcdnl as a code owner September 5, 2018 13:59
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 5, 2018
@@ -30,7 +30,7 @@ export default function(options: Schema): Rule {
*/
function addFormModulesToModule(options: Schema) {
return (host: Tree) => {
const modulePath = findModuleFromOptions(host, options);
const modulePath = findModuleFromOptions(host, options)!;
Copy link
Member Author

@devversion devversion Sep 5, 2018

Choose a reason for hiding this comment

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

This is never undefined or null because the addXModulesToModule methods are only called if skipImport is set to false. And the findModuleFromOptions method only returns undefined if that option is enabled.

See: https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/find-module.ts#L26

@jelbourn Want me to add a comment for this? (afraid of repeating it for every schematic though)

Copy link
Member

Choose a reason for hiding this comment

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

Could the function return an empty string in that case?

Copy link
Member Author

@devversion devversion Sep 5, 2018

Choose a reason for hiding this comment

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

What would be the benefit? This comes from an utility function from @schematics/angular. It should be really never undefined because we already check for skipImport before.

@@ -30,7 +30,7 @@ export default function(options: Schema): Rule {
*/
function addFormModulesToModule(options: Schema) {
return (host: Tree) => {
const modulePath = findModuleFromOptions(host, options);
const modulePath = findModuleFromOptions(host, options)!;
Copy link
Member

Choose a reason for hiding this comment

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

Could the function return an empty string in that case?

@@ -33,7 +33,7 @@ export function appendElementToHead(host: Tree, project: WorkspaceProject, eleme
throw `Could not find '<head>' element in HTML file: ${indexPath}`;
}

const endTagOffset = headTag.sourceCodeLocation.endTag.startOffset;
const endTagOffset = headTag.sourceCodeLocation!.endTag.startOffset;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this never null?

Similar for other one-off places (like architect below)

Copy link
Member Author

Choose a reason for hiding this comment

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

This is never null because we explicitly enabled the parse5 option to track the source code location. So we will always have this property set.

Regarding architect: We could do better here, but I just went with the assumption that every CLI project has an architect block configured. We also assert if the default architect builders are used in the theming/index.ts file.

Copy link
Member

Choose a reason for hiding this comment

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

We should capture that explanation somewhere in the code

Copy link
Member Author

Choose a reason for hiding this comment

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

I will add a note for the parse5 instances. There aren't too much of those.

For the architect. I'll probably create an utility function that does the checking for an architect target. That way we are strict-null check compliant and also make sure that projects that don't have a builder will gracefully exit with a meaningful error message.

* Enables strict null checks for the schematics and fixes a few potential unsafe accesses.
* Supports specifying a project for `ng-add`.
@devversion devversion force-pushed the refactor/schematics-strict-null-checks branch 2 times, most recently from e7f5c42 to 0a872c4 Compare September 6, 2018 10:34
@devversion devversion force-pushed the refactor/schematics-strict-null-checks branch from 0a872c4 to 85ded36 Compare September 6, 2018 10:36
@devversion
Copy link
Member Author

@jelbourn Addressed your feedback. Please have another look

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn
Copy link
Member

jelbourn commented Sep 6, 2018

Just looks like there's a lint error

@devversion
Copy link
Member Author

Fixed

@devversion devversion added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Sep 6, 2018
@jelbourn jelbourn merged commit 098f25a into angular:master Sep 6, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants