This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Use resolve
instead of Node.JS require for resolving configuration files
#1172
Merged
jkillian
merged 21 commits into
palantir:master
from
janslow:feature/#1171-extends-resolve
Apr 29, 2016
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
19d6068
Add `resolve` dependency (and custom typings)
janslow ac0f51e
Use resolve instead of require in resolveConfigurationPaths
janslow 49d6b1e
Simplify resolveConfigurationPath
janslow 99110fe
path-is-absolute is no longer needed.
janslow eb4dde7
Install test packages in subdirectory, so they aren't in the scope of…
janslow 40dece1
Run `npm install` in test config directory to install test packages.
janslow f74bd30
Fix code style issue
janslow fa0b947
Add dev dependency on npm for grunt run:installTestDeps task
janslow fdb7fd8
Revert "Add dev dependency on npm for grunt run:installTestDeps task"
janslow 5663683
Replace run:installTestDeps task with npm-command:test
janslow 5f787b6
Fix lint errors in Gruntfile
janslow 947c0f0
Resolve config file relative to the cwd if it can't be found relative…
janslow 4cb1fdd
Make rules in tslint-test-custom-rules package valid rules (copied fr…
janslow f9a07d1
Add CLI test with a relative extend config
janslow b4b2e1a
Add test config package
janslow a439cac
Add CLI test for extending a package which is installed in tslint.
janslow b35f877
Make test packages private
janslow 863794a
Fix entry point of test packages
janslow d4f95de
Simplify non-relative test package
janslow 2496581
Merge branch 'master' into feature/#1171-extends-resolve
janslow 41cf934
Add unit test for loadConfigurationFromPath with configs outside of t…
janslow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
declare module "resolve" { | ||
interface ResolveOptions { | ||
basedir?: string; | ||
extensions?: string[]; | ||
paths?: string[]; | ||
moduleDirectory?: string | string[]; | ||
} | ||
interface AsyncResolveOptions extends ResolveOptions { | ||
package?: any; | ||
readFile?: Function; | ||
isFile?: (file: string, cb: Function) => void; | ||
packageFilter?: Function; | ||
pathFilter?: Function; | ||
} | ||
interface SyncResolveOptions extends ResolveOptions { | ||
readFile?: Function; | ||
isFile?: (file: string) => boolean; | ||
packageFilter?: Function; | ||
} | ||
interface ResolveFunction { | ||
(id: string, cb: (err: any, res: string, pkg: any) => void): void; | ||
(id: string, opts: AsyncResolveOptions, cb: (err: any, res: string, pkg: any) => void): void; | ||
sync(id: string, opts?: SyncResolveOptions): string; | ||
isCore(pkg: any): any; | ||
} | ||
|
||
const resolve: ResolveFunction; | ||
export = resolve; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ expectOut () { | |
msg=$3 | ||
|
||
nodeV=`node -v` | ||
|
||
# if Node 0.10.*, node will sometimes exit with status 8 when an error is thrown | ||
if [[ $expect != $actual || $nodeV == v0.10.* && $expect == 1 && $actual == 8 ]] ; then | ||
echo "$msg: expected $expect got $actual" | ||
|
@@ -70,6 +70,18 @@ expectOut $? 0 "tslint with with -r pointing to custom rules did not find lint f | |
./bin/tslint -c test/config/tslint-almost-empty.json src/tslint.ts | ||
expectOut $? 0 "-c relative path without ./ did not work" | ||
|
||
# make sure calling tslint with a config file which extends a package relative to the config file works | ||
./bin/tslint -c test/config/tslint-extends-package-no-mod.json src/tslint.ts | ||
expectOut $? 0 "tslint (with config file extending relative package) did not work" | ||
|
||
# check that a tslint file (outside of the resolution path of the tslint package) can resolve a package that is | ||
# installed as a dependency of tslint. See palantir/tslint#1172 for details. | ||
tmpDir=$(mktemp -d) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the version of |
||
echo "{ \"extends\": \"tslint-test-config-non-relative\" }" > $tmpDir/tslint.json | ||
./bin/tslint -c $tmpDir/tslint.json src/tslint.ts | ||
expectOut $? 0 "tslint (with config file extending package in tslint scope) did not work" | ||
rm -r $tmpDir | ||
|
||
# make sure tslint --init generates a file | ||
cd ./bin | ||
if [ -f tslint.json ]; then | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "tslint-test-configs", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"tslint-test-config": "../external/tslint-test-config", | ||
"tslint-test-custom-rules": "../external/tslint-test-custom-rules" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "tslint-test-config-non-relative", | ||
"description": "A test package with a tslint config which is installed in the tslint.", | ||
"version": "0.0.1", | ||
"private": true, | ||
"main": "tslint.json", | ||
"scripts": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"class-name": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"name": "tslint-test-config", | ||
"version": "0.0.1", | ||
"main": "index.js", | ||
"private": true, | ||
"main": "tslint.json", | ||
"scripts": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"name": "tslint-test-custom-rules", | ||
"version": "0.0.1", | ||
"private": true, | ||
"main": "tslint.json", | ||
"scripts": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var Lint = require("tslint/lib/lint"); | ||
var Rule = (function (_super) { | ||
__extends(Rule, _super); | ||
function Rule() { | ||
_super.apply(this, arguments); | ||
} | ||
Rule.prototype.apply = function (sourceFile) { | ||
return this.applyWithWalker(new NoFailWalker(sourceFile, this.getOptions())); | ||
}; | ||
return Rule; | ||
})(Lint.Rules.AbstractRule); | ||
exports.Rule = Rule; | ||
var NoFailWalker = (function (_super) { | ||
__extends(NoFailWalker, _super); | ||
function NoFailWalker() { | ||
_super.apply(this, arguments); | ||
} | ||
NoFailWalker.prototype.visitSourceFile = function (node) { | ||
// yay, no failures! | ||
}; | ||
return NoFailWalker; | ||
})(Lint.RuleWalker); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var Lint = require("tslint/lib/lint"); | ||
var Rule = (function (_super) { | ||
__extends(Rule, _super); | ||
function Rule() { | ||
_super.apply(this, arguments); | ||
} | ||
Rule.prototype.apply = function (sourceFile) { | ||
return this.applyWithWalker(new NoFailWalker(sourceFile, this.getOptions())); | ||
}; | ||
return Rule; | ||
})(Lint.Rules.AbstractRule); | ||
exports.Rule = Rule; | ||
var NoFailWalker = (function (_super) { | ||
__extends(NoFailWalker, _super); | ||
function NoFailWalker() { | ||
_super.apply(this, arguments); | ||
} | ||
NoFailWalker.prototype.visitSourceFile = function (node) { | ||
// yay, no failures! | ||
}; | ||
return NoFailWalker; | ||
})(Lint.RuleWalker); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
var Lint = require("tslint/lib/lint"); | ||
var Rule = (function (_super) { | ||
__extends(Rule, _super); | ||
function Rule() { | ||
_super.apply(this, arguments); | ||
} | ||
Rule.prototype.apply = function (sourceFile) { | ||
return this.applyWithWalker(new NoFailWalker(sourceFile, this.getOptions())); | ||
}; | ||
return Rule; | ||
})(Lint.Rules.AbstractRule); | ||
exports.Rule = Rule; | ||
var NoFailWalker = (function (_super) { | ||
__extends(NoFailWalker, _super); | ||
function NoFailWalker() { | ||
_super.apply(this, arguments); | ||
} | ||
NoFailWalker.prototype.visitSourceFile = function (node) { | ||
// yay, no failures! | ||
}; | ||
return NoFailWalker; | ||
})(Lint.RuleWalker); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love how you simplified this code!
I'm wondering if we should also attempt to load with regular
require.resolve
as well. I.e. does it also make sense to look for the node module relative to the installed location of TSLint? I think it does, but only if the "path" specified is not a path but instead a package referenceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would mean it's not the same as the [
require.resolve() algorithm](https://nodejs.org/dist/latest-v4.x/docs/api/modules.html#modules_all_together). Before this pull request
Y(in the algorithm) was always the location of the tslint package, now it's the location of the parent
tslint.json` file (or the tslint package the first time).It's similar to how you'd expect
node -e "require('foo')"
to fail iffoo
was installed globally but not locally.However, it's arguably a breaking change, so I'm happy to add a fallback which resolves with
require.resolve
to avoid issues for other users.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with you that ideally
extends
references in atslint.json
file should only reference packages installed where the file is, but I can see this being a situation where people are relying on other cases, so let's prevent the breaking change here