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

fix(ruleLoader): resolve rule files using node path resolution #3108

Merged
merged 1 commit into from
Aug 9, 2017

Conversation

devversion
Copy link
Contributor

@devversion devversion commented Aug 8, 2017

Custom rule files are no longer being loaded with a hardcoded/fixed extenson (.js).

With this change all rule files will be resolved using Nodes path resolution. This means that the extension will be detected automatically and other extension loaders like ts-node can be used.

This means that rules can be also written in TypeScript and loaded through TS-Node.

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests (Not sure if it's worth testing with TS-node etc. Might be a better follow-up)
  • Documentation update

Overview of change:

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

CHANGELOG.md entry:

(Edit by @ajafff: update changelog entry)
[enhancement] custom lint rules will be resolved using node's path resolution to allow for loaders like ts-node

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @devversion! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@adidahiya adidahiya requested a review from ajafff August 8, 2017 18:58
Copy link
Contributor

@ajafff ajafff left a comment

Choose a reason for hiding this comment

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

Only found some nits. Looks good otherwise.

This may cause some slowdown when loading rules, but since we cache the result that should not be noticeable.

Re tests: the important thing is that the existing behavior still works. And that's covered by the existing tests.
I don't think we need to add ts-node as dependency just for this one test.

try {
return require.resolve(path.join(directory, ruleName));
} catch (e) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use undefined instead

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. This is probably different from project to project. Going to change it.

if (fs.existsSync(`${fullPath}.js`)) {
const ruleModule = require(fullPath) as { Rule: RuleConstructor } | undefined;
const ruleFullPath = getRuleFullPath(directory, ruleName);
if (ruleFullPath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use ruleFullPath !== undefined to make the linter happy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

const fullPath = path.join(directory, ruleName);
if (fs.existsSync(`${fullPath}.js`)) {
const ruleModule = require(fullPath) as { Rule: RuleConstructor } | undefined;
const ruleFullPath = getRuleFullPath(directory, ruleName);
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to rename the variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before it wasn't very explicit. The variable indicated that it would be the path, but it's just a part of the path that misses the extension.

To make it aligned with the method name I thought it would make sense to be more explicit about it.

Custom rule files are no longer being loaded with a hardcoded/fixed extenson (`.js`).

With this change all rule files will be resolved using Nodes path resolution. This means that the extension will be detected automatically and other extension loaders like `ts-node` can be used.

This means that rules can be also written in TypeScript and loaded through TS-Node.
@adidahiya adidahiya merged commit 90dd3f4 into palantir:master Aug 9, 2017
@devversion devversion deleted the fix/resolve-rules-using-node branch August 9, 2017 15:34
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.

4 participants