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

New rule: import-blacklist #1841

Merged
merged 11 commits into from
Dec 13, 2016
Merged

New rule: import-blacklist #1841

merged 11 commits into from
Dec 13, 2016

Conversation

SimonSchick
Copy link
Contributor

@SimonSchick SimonSchick commented Dec 10, 2016

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

What changes did you make?

Cont. of #1549

I was given permission by the author to continue his work:
I added tests.
Fixed problems detected by tests (somewhat of a refactor too).
Added license.

Also due to a previous change master broke, I fixed this in my PR to make it build for now.

Is there anything you'd like reviewers to focus on?

Perhaps a better error message? I'm not good with words.

use-strict and others added 8 commits December 10, 2016 15:12
This rule checks for require/import statements with a user-specified list of banned strings. This is useful to prevent accidental inclusions of entire libraries, when tree shaking is not possible. E.g. importing "material-ui" instead of "material-ui/lib/something".
@SimonSchick
Copy link
Contributor Author

Don't want to spam you @use-strict but please sign the palantir CLA in your branch to get this rolling, a review is also welcome 😄

@SimonSchick
Copy link
Contributor Author

SimonSchick commented Dec 10, 2016

@use-strict
Copy link

@SimonSchick , done. Great job, thanks!

@SimonSchick
Copy link
Contributor Author

Noticed conflict, will merge/rebase after I got approval.

@adidahiya adidahiya changed the title New rule - import-blacklist New rule: import-blacklist Dec 12, 2016
Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

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

@SimonSchick thanks for bearing with the broken build on master

minLength: 1,
},
optionExamples: ["true", '[true, "rxjs", "lodash"]'],
type: "typescript",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is typescript-only -- I'd throw it in the "functionality" bucket (kind of a catch-all)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yea, that is a copy paste error, sorry 😛

typescriptOnly: false,
};

public static FAILURE_STRING = "This import is not allowed.";
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: "This import is blacklisted, consider importing a submodule instead"

Minor: we're a little inconsistent about this elsewhere, but I'd prefer to omit the trailing period in this failure string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure thing

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 I gonna go for a slightly more imperative and concise message, feel free to complain tho 😛

super(sourceFile, options);
}

protected isListed(text: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

why protected?

Copy link
Contributor

Choose a reason for hiding this comment

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

also I think a nicer method signature would be private isModuleBlacklisted(moduleName: string): boolean

}

class NoRequireFullLibraryWalker extends Lint.RuleWalker {
constructor (sourceFile: ts.SourceFile, options: Lint.IOptions, private blackList: string[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: can you remove the private parameter property syntax and do this the old fashioned way, this.blacklist = blacklist? I find that more readable. (note the lack of camelCase, just "blacklist", not "blackList".)

super.visitImportDeclaration(node);
}

private failure (node: ts.Node): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

question: is there any more specific common type for the node parameter here that we can declare?

minor: can you please call this method reportFailure to be more verbose / readable

@SimonSchick
Copy link
Contributor Author

Happy? 😄

@adidahiya
Copy link
Contributor

looks great, thanks @SimonSchick!

@adidahiya adidahiya merged commit 6979cda into palantir:master Dec 13, 2016
@SimonSchick SimonSchick deleted the patch-1 branch December 13, 2016 13:35
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.

3 participants