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

Bugfix --project with --fix #2864

Merged
merged 5 commits into from
Jun 19, 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
8 changes: 8 additions & 0 deletions src/linter.ts
Expand Up @@ -187,11 +187,19 @@ class Linter {
fileNewSource = Replacement.applyFixes(oldSource, fileFixes);
}
fs.writeFileSync(filePath, fileNewSource);
this.updateProgram(filePath);
});

return source;
}

private updateProgram(sourceFilePath: string) {
if (this.program !== undefined && this.program.getSourceFile(sourceFilePath) !== undefined) {
const options = this.program.getCompilerOptions();
this.program = ts.createProgram(this.program.getRootFileNames(), options, ts.createCompilerHost(options, true), this.program);
}
}

private applyRule(rule: IRule, sourceFile: ts.SourceFile): RuleFailure[] {
try {
if (this.program !== undefined && isTypedRule(rule)) {
Expand Down
61 changes: 39 additions & 22 deletions test/executable/executableTests.ts
Expand Up @@ -28,10 +28,8 @@ const EXECUTABLE_PATH = path.resolve(EXECUTABLE_DIR, "npm-like-executable");
const TEMP_JSON_PATH = path.resolve(EXECUTABLE_DIR, "tslint.json");

describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
// tslint:disable:no-invalid-this
this.slow(3000); // the executable is JIT-ed each time it runs; avoid showing slowness warnings
this.timeout(4000);
// tslint:enable:no-invalid-this

describe("Files", () => {
it("exits with code 1 if no arguments passed", (done) => {
Expand Down Expand Up @@ -148,19 +146,22 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
describe("--fix flag", () => {
it("fixes multiple rules without overwriting each other", (done) => {
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) => {
const content = fs.readFileSync(tempFile, "utf8");
// compare against file name which will be returned by formatter (used in TypeScript)
const denormalizedFileName = denormalizeWinPath(tempFile);
fs.unlinkSync(tempFile);
assert.strictEqual(content, "import * as y from \"a_long_module\";\nimport * as x from \"b\";\n");
assert.isNull(err, "process should exit without an error");
assert.strictEqual(stdout, `Fixed 2 error(s) in ${denormalizedFileName}`);
done();
fs.createReadStream("test/files/multiple-fixes-test/multiple-fixes.test.ts")
.pipe(fs.createWriteStream(tempFile))
.on("finish", () => {
execCli(["-c", "test/files/multiple-fixes-test/tslint.json", tempFile, "--fix"],
(err, stdout) => {
const content = fs.readFileSync(tempFile, "utf8");
// compare against file name which will be returned by formatter (used in TypeScript)
const denormalizedFileName = denormalizeWinPath(tempFile);
fs.unlinkSync(tempFile);
assert.strictEqual(content, "import * as y from \"a_long_module\";\nimport * as x from \"b\";\n");
assert.isNull(err, "process should exit without an error");
assert.strictEqual(stdout, `Fixed 2 error(s) in ${denormalizedFileName}`);
done();
});
});
});
}).timeout(8000);
});

describe("--force flag", () => {
Expand Down Expand Up @@ -349,18 +350,22 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});
});

it("can handle multiple '--exclude' globs", (done) => {
it("can apply fixes from multiple rules", (done) => {
fs.writeFileSync("test/files/project-multiple-fixes/testfile.test.ts",
fs.readFileSync("test/files/project-multiple-fixes/before.test.ts", "utf-8"));
execCli(
[
"-c", "test/files/multiple-excludes/tslint.json",
"--exclude", "'test/files/multiple-excludes/invalid.test.ts'",
"--exclude", "'test/files/multiple-excludes/invalid2*'",
"'test/files/multiple-excludes/**.ts'",
], (err) => {
[ "-p", "test/files/project-multiple-fixes/", "--fix"],
(err) => {
const actual = fs.readFileSync("test/files/project-multiple-fixes/testfile.test.ts", "utf-8");
fs.unlinkSync("test/files/project-multiple-fixes/testfile.test.ts");
assert.isNull(err, "process should exit without an error");
assert.strictEqual(
actual,
fs.readFileSync("test/files/project-multiple-fixes/after.test.ts", "utf-8"),
);
done();
});
});
}).timeout(8000);
});

describe("--type-check", () => {
Expand Down Expand Up @@ -428,6 +433,18 @@ describe("Executable", function(this: Mocha.ISuiteCallbackContext) {
});
});

it("can handle multiple '--exclude' globs", (done) => {
execCli(
[
"-c", "test/files/multiple-excludes/tslint.json",
"--exclude", "'test/files/multiple-excludes/invalid.test.ts'",
"--exclude", "'test/files/multiple-excludes/invalid2*'",
"'test/files/multiple-excludes/**.ts'",
], (err) => {
assert.isNull(err, "process should exit without an error");
done();
});
});
});
});

Expand Down
1 change: 1 addition & 0 deletions test/files/project-multiple-fixes/.gitignore
@@ -0,0 +1 @@
/testfile.test.ts
22 changes: 22 additions & 0 deletions test/files/project-multiple-fixes/after.test.ts
@@ -0,0 +1,22 @@
declare var winston: any;

const DefaultOptions = {
transports: [
new winston.transports.Console({
json: true,
colorize: true,
stringify: true,
})],
rewriters: [
(level, message, meta) => {
if (meta)
{
meta.timeStamp = new Date();
}
return meta;
},
],

};

export const Logger = new winston.Logger(DefaultOptions);
23 changes: 23 additions & 0 deletions test/files/project-multiple-fixes/before.test.ts
@@ -0,0 +1,23 @@
declare var winston: any;

const DefaultOptions = {
transports: [
new winston.transports.Console({
json: true,
colorize: true,
stringify:true
})],
rewriters:[
(level,message,meta)=>{
if(meta)
{
meta.timeStamp = new Date();
}
return meta;
}
]


};

export const Logger = new winston.Logger(DefaultOptions);
6 changes: 6 additions & 0 deletions test/files/project-multiple-fixes/tsconfig.json
@@ -0,0 +1,6 @@
{
"exclude": [
"before.test.ts",
"after.test.ts"
]
}
23 changes: 23 additions & 0 deletions test/files/project-multiple-fixes/tslint.json
@@ -0,0 +1,23 @@
{
"rules": {
"no-consecutive-blank-lines": true,
"no-trailing-whitespace": true,
"whitespace": [
true,
"check-branch",
"check-operator",
"check-preblock",
"check-separator",
"check-decl",
"check-module",
"check-type",
"check-typecast"
],
"trailing-comma": [
true, {
"multiline": "always"
}
],
"eofline": true
}
}