Skip to content

Conversation

@armanio123
Copy link
Contributor

Added path check for not looking for TODOs if the file is inside the node_modules folder.

@msftclas
Copy link

@armanio123,
Thanks for your contribution as a Microsoft full-time employee or intern. You do not need to sign a CLA.
Thanks,
Microsoft Pull Request Bot

@sandersn
Copy link
Member

I don't know this code well at all, so I'll defer a real review to others. The only thing I wonder about is whether node_modules might have upper-case letters on Windows. @amcasey I think you were talking about this yesterday?

@amcasey
Copy link
Member

amcasey commented Jul 18, 2017

@sandersn We discussed it and decided that, since manual intervention is required to introduce uppercase letters and since the impact is extra TODOs in the Task list, there was limited benefit in making the check more flexible.

function isNodeModulesFile(path: string): boolean {
const node_modulesFolderName = "/node_modules/";

return path.indexOf(node_modulesFolderName) !== -1;
Copy link
Member

Choose a reason for hiding this comment

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

This is simply a peeve of mine, but I don't think that checking for -1, specifically, makes sense. Why not rule out any negative value (i.e. >= 0)? This specific API may be documented to return exactly -1 (I haven't checked), but many others are not.

Copy link
Member

Choose a reason for hiding this comment

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

My favorite is:

  return !!~path.indexOf(somevar); // Note: Overly cute hack. Do not use! :-)

Take your time.... I'll wait 😉

Note in JavaScript it is explicitly a result of -1 if not found (http://es5.github.io/#x15.5.4.7). Simply checking for < 0 seems less correct, in the unlikely event they add other negative values for other scenarios.

Copy link
Member

Choose a reason for hiding this comment

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

While I agree that the meanings of other negative values are unspecified, it seems borderline inconceivable that they could eventually be defined as success cases. Ruling them out now seems more forward (and slightly-non-compliant implementation) compatible.

Copy link
Contributor

Choose a reason for hiding this comment

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

From the point of readability path.indexOf(node_modulesFolderName) !== -1 sucks.

As with any double-negative, you're forced to pause and do some mental calculations: "ah, it's checking for position, then asserts that position is not good; no wait it asserts that it's not wrong, then proceeds — therefore this statement checks that the substring exists in the path string! Phew...."

Rules are academic. In the end what counts is cost of maintenance. Replacing !==-1 with >=0 improves readability and reduces possibility of bugs.

const result: TodoComment[] = [];

if (descriptors.length > 0) {
if (descriptors.length > 0 && !isNodeModulesFile(fileName)) {
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I would add a comment explaining why we exclude such 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 existing function program.isSourceFileFromExternalLibrary probably does what you want here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added as suggested.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The program.isSourceFileFromExternalLibrary cannot be used as it only identifies a file as external library if it is found during the dependency resolution. Since the getTodoComments method checks the file individually it fails when an editor submits a node_modules file as a root file.

const result: TodoComment[] = [];

if (descriptors.length > 0) {
if (descriptors.length > 0 && !isNodeModulesFile(fileName)) {
Copy link
Member

Choose a reason for hiding this comment

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

Is fileName already normalized by this point? If not, is sourceFile.fileName?

Copy link
Member

Choose a reason for hiding this comment

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

If neither, consider just normalizing the path in isNodeModulesFile.

Copy link
Contributor Author

@armanio123 armanio123 Jul 19, 2017

Choose a reason for hiding this comment

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

Changed to use sourceFile.fileName as it is normalized. I have confirmed checking the source code.

Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

LGTM.

p.s. I don't see how the test failures could be related to your changes.

/// <reference path='fourslash.ts' />

// @Filename: dir1/node_modules/todoTest0.ts
//// // TODO
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand these last 2 tests. They seem to be verify the comment IS present, when based on the file names shouldn't they be NOT present? Or am I reading the tests wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This last two tests creates some node_module files with Todos in them.

Since no "spans" have been added to those files, the line of code verify.todoCommentsInCurrentFile(["TODO"]); pretty much checks for 0 todos (no more, no less). This makes the test scenario successful when the service don't report any todos when checking node_modules files even if they contain some.

Copy link
Member

Choose a reason for hiding this comment

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

The way the test code reads is entirely unintuitive (not your fault, but the semantics of the test infra). Some clarifying comments in the test would be good for future readers, else looks good to me. Thanks!

@armanio123 armanio123 merged commit fe86d2f into microsoft:master Jul 20, 2017
// @Filename: node_modules/fake-module/ts.ts
//// // TODO

verify.todoCommentsInCurrentFile(["TODO"]);
Copy link
Contributor

Choose a reason for hiding this comment

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

The check is for /node_modules/ — would it apply in Windows with its backslash directory separators?

Would it be sensible to add a test for that?

}

function isNodeModulesFile(path: string): boolean {
const node_modulesFolderName = "/node_modules/";
Copy link
Contributor

@mihailik mihailik Jul 24, 2017

Choose a reason for hiding this comment

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

What if the whole project is situated nested inside node_modules somewhere? Will all TODOs suddenly disappear?

/myDir1/node_modules/myOtherDirectory/myProjectRoot/src/blablaFile.ts

  1. People do put temporary junk in their node_modules for the purpose of quick-checking someething and avoiding erroneous commit.

  2. Additionally, in complex large projects there may be a legitimate need to keep projects in node_modules due to some process optimisation.

All in all, the check should only turn true if node_modules appear after the project root in full path, not before it.

@armanio123 armanio123 deleted the FixNodeModulesTodos branch July 24, 2017 17:17
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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.

6 participants