This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Add text replacement fixes for rules #1423
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b63de85
Add text replacement fixes for rules
ScottSWu bb336e7
Review changes
ScottSWu a8df88b
Changed methods to getters, removed description from fixes
ScottSWu 64d8fd8
Rules produce at most one fix
ScottSWu 46f4e38
Add Fix.applyAll
ScottSWu 00f94e0
Removed createFix, added helper Replacement methods.
ScottSWu 07b8719
Add Replacement.applyAll and check-bin tests for fixes
ScottSWu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,32 +22,36 @@ import * as glob from "glob"; | |
import * as path from "path"; | ||
import * as ts from "typescript"; | ||
|
||
import {Fix} from "./language/rule/rule"; | ||
import {createCompilerOptions} from "./language/utils"; | ||
import {LintError} from "./test/lintError"; | ||
import * as parse from "./test/parse"; | ||
import * as Linter from "./tslint"; | ||
|
||
const FILE_EXTENSION = ".lint"; | ||
const MARKUP_FILE_EXTENSION = ".lint"; | ||
const FIXES_FILE_EXTENSION = ".fix"; | ||
|
||
export interface TestResult { | ||
directory: string; | ||
results: { | ||
[fileName: string]: { | ||
errorsFromMarkup: LintError[]; | ||
errorsFromLinter: LintError[]; | ||
errorsFromMarkup: LintError[]; | ||
fixesFromLinter: string; | ||
fixesFromMarkup: string; | ||
markupFromLinter: string; | ||
markupFromMarkup: string; | ||
} | ||
}; | ||
} | ||
|
||
export function runTest(testDirectory: string, rulesDirectory?: string | string[]): TestResult { | ||
const filesToLint = glob.sync(path.join(testDirectory, `**/*${FILE_EXTENSION}`)); | ||
const filesToLint = glob.sync(path.join(testDirectory, `**/*${MARKUP_FILE_EXTENSION}`)); | ||
const tslintConfig = Linter.findConfiguration(path.join(testDirectory, "tslint.json"), null); | ||
const results: TestResult = { directory: testDirectory, results: {} }; | ||
|
||
for (const fileToLint of filesToLint) { | ||
const fileBasename = path.basename(fileToLint, FILE_EXTENSION); | ||
const fileBasename = path.basename(fileToLint, MARKUP_FILE_EXTENSION); | ||
const fileCompileName = fileBasename.replace(/\.lint$/, ""); | ||
const fileText = fs.readFileSync(fileToLint, "utf8"); | ||
const fileTextWithoutMarkup = parse.removeErrorMarkup(fileText); | ||
|
@@ -87,7 +91,8 @@ export function runTest(testDirectory: string, rulesDirectory?: string | string[ | |
rulesDirectory, | ||
}; | ||
const linter = new Linter(fileBasename, fileTextWithoutMarkup, lintOptions, program); | ||
const errorsFromLinter: LintError[] = linter.lint().failures.map((failure) => { | ||
const failures = linter.lint().failures; | ||
const errorsFromLinter: LintError[] = failures.map((failure) => { | ||
const startLineAndCharacter = failure.getStartPosition().getLineAndCharacter(); | ||
const endLineAndCharacter = failure.getEndPosition().getLineAndCharacter(); | ||
|
||
|
@@ -104,9 +109,27 @@ export function runTest(testDirectory: string, rulesDirectory?: string | string[ | |
}; | ||
}); | ||
|
||
// test against fixed files | ||
let fixedFileText: string; | ||
let newFileText: string; | ||
try { | ||
const fixedFile = fileToLint.replace(/\.lint$/, FIXES_FILE_EXTENSION); | ||
const stat = fs.statSync(fixedFile); | ||
if (stat.isFile()) { | ||
fixedFileText = fs.readFileSync(fixedFile, "utf8"); | ||
const fixes = failures.filter(f => f.hasFix()).map(f => f.getFix()); | ||
newFileText = Fix.applyAll(fileTextWithoutMarkup, fixes); | ||
} | ||
} catch (e) { | ||
fixedFileText = ""; | ||
newFileText = ""; | ||
} | ||
|
||
results.results[fileToLint] = { | ||
errorsFromMarkup, | ||
errorsFromLinter, | ||
errorsFromMarkup, | ||
fixesFromLinter: newFileText, | ||
fixesFromMarkup: fixedFileText, | ||
markupFromLinter: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromMarkup), | ||
markupFromMarkup: parse.createMarkupFromErrors(fileTextWithoutMarkup, errorsFromLinter), | ||
}; | ||
|
@@ -122,31 +145,43 @@ export function consoleTestResultHandler(testResult: TestResult): boolean { | |
const results = testResult.results[fileName]; | ||
process.stdout.write(`${fileName}:`); | ||
|
||
const diffResults = diff.diffLines(results.markupFromMarkup, results.markupFromLinter); | ||
const didTestPass = !diffResults.some((diff) => diff.added || diff.removed); | ||
const markupDiffResults = diff.diffLines(results.markupFromMarkup, results.markupFromLinter); | ||
const fixesDiffResults = diff.diffLines(results.fixesFromMarkup, results.fixesFromLinter); | ||
const didMarkupTestPass = !markupDiffResults.some((diff) => diff.added || diff.removed); | ||
const didFixesTestPass = !fixesDiffResults.some((diff) => diff.added || diff.removed); | ||
|
||
/* tslint:disable:no-console */ | ||
if (didTestPass) { | ||
if (didMarkupTestPass && didFixesTestPass) { | ||
console.log(colors.green(" Passed")); | ||
} else { | ||
console.log(colors.red(" Failed!")); | ||
console.log(colors.green(`Expected (from ${FILE_EXTENSION} file)`)); | ||
console.log(colors.red("Actual (from TSLint)")); | ||
|
||
didAllTestsPass = false; | ||
|
||
for (const diffResult of diffResults) { | ||
let color = colors.grey; | ||
if (diffResult.added) { | ||
color = colors.green; | ||
} else if (diffResult.removed) { | ||
color = colors.red; | ||
} | ||
process.stdout.write(color(diffResult.value)); | ||
if (!didMarkupTestPass) { | ||
displayDiffResults(markupDiffResults, MARKUP_FILE_EXTENSION); | ||
} | ||
if (!didFixesTestPass) { | ||
displayDiffResults(fixesDiffResults, FIXES_FILE_EXTENSION); | ||
} | ||
} | ||
/* tslint:enable:no-console */ | ||
} | ||
|
||
return didAllTestsPass; | ||
} | ||
|
||
function displayDiffResults(diffResults: diff.IDiffResult[], extension: string) { | ||
/* tslint:disable:no-console */ | ||
console.log(colors.green(`Expected (from ${extension} file)`)); | ||
console.log(colors.red("Actual (from TSLint)")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we want any message here to differentiate that you're looking at fix or lint failure differences? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, since these outputs are just for TSLint tests, is the extension enough or should it be more explicit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry, missed the |
||
|
||
for (const diffResult of diffResults) { | ||
let color = colors.grey; | ||
if (diffResult.added) { | ||
color = colors.green; | ||
} else if (diffResult.removed) { | ||
color = colors.red; | ||
} | ||
process.stdout.write(color(diffResult.value)); | ||
} | ||
/* tslint:enable:no-console */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var testVariable = "eval"; | ||
|
||
function a() { | ||
function b() { | ||
function c() { | ||
console.log("hi"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var testVariable = "eval" | ||
~nil [Missing semicolon] | ||
|
||
function a() { | ||
function b() { | ||
function c() { | ||
console.log("hi") | ||
~nil [Missing semicolon] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"semicolon": [true, "always"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var testVariable = "eval"; | ||
|
||
function a() { | ||
function b() { | ||
function c() { | ||
console.log("hi") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var testVariable = "eval" | ||
~nil [Missing semicolon] | ||
|
||
function a() { | ||
function b() { | ||
function c() { | ||
console.log("hi") | ||
~nil [Missing semicolon] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"semicolon": [true, "always"] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the future, we could possible add some helper methods for typical fixes (
insertAt
,delete
, stuff like that).