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

Support yaml configuration (#1598) #3433

Merged
merged 4 commits into from
Nov 8, 2017
Merged

Conversation

jhanschoo
Copy link
Contributor

@jhanschoo jhanschoo commented Nov 1, 2017

PR checklist

Overview of change:

Implement support for yaml. The "js-yaml" library is already being used in documentation, so it's a simple matter of moving a devDependency to a dependency. I know there's talk about using cosmicConfig, but that would require rewriting a number of tests, and this was way simpler haha

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

Let me know what other tests I should write.

CHANGELOG.md entry:

[api] Support for YAML config files.

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @jhanschoo! 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.

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.

Overall LGTM

I left some suggestions.

// As per eslint convention, yaml config is used if present
// over json config
export const JSON_CONFIG_FILENAME = "tslint.json";
export const CONFIG_FILENAMES = ["tslint.yaml", "tslint.yml", JSON_CONFIG_FILENAME];
Copy link
Contributor

Choose a reason for hiding this comment

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

consider moving the json file to the first position to avoid unnecessary file lookups

I don't think we need to adopt the behavior of ESLint

// TODO: remove in v6.0.0
// Try reading in the entire directory and looking for a file with different casing.
const filenameLower = filename.toLowerCase();
const result = fs.readdirSync(cwd).find((entry) => entry.toLowerCase() === filenameLower);
Copy link
Contributor

Choose a reason for hiding this comment

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

this reads the directory 3 times, each time only trying to match one file name.
instead it should read the directory once and compare to all possible file names

const fileContent = stripComments(fs.readFileSync(resolvedConfigFilePath)
.toString()
.replace(/^\uFEFF/, ""));
if (resolvedConfigFileExt.match(/\.(json|ya?ml)/) !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

prefer /^\.(json|ya?ml)$/.test(resolvedConfigFileExt)

rawConfigFile = JSON.parse(fileContent) as RawConfigFile;
if (resolvedConfigFileExt === ".json") {
rawConfigFile = JSON.parse(stripComments(fileContent)) as RawConfigFile;
} else if (resolvedConfigFileExt.match(/ya?ml/) !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this condition will always be true. you can remove the condition

}) as RawConfigFile;
} else {
// throw error for static analysis purpose; should not happen
throw new Error("File format not supported yet.");
Copy link
Contributor

Choose a reason for hiding this comment

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

this is dead code

package.json Outdated
@@ -39,10 +39,11 @@
"dependencies": {
"babel-code-frame": "^6.22.0",
"builtin-modules": "^1.1.1",
"chalk": "^2.1.0",
"chalk": "~2.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll look into that and open a separate PR. The change shouldn't be necesary if you use yarn instead of npm

showWarningOnce(`Using mixed case tslint.json is deprecated. Found: ${path.join(cwd, result)}`);
// TODO: remove in v6.0.0
// Try reading in the entire directory and looking for a file with different casing.
const filenameLower = filename.toLowerCase();
Copy link
Contributor

Choose a reason for hiding this comment

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

you can probably remove this line while you are here. we already know filename is lowercase

@ajafff
Copy link
Contributor

ajafff commented Nov 1, 2017

Personally I'd prefer tslint.json having priority over tslint.ya?ml: json > yaml > yml

@adidahiya want to share your opinion on this?

@jhanschoo
Copy link
Contributor Author

Ok thanks! I'll look into incorporating them in the weekend.

for (const filename of filenames) {
const index = dirFiles.indexOf(filename);
if (index > -1) {
return dirFiles[index];
Copy link
Contributor

Choose a reason for hiding this comment

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

or just return filename;

.toString()
.replace(/^\uFEFF/, ""));
if (/\.(json|ya?ml)/.test(resolvedConfigFileExt)) {
const fileContent = fs.readFileSync(resolvedConfigFilePath).toString().replace(/^\uFEFF/, "");
Copy link
Contributor

Choose a reason for hiding this comment

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

while you are already touching this code: if you pass "utf8" as second argument to fs.readFileSync, you can simply remove the .toString() call

assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 1, "error code should be 1");

assert.include(stderr, "Failed to load", "stderr should contain notification about failing to load json");
Copy link
Contributor

Choose a reason for hiding this comment

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

s/json/config/

package.json Outdated
"license": "Apache-2.0",
"engines": {
"node": ">=4.8.0"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

please revert the style-only changes in this file

if (fs.existsSync(path.join(cwd, filename))) {
return filename;
const dirFiles = fs.readdirSync(cwd);
for (const filename of filenames) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this might be unexpected as it prefers tslint.yaml over Tslint.json

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.

overall the approach here seems reasonable and is a minimal change 👍 looks like js-yaml is a relatively light dependency, so i'm cool with taking it now

@@ -4,12 +4,12 @@ title: Configuring TSLint
permalink: /usage/configuration/
---

### tslint.json
### The `tslint` File
Copy link
Contributor

Choose a reason for hiding this comment

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

Please just name this "TSLint Configuration"

rawConfigFile = JSON.parse(fileContent) as RawConfigFile;
if (resolvedConfigFileExt === ".json") {
rawConfigFile = JSON.parse(stripComments(fileContent)) as RawConfigFile;
} else { // /\.ya?ml/.test(resolvedConfigFileExt) === true
Copy link
Contributor

Choose a reason for hiding this comment

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

what's this comment doing here? if it's stray code, please delete. if it's explanatory, write some additional words at the beginning (don't start with "/") and put it on its own line

@jhanschoo
Copy link
Contributor Author

Alright give me a few mins. Should I squash my commits?

@ajafff
Copy link
Contributor

ajafff commented Nov 7, 2017

@jhanschoo no need to sqash, we do it anyway while merging

@jhanschoo
Copy link
Contributor Author

Ok thanks!

@ajafff ajafff merged commit 6d3f78e into palantir:master Nov 8, 2017
@ajafff
Copy link
Contributor

ajafff commented Nov 8, 2017

Thanks @jhanschoo

@jhanschoo
Copy link
Contributor Author

Thanks!

@@ -63,7 +64,10 @@ export interface IConfigurationLoadResult {
results?: IConfigurationFile;
}

export const CONFIG_FILENAME = "tslint.json";
Copy link
Contributor

Choose a reason for hiding this comment

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

this could actually break some users of our API:

import * as Lint from "tslint";
Lint.Configuration.CONFIG_FILENAME; // no longer valid

I doubt this is used in real world code, so I don't know if this is worth fixing.

adidahiya pushed a commit that referenced this pull request Nov 28, 2017
Restores the removed export `CONFIG_FILENAME` to avoid breaking API users.
Ref: #3433 (comment)

[no-log]
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Restores the removed export `CONFIG_FILENAME` to avoid breaking API users.
Ref: palantir#3433 (comment)

[no-log]
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