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

Add extends support #1016

Merged
merged 1 commit into from
Apr 1, 2016
Merged

Add extends support #1016

merged 1 commit into from
Apr 1, 2016

Conversation

unional
Copy link
Contributor

@unional unional commented Mar 5, 2016

I want to add some test in but don't know how. I have added a config.json file. Can you give me some pointers?

Also, please comment on the name and location of the functions.

I can adjust accordingly.


edit from @adidahiya: closes #997

@unional unional changed the title Add basic support Add basic extends support Mar 5, 2016
@unional
Copy link
Contributor Author

unional commented Mar 5, 2016

Will update the PR. It should merge the whole config. Not just the rules.

Do you want it to be done directly in loadConfigurationFromFile()?

@unional
Copy link
Contributor Author

unional commented Mar 6, 2016

Move extends logic into loadConfigurationFromPath

Test is skipped right now because I don't know how to get the config copied and referenced.

@unional
Copy link
Contributor Author

unional commented Mar 6, 2016

Currently it should support relative path. Once this is okey from you then I will code for array and package support.

@unional
Copy link
Contributor Author

unional commented Mar 6, 2016

Updated to support package and array. Definitely need testing

@jkillian
Copy link
Contributor

jkillian commented Mar 7, 2016

Thanks for the PR @unional! I haven't reviewed the code, but I like the extends syntax, simple and usable

@unional
Copy link
Contributor Author

unional commented Mar 7, 2016

Please take a look at the PR when you have time. While the test passed, when I try to use it using npm link (with vscode) it is not working. I want to figure out why.

Haven't test directly with tslint cli or gulp-tslint yet.

}
let parentRules = parentConfig.rules;
if (parentRules) {
for (let name in parentRules) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We prefer for (const name of Object.keys(parentRules)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

@unional
Copy link
Contributor Author

unional commented Mar 7, 2016

@jkillian , what would you suggest in adding test? You can see in https://github.com/palantir/tslint/pull/1016/files#diff-7adcebcb9ab1532bda827cc5be7bc94bR6, I'm routing back to the test source for the config file.

Any suggestion?

@jkillian
Copy link
Contributor

jkillian commented Mar 7, 2016

Well, the cwd for the tests should be the root of the project, so instead of doing __dirname + "/../../test/config/tslint-extends-rules.json" you should just be able to use ./test/config/tslint-extends-rules.json.

As far as testing with a package, I think the easiest solution will be to make a super-simple npm package and list it as a devDependency instead of trying to make a fake npm package somewhere.

@unional
Copy link
Contributor Author

unional commented Mar 7, 2016

Well, the cwd for the tests should be the root of the project, so instead of doing __dirname + "/../../test/config/tslint-extends-rules.json" you should just be able to use ./test/config/tslint-extends-rules.json.

That doesn't work because the config/* is not copied to build folder.

@unional
Copy link
Contributor Author

unional commented Mar 7, 2016

As far as testing with a package, I think the easiest solution will be to make a super-simple npm package and list it as a devDependency

I have one ready.

@jkillian
Copy link
Contributor

jkillian commented Mar 7, 2016

That doesn't work because the config/* is not copied to build folder.

But loadConfigurationFromPath uses fs.readFileSync, which if supplied a relative path, will load that path relative to the process's current working directory. In this case, the cwd for running tests should be the project root, and you should be able to use a relative path like ./test/config/tslint-extends-rules.json. Have you tried this and it doesn't work? Perhaps I'm not understanding something correctly here.

@unional
Copy link
Contributor Author

unional commented Mar 7, 2016

The cwd at that moment is ./build/test/, thus __dirname + "../../test/config/..." point to the right file.

Update: You are correct. it does start at root. 👍

@unional
Copy link
Contributor Author

unional commented Mar 8, 2016

What's your opinion on delete config.extends?

@unional
Copy link
Contributor Author

unional commented Mar 8, 2016

Don't have to review code at the moment. Found bugs. Since I can now run tests, I'll get them all work and do a rebase. Will ping you soon. 🌹

@jkillian
Copy link
Contributor

jkillian commented Mar 8, 2016

What's your opinion on delete config.extends?

Probably better to not do this I'd say

@unional
Copy link
Contributor Author

unional commented Mar 8, 2016

Alrighty.

@unional
Copy link
Contributor Author

unional commented Mar 8, 2016

Tests in, passed. Code rebased. Ready to go! @jkillian

let packageJson = JSON.parse(fileData);
return path.resolve(`node_modules/${relativeFilePath}/${packageJson.main}`);
} else {
return path.resolve(relativeTo, relativeFilePath);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want to change this to:

return getRelativePath(relativeFilePath, relativeTo);

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, let's use that 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.

ok

@unional
Copy link
Contributor Author

unional commented Mar 9, 2016

With this in, custom rules could be published as tslint-config-X too. Rules directory will be merged.:

// tslint-eslint-rules -> tslint-config-eslint
// main: tslint.json
{
  "rulesDirectory": "./<local rule relative path>",
  "rules": {
    // Can add default rules value here, and
    // disable conflicting core rules if needed.
  } 
}

@unional
Copy link
Contributor Author

unional commented Mar 31, 2016

Build failing because path.isAbsolute is not available in older node. Any suggestion?

@unional
Copy link
Contributor Author

unional commented Mar 31, 2016

@unional unional changed the title Add basic extends support Add extends support Mar 31, 2016
@jkillian
Copy link
Contributor

https://github.com/sindresorhus/path-is-absolute

Feel free to just add that as a dependency and use it. I'm not sure how the legal technicalities work of copying code form an MIT-licensed project into an Apache licensed one

@jkillian
Copy link
Contributor

Also, I like how simple & clean this code is ending up for such a powerful feature! This will be a great PR once it gets merged in

@unional
Copy link
Contributor Author

unional commented Mar 31, 2016

Ok

What you think on the exception message for resolve?

@unional
Copy link
Contributor Author

unional commented Mar 31, 2016

Also, for the typings for path-is-abs, I have added it to typings. But adding to DT might take a while. What's your approach? Custom?

@jkillian
Copy link
Contributor

What you think on the exception message for resolve?

Maybe something like this:

Error: Invalid "extends" configuration value - could not require "some-invalid-path". Review the Node lookup algorithm (https://nodejs.org/api/modules.html#modules_all_together) for the approximate method TSLint uses to find the referenced configuration file.

Also, for the typings for path-is-abs, I have added it to typings. But adding to DT might take a while. What's your approach? Custom?

Seems like a good solution to me, just drop it in typings/custom.

@unional
Copy link
Contributor Author

unional commented Mar 31, 2016

Ok. I drop the file under /custom-typings/ instead of /typings/custom to differentiate it from the typings installed by tsd.

Should be good to go V2!

@@ -78,7 +79,7 @@ export function findConfiguration(configFile: string, inputFilePath: string): IC
/**
* Searches for a TSLint configuration and returns the path to it.
* Could return undefined if not configuration is found.
* @param suppliedConfigFilePath A path to an known config file supplied by a user. Pass null here if
* @param suppliedConfigFilePath A path to an know``n config file supplied by a user. Pass null here if
Copy link
Contributor

Choose a reason for hiding this comment

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

stray ``

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch

UPDATE: DONE!

@jkillian
Copy link
Contributor

jkillian commented Apr 1, 2016

Sweet! Comments I left are just minor style things, I'll make the tweaks in a follow-up PR if you don't have a chance to do it yourself. Also, can you squash this into one commit now?

@unional
Copy link
Contributor Author

unional commented Apr 1, 2016

Sure

@unional
Copy link
Contributor Author

unional commented Apr 1, 2016

Regarding the declare namespace ... trick, it is neat and I haven't thought about that before.

I don't know if you aware of microsoft/TypeScript#7398. There may be problem in the future.

Just want to bring it up to your attention.

@jkillian jkillian merged commit 5b0eb9a into palantir:master Apr 1, 2016
@jkillian
Copy link
Contributor

jkillian commented Apr 1, 2016

🎈 🎉

@jkillian
Copy link
Contributor

jkillian commented Apr 2, 2016

I wrote a little post about this: http://palantir.github.io/tslint/2016/03/31/sharable-configurations-rules.html

Does it all seem correct to you @unional?

@unional
Copy link
Contributor Author

unional commented Apr 2, 2016

Seems like you go all the way to the complex scenario. 😄
Haven't try the tslint-crazy-config.js route myself. But given of what it is, it should work.

The simplest case would be:

// package.json
{
  "main": "tslint.json"  // or any json file
}
// tslint.json
{
  "rulesDirectory": "./rules",
  "rules": {
     // default values
  }
}

Then if you want to extend from someone else, just change your tslint.json to:

// tslint.json
{
  "extends": "some-other-package",
  "rulesDirectory": "./rules",
  "rules": {
     // my rules default
     // overriding `some-other-package` defaults
  }
}

@unional
Copy link
Contributor Author

unional commented Apr 2, 2016

In your example you are suggesting the package authors to share multiple configs. It is powerful, but it is also more advanced. 🌹

@unional
Copy link
Contributor Author

unional commented Apr 2, 2016

So you may organize the post into three sections:

  • how do I use it as the end user
  • how do I use it as a package author extending other packages (i.e. adding my own touch)
  • how do I use it as a package author to expose multiple configuration for my user to use.

My two cents.

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.

Extend configuration files
4 participants