Skip to content

Commit

Permalink
Configure format from file (palantir#4155)
Browse files Browse the repository at this point in the history
* Add ability to configure formatter from config file

* Add test case

* Fix lint issues
  • Loading branch information
nomcopter authored and johnwiseheart committed Sep 21, 2018
1 parent 0437cd9 commit e19d3b7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/usage/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ A path to a directory or an array of paths to directories of [custom rules][2].
* `defaultSeverity?: "error" | "warning" | "off"`: The severity level that is applied to rules in this config file as well as rules in any inherited config files which have their severity set to "default". If undefined, "error" is used as the defaultSeverity.
* `linterOptions?: { exclude?: string[] }`:
- `exclude: string[]`: An array of globs. Any file matching these globs will not be linted. All exclude patterns are relative to the configuration file they were specified in.
- `format: string`: Default [lint formatter][4]

`tslint.json` configuration files may have JavaScript-style `// single-line` and `/* multi-line */` comments in them (even though this is technically invalid JSON). If this confuses your syntax highlighter, you may want to switch it to JavaScript format.

Expand Down Expand Up @@ -129,6 +130,7 @@ Some commonly used custom rule packages in the TSLint community are listed in th
[1]: {{site.baseurl | append: "/usage/third-party-tools"}}
[2]: {{site.baseurl | append: "/develop/custom-rules"}}
[3]: {{site.baseurl | append: "/rules"}}
[4]: {{site.baseurl | append: "/formatters"}}
[rule-ban]: {{site.baseurl | append: "/rules/ban"}}
[rule-import-blacklist]: {{site.baseurl | append: "/rules/import-blacklist"}}
[rule-file-header]: {{site.baseurl | append: "/rules/file-header"}}
19 changes: 15 additions & 4 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface IConfigurationFile {
*/
linterOptions?: Partial<{
exclude: string[];
format: string;
}>;

/**
Expand Down Expand Up @@ -617,13 +618,23 @@ export function parseConfigFile(
raw: RawConfigFile["linterOptions"],
dir?: string
): IConfigurationFile["linterOptions"] {
if (raw === undefined || raw.exclude === undefined) {
if (raw === undefined) {
return {};
}
return {
exclude: arrayify(raw.exclude).map(
pattern => (dir === undefined ? path.resolve(pattern) : path.resolve(dir, pattern))
)
...(raw.exclude !== undefined
? {
exclude: arrayify(raw.exclude).map(
pattern =>
dir === undefined ? path.resolve(pattern) : path.resolve(dir, pattern)
)
}
: {}),
...(raw.format !== undefined
? {
format: raw.format
}
: {})
};
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,21 @@ function resolveGlobs(files: string[], ignore: string[], outputAbsolutePaths: bo
}

async function doLinting(options: Options, files: string[], program: ts.Program | undefined, logger: Logger): Promise<LintResult> {
let configFile =
options.config !== undefined ? findConfiguration(options.config).results : undefined;

let formatter = options.format;
if (formatter === undefined) {
formatter =
configFile && configFile.linterOptions && configFile.linterOptions.format
? configFile.linterOptions.format
: "prose";
}

const linter = new Linter(
{
fix: !!options.fix,
formatter: options.format,
formatter,
formattersDirectory: options.formattersDirectory,
quiet: !!options.quiet,
rulesDirectory: options.rulesDirectory,
Expand All @@ -254,7 +265,6 @@ async function doLinting(options: Options, files: string[], program: ts.Program
);

let lastFolder: string | undefined;
let configFile = options.config !== undefined ? findConfiguration(options.config).results : undefined;

for (const file of files) {
if (options.config === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/tslintCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ run(
files: arrayify(commander.args),
fix: argv.fix,
force: argv.force,
format: argv.format === undefined ? "prose" : argv.format,
format: argv.format,
formattersDirectory: argv.formattersDir,
init: argv.init,
out: argv.out,
Expand Down
16 changes: 16 additions & 0 deletions test/config/tslint-custom-rules-with-dir-and-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"rulesDirectory": "../files/custom-rules/",
"jsRules": {
"always-fail": {
"severity": "error"
}
},
"rules": {
"always-fail": {
"severity": "error"
}
},
"linterOptions": {
"format": "tslint-test-custom-formatter"
}
}
38 changes: 30 additions & 8 deletions test/executable/executableTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,40 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});

describe("Custom formatters", () => {
it("can be loaded from node_modules", (done) => {
const createFormatVerifier = (done: MochaDone): ExecFileCallback => (err, stdout) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 2, "error code should be 2");
assert.include(
stdout,
"hello from custom formatter",
"stdout should contain output of custom formatter"
);
done();
};

it("can be loaded from node_modules", done => {
execCli(
["-c", "tslint-custom-rules-with-dir.json", "../../src/test.ts", "-t", "tslint-test-custom-formatter"],
[
"-c",
"tslint-custom-rules-with-dir.json",
"../../src/test.ts",
"-t",
"tslint-test-custom-formatter"
],
{
cwd: "./test/config",
cwd: "./test/config"
},
(err, stdout) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 2, "error code should be 2");
assert.include(stdout, "hello from custom formatter", "stdout should contain output of custom formatter");
done();
createFormatVerifier(done)
);
});

it("can be specified from config", done => {
execCli(
["-c", "tslint-custom-rules-with-dir-and-format.json", "../../src/test.ts"],
{
cwd: "./test/config"
},
createFormatVerifier(done)
);
});
});
Expand Down

0 comments on commit e19d3b7

Please sign in to comment.