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

completed-docs: Limit variable checking to file-level variables and use correct visibility #2950

Merged
merged 1 commit into from
Jul 23, 2017
Merged

completed-docs: Limit variable checking to file-level variables and use correct visibility #2950

merged 1 commit into from
Jul 23, 2017

Conversation

reduckted
Copy link
Contributor

@reduckted reduckted commented Jun 24, 2017

PR checklist

Overview of change:

This PR fixes two issues:

  1. When checking if a variable meets the required visibility, the modifiers on the variable were being used, but the variable doesn't have modifiers - the modifiers are on the variable statement which is two levels up. These changes step up two levels to get the correct modifiers.
  2. When checking variables, all variables were being checked, including those inside for loops, catch statements, and local variables inside functions. This PR limits variable checking to variables that have a parent of a SourceFile - i.e. variables at the file-level.

As a side note, I've also fixed the .editorconfig file to have a rule for tslint.json files that enforces an indent size of 2 instead of the default 4, because all tslint.json files I could see used an indentation of 2.

CHANGELOG.md entry:

[bugfix] completed-docs: Uses correct visibility of variables.
[bugfix] completed-docs: Only checks variables at the file-level.

private checkVariable(node: ts.VariableDeclaration) {
// Only check variables in variable declaration lists
// and not variables in catch clauses and for loops.
if (node.parent === undefined || node.parent.kind !== ts.SyntaxKind.VariableDeclarationList) {
Copy link
Contributor

Choose a reason for hiding this comment

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

node.parent as well as list.parent and statement.parent will never be undefined. You can simply assert that using for example node.parent!.kind

// Only check variables at the file-level and not
// variables declared inside functions and other things.
const statement = list.parent;
if (statement.parent === undefined || statement.parent.kind === ts.SyntaxKind.SourceFile) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what about (exported) variables inside a namespace (ts.SyntaxKind.ModuleBlock)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point 👍

@reduckted
Copy link
Contributor Author

Just pushed up some changes suggested by @ajafff.


// Only check variables at the namespace/module-level or file-level
// and not variables declared inside functions and other things.
let check = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

statement.parent can not be undefined. This check ccould be simplified to

switch (statement.parent!.kind) {
    case ts.SyntaxKind.SourceFile:
    case ts.SyntaxKind.ModuleBlock:
        break;
    default:
        return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doh! Sorry, missed that on your previous comment. Am I correct in assuming it's defined as parent?: Node because it can be undefined if the setParentNodes argument is false when you load the TypeScript source file. But since TSLint always passes true to that argument, the parent nodes will always be set (except on the root SourceFile node)?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's exactly how it works.

@ajafff ajafff merged commit 9afcf17 into palantir:master Jul 23, 2017
@ajafff
Copy link
Contributor

ajafff commented Jul 23, 2017

Thanks @reduckted

@reduckted reduckted deleted the completed-docs-variables branch July 23, 2017 08:57
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants