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

Add outputAbsolutePaths option #2667

Merged
merged 8 commits into from May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage/cli/index.md
Expand Up @@ -40,6 +40,7 @@ Options:
-h, --help display detailed help
-i, --init generate a tslint.json config file in the current working directory
-o, --out output file
--outputAbsolutePaths whether or not outputted file paths are absolute
-p, --project tsconfig.json file
-r, --rules-dir rules directory
-s, --formatters-dir formatters directory
Expand Down Expand Up @@ -89,6 +90,9 @@ tslint accepts the following command-line options:
A filename to output the results to. By default, tslint outputs to
stdout, which is usually the console where you're running it from.

--outputAbsolutePaths:
Indicates whether or not outputted file paths are absolute paths.

-r, --rules-dir:
An additional rules directory, for user-created rules.
tslint will always check its default rules directory, in
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -65,6 +65,7 @@
"github": "^8.1.1",
"js-yaml": "^3.7.0",
"json-stringify-pretty-compact": "^1.0.3",
"memory-streams": "^0.1.2",
"mocha": "^3.2.0",
"npm-run-all": "^3.1.0",
"nyc": "^10.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/linter.ts
Expand Up @@ -178,7 +178,7 @@ class Linter {
const fixesByFile = createMultiMap(fixableFailures, (f) => [f.getFileName(), f.getFix()!]);
fixesByFile.forEach((fileFixes, filePath) => {
let fileNewSource: string;
if (filePath === sourceFilePath) {
if (path.resolve(filePath) === path.resolve(sourceFilePath)) {
source = Replacement.applyFixes(source, fileFixes);
fileNewSource = source;
} else {
Expand Down
24 changes: 21 additions & 3 deletions src/runner.ts
Expand Up @@ -78,6 +78,11 @@ export interface IRunnerOptions {
*/
out?: string;

/**
* Whether to output absolute paths
*/
outputAbsolutePaths?: boolean;

/**
* tsconfig.json file.
*/
Expand Down Expand Up @@ -161,8 +166,15 @@ export class Runner {
// emit any error messages
let message = ts.DiagnosticCategory[diag.category];
if (diag.file) {
const {line, character} = diag.file.getLineAndCharacterOfPosition(diag.start);
message += ` at ${diag.file.fileName}:${line + 1}:${character + 1}:`;
const { line, character } = diag.file.getLineAndCharacterOfPosition(diag.start);
let file: string;
const currentDirectory = program!.getCurrentDirectory();
if (this.options.outputAbsolutePaths) {
file = path.resolve(currentDirectory, diag.file.fileName);
} else {
file = path.relative(currentDirectory, diag.file.fileName);
}
message += ` at ${file}:${line + 1}:${character + 1}:`;
}
message += " " + ts.flattenDiagnosticMessageText(diag.messageText, "\n");
return message;
Expand Down Expand Up @@ -190,7 +202,13 @@ export class Runner {
// remove single quotes which break matching on Windows when glob is passed in single quotes
.map(Runner.trimSingleQuotes)
.map((file: string) => glob.sync(file, { ignore: ignorePatterns, nodir: true }))
.reduce((a: string[], b: string[]) => a.concat(b), []);
.reduce((a: string[], b: string[]) => a.concat(b), [])
.map((file: string) => {
if (this.options.outputAbsolutePaths) {
return path.resolve(file);
}
return path.relative(process.cwd(), file);
});

try {
this.processFiles(onComplete, files, program);
Expand Down
6 changes: 6 additions & 0 deletions src/tslint-cli.ts
Expand Up @@ -33,6 +33,7 @@ interface Argv {
init?: boolean;
o?: string;
out?: string;
outputAbsolutePaths?: boolean;
p?: string;
project?: string;
r?: string;
Expand Down Expand Up @@ -95,6 +96,10 @@ const processed = optimist
describe: "output file",
type: "string",
},
"outputAbsolutePaths": {
describe: "whether or not outputted file paths are absolute",
type: "boolean",
},
"p": {
alias: "project",
describe: "tsconfig.json file",
Expand Down Expand Up @@ -240,6 +245,7 @@ const options: IRunnerOptions = {
formattersDirectory: argv.s,
init: argv.init,
out: argv.out,
outputAbsolutePaths: argv.outputAbsolutePaths,
project: argv.p,
rulesDirectory: argv.r,
test: argv.test,
Expand Down
2 changes: 1 addition & 1 deletion test/executable/executableTests.ts
Expand Up @@ -146,7 +146,7 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {

describe("--fix flag", () => {
it("fixes multiple rules without overwriting each other", (done) => {
const tempFile = createTempFile("ts");
const tempFile = path.relative(process.cwd(), createTempFile("ts"));
fs.createReadStream("test/files/multiple-fixes-test/multiple-fixes.test.ts").pipe(fs.createWriteStream(tempFile));
execCli(["-c", "test/files/multiple-fixes-test/tslint.json", tempFile, "--fix"],
(err, stdout) => {
Expand Down
52 changes: 52 additions & 0 deletions test/runner/runnerTests.ts
@@ -0,0 +1,52 @@
/*
* Copyright 2017 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 { assert } from "chai";
import * as streams from "memory-streams";
import { Runner } from "../../src/runner";
import { IRunnerOptions } from "./../../src/runner";

const customRulesOptions: IRunnerOptions = {
config: "./test/config/tslint-custom-rules.json",
files: ["src/test.ts"],
rulesDirectory: "./test/files/custom-rules",
};

describe("Runner Tests", () => {
it("outputs absolute path with --outputAbsolutePaths", () => {
const output = runLint({ ...customRulesOptions, outputAbsolutePaths: true });

// match either a path starting with `/` or something like `C:`
assert.isTrue(/ERROR: (\/|\w:)/.test(output));
});

it("outputs relative path without --outputAbsolutePaths", () => {
const output = runLint(customRulesOptions);
assert.include(output, "ERROR: src/");
});
});

function runLint(options: IRunnerOptions, callback?: (status: number) => void) {
const output = new streams.WritableStream();
const runner = new Runner(options, output);
if (callback === undefined) {
callback = () => {
// do nothing
};
}
runner.run(callback);
return output.toString();
}
29 changes: 28 additions & 1 deletion yarn.lock
Expand Up @@ -321,6 +321,10 @@ core-js@^2.4.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.4.1.tgz#4de911e667b0eae9124e34254b53aea6fc618d3e"

core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

cross-spawn@^4, cross-spawn@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41"
Expand Down Expand Up @@ -637,7 +641,7 @@ inflight@^1.0.4:
once "^1.3.0"
wrappy "1"

inherits@2:
inherits@2, inherits@~2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

Expand Down Expand Up @@ -737,6 +741,10 @@ is-utf8@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72"

isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

isarray@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
Expand Down Expand Up @@ -933,6 +941,12 @@ md5-o-matic@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/md5-o-matic/-/md5-o-matic-0.1.1.tgz#822bccd65e117c514fab176b25945d54100a03c3"

memory-streams@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/memory-streams/-/memory-streams-0.1.2.tgz#273ff777ab60fec599b116355255282cca2c50c2"
dependencies:
readable-stream "~1.0.2"

merge-source-map@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/merge-source-map/-/merge-source-map-1.0.3.tgz#da1415f2722a5119db07b14c4f973410863a2abf"
Expand Down Expand Up @@ -1212,6 +1226,15 @@ read-pkg@^1.0.0, read-pkg@^1.1.0:
normalize-package-data "^2.3.2"
path-type "^1.0.0"

readable-stream@~1.0.2:
version "1.0.34"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.34.tgz#125820e34bc842d2f2aaafafe4c2916ee32c157c"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"

regenerator-runtime@^0.10.0:
version "0.10.5"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658"
Expand Down Expand Up @@ -1375,6 +1398,10 @@ string.prototype.padend@^3.0.0:
es-abstract "^1.4.3"
function-bind "^1.0.2"

string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"

strip-ansi@^3.0.0, strip-ansi@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf"
Expand Down