-
Notifications
You must be signed in to change notification settings - Fork 101
[Experimental Feature] Opt in flag convert disable comments #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoshuaKGoldberg
merged 24 commits into
typescript-eslint:master
from
KingDarBoja:feature/opt-in-flag-convert-disable-comments
Apr 27, 2020
Merged
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
29baeee
Set basic replace comment conversion
e84bb8c
Fix regex pattern and cli command name for convert comments
dcfce0c
Fix regex pattern condition to match rules without carriage return
300ebd3
Replace regex and use tslint functions to retrieve tslint comment data
fae2869
Switching rule handler
5dbd345
Merge branch 'master' into feature/opt-in-flag-convert-disable-comments
8cb8c06
Write file after switching rule comments
67b4d66
Merge branch 'master' into feature/opt-in-flag-convert-disable-comments
KingDarBoja 2830122
Merge branch 'master' into feature/opt-in-flag-convert-disable-comments
KingDarBoja dd319f8
replace old path to rulesConverter
KingDarBoja f1bd492
Merge branch 'master' into feature/opt-in-flag-convert-disable-comments
JoshuaKGoldberg bacf9e8
Working replacements for single line changes
JoshuaKGoldberg 35adb28
4am and it works
JoshuaKGoldberg 63a3876
Revert .prettierrc testing change
JoshuaKGoldberg 0456314
Love that nullish operator
JoshuaKGoldberg e61a434
Comments WIP
JoshuaKGoldberg 5248447
Seems to be all working and tested now
JoshuaKGoldberg da1ace3
Merge branch 'master'
JoshuaKGoldberg 425280a
Feature add: logged directive counts as a summary
JoshuaKGoldberg 87e8953
Docs in CLI and README.md
JoshuaKGoldberg 3098453
Merge branch 'master'
JoshuaKGoldberg fa7fcda
Filled in basic test for separateErrors
JoshuaKGoldberg 2ec5b67
Normalized output per new norms
JoshuaKGoldberg bf6574f
Merge branch 'master' (and added --comments validation)
JoshuaKGoldberg 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import * as fs from "fs"; | ||
|
||
export type ReadDirOptions = { encoding?: string | null; withFileTypes: true }; | ||
|
||
export type FileSystem = { | ||
fileExists: (filePath: string) => Promise<boolean>; | ||
readFile: (filePath: string) => Promise<Error | string>; | ||
writeFile: (filePath: string, contents: string) => Promise<Error | undefined>; | ||
readDir: (dirPath: string, options: ReadDirOptions) => Promise<Error | fs.Dirent[]>; | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
import { FileSystem } from "../adapters/fileSystem"; | ||
import { isError } from "../utils"; | ||
import { getCommentAtPosition } from "tsutils"; | ||
import * as ts from "typescript"; | ||
|
||
// Check https://github.com/Microsoft/TypeScript/issues/21049 | ||
|
||
export type ConvertCommentsResultsDependencies = { | ||
fileSystem: Pick<FileSystem, "readDir" | "readFile" | "writeFile">; | ||
}; | ||
|
||
export const convertComments = async (dependencies: ConvertCommentsResultsDependencies) => { | ||
// TODO: Remove console logs | ||
console.log("Started"); | ||
const filenames = await dependencies.fileSystem.readDir("./", { withFileTypes: true }); | ||
if (!isError(filenames)) { | ||
const filteredFilenames: string[] = filenames | ||
.filter(fileEnt => fileEnt.isFile()) | ||
.map(fileEnt => fileEnt.name); | ||
// TODO: Remove console logs | ||
console.log("Filenames filtered"); | ||
console.log(filteredFilenames); | ||
for (const filename of filteredFilenames) { | ||
const fileContent: string | Error = await dependencies.fileSystem.readFile(filename); | ||
if (!isError(fileContent)) { | ||
const writeFileRes = await replaceComments(dependencies, filename, fileContent); | ||
if (isError(writeFileRes)) { | ||
return Error("Failed to convert file comments"); | ||
} | ||
} | ||
} | ||
return undefined; | ||
} else { | ||
return Error("Failed to convert file comments"); | ||
} | ||
}; | ||
|
||
const replaceComments = async ( | ||
dependencies: ConvertCommentsResultsDependencies, | ||
fileName: string, | ||
fileContent: string, | ||
) => { | ||
const tslintRegex: RegExp = new RegExp( | ||
/\/[\/\*]\s*(tslint):(enable|disable)((?:-next)?-line)?(?::\s)?(:?(?:(?:[\w-]+\/)*[\w-]+\s*\s*)*(?:(?:[\w-]+\/)*[\w-]+\s*)*[\w-]+)?\s*?(?:$|\*\/)/gm, | ||
); | ||
const sourceFile: ts.SourceFile = ts.createSourceFile( | ||
fileName, | ||
fileContent, | ||
ts.ScriptTarget.ES2015, | ||
/*setParentNodes */ true, | ||
); | ||
for ( | ||
let match = tslintRegex.exec(sourceFile.text); | ||
match !== null; | ||
match = tslintRegex.exec(sourceFile.text) | ||
) { | ||
// TODO: Remove console logs | ||
console.log("Match"); | ||
console.log(match); | ||
console.log(match[0].length); | ||
const comment: ts.CommentRange | undefined = getCommentAtPosition(sourceFile, match.index); | ||
console.log("comment"); | ||
console.log(comment); | ||
if ( | ||
comment === undefined || | ||
comment.pos !== match.index || | ||
comment.end !== match.index + match[0].length | ||
) { | ||
continue; | ||
} | ||
console.log("Passed Check"); | ||
} | ||
return await dependencies.fileSystem.writeFile(fileName, fileContent); | ||
}; |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.