Skip to content

Commit

Permalink
Add new formatter: fileslist (palantir#1558)
Browse files Browse the repository at this point in the history
This formatter does not output any lint errors. Instead, it lists all
of the files that had lint errors.
This is useful for creating lists of files that need to be taken care
of, or for bulk-opening all of the files in an editor from CLI.
  • Loading branch information
nomaed authored and adidahiya committed Sep 20, 2016
1 parent 6cb1f11 commit 5cc19bd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/formatters/fileslistFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {AbstractFormatter} from "../language/formatter/abstractFormatter";
import {RuleFailure} from "../language/rule/rule";

export class Formatter extends AbstractFormatter {
public format(failures: RuleFailure[]): string {
const files: string[] = [];
let currentFile: string;

for (const failure of failures) {
const fileName = failure.getFileName();
if (fileName !== currentFile) {
files.push(fileName);
currentFile = fileName;
}
}

return files.join("\n") + "\n";
}
}
1 change: 1 addition & 0 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export { Formatter as PmdFormatter } from "./pmdFormatter";
export { Formatter as ProseFormatter } from "./proseFormatter";
export { Formatter as VerboseFormatter } from "./verboseFormatter";
export { Formatter as StylishFormatter } from "./stylishFormatter";
export { Formatter as FileslistFormatter } from "./fileslistFormatter";
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"configs/latest.ts",
"configs/recommended.ts",
"formatters/checkstyleFormatter.ts",
"formatters/fileslistFormatter.ts",
"formatters/index.ts",
"formatters/jsonFormatter.ts",
"formatters/msbuildFormatter.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ let processed = optimist
t: {
alias: "format",
default: "prose",
describe: "output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso)",
describe: "output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist)",
},
test: {
describe: "test that tslint produces the correct output for the specified directory",
Expand Down
9 changes: 9 additions & 0 deletions test/files/formatters/fileslistFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module FilesListModule {
export class FilesListClass {
name: string;

constructor(name: string) {
this.name = name;
}
}
}
50 changes: 50 additions & 0 deletions test/formatters/fileslistFormatterTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2013 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as ts from "typescript";

import {IFormatter, RuleFailure, TestUtils} from "../lint";

describe("Files-list Formatter", () => {
const TEST_FILE = "formatters/fileslistFormatter.test.ts";
let sourceFile: ts.SourceFile;
let formatter: IFormatter;

before(() => {
const Formatter = TestUtils.getFormatter("fileslist");
sourceFile = TestUtils.getSourceFile(TEST_FILE);
formatter = new Formatter();
});

it("formats failures", () => {
// this part really doesn't matter, as long as we get some failure`
const failures = [
new RuleFailure(sourceFile, 0, 1, "first failure", "first-name"),
new RuleFailure(sourceFile, 32, 36, "last failure", "last-name"),
];

// we only print file-names in this formatter
const expectedResult = TEST_FILE + "\n";

const actualResult = formatter.format(failures);
assert.equal(actualResult, expectedResult);
});

it("handles no failures", () => {
const result = formatter.format([]);
assert.equal(result, "\n");
});
});
2 changes: 2 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"../src/formatterLoader.ts",
"../src/formatters.ts",
"../src/formatters/checkstyleFormatter.ts",
"../src/formatters/fileslistFormatter.ts",
"../src/formatters/index.ts",
"../src/formatters/jsonFormatter.ts",
"../src/formatters/msbuildFormatter.ts",
Expand Down Expand Up @@ -163,6 +164,7 @@
"utils.ts",
"utilsTests.ts",
"formatters/checkstyleFormatterTests.ts",
"formatters/fileslistFormatterTests.ts",
"formatters/externalFormatterTests.ts",
"formatters/jsonFormatterTests.ts",
"formatters/msbuildFormatterTests.ts",
Expand Down

0 comments on commit 5cc19bd

Please sign in to comment.