Skip to content

Commit

Permalink
Merge pull request #27 from IsLand-x/main
Browse files Browse the repository at this point in the history
fix: Refresh untracked file list once a new file is created.
  • Loading branch information
paleite committed Aug 7, 2022
2 parents e17b3b0 + 4588ff9 commit 2f01120
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.tsbuildinfo
/coverage/
/dist/
node_modules
22 changes: 14 additions & 8 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,25 @@ const fetchFromOrigin = (branch: string) => {
child_process.execFileSync(COMMAND, args, OPTIONS);
};

const getUntrackedFileList = (staged = false): string[] => {
let untrackedFileListCache: string[] | undefined;
const getUntrackedFileList = (
staged = false,
shouldRefresh = false
): string[] => {
if (staged) {
return [];
}

const args = ["ls-files", "--exclude-standard", "--others"];
if (untrackedFileListCache === undefined || shouldRefresh) {
const args = ["ls-files", "--exclude-standard", "--others"];

const untrackedFileListCache = child_process
.execFileSync(COMMAND, args, OPTIONS)
.toString()
.trim()
.split("\n")
.map((filePath) => resolve(filePath));
untrackedFileListCache = child_process
.execFileSync(COMMAND, args, OPTIONS)
.toString()
.trim()
.split("\n")
.map((filePath) => resolve(filePath));
}

return untrackedFileListCache;
};
Expand Down
17 changes: 11 additions & 6 deletions src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ if (process.env.CI !== undefined) {
* This is increasingly useful the more files there are in the repository.
*/
const getPreProcessor =
(untrackedFileList: string[], diffFileList: string[]) =>
(diffFileList: string[], staged: boolean) =>
(text: string, filename: string) => {
let untrackedFileList = getUntrackedFileList(staged);
const shouldRefresh =
!diffFileList.includes(filename) && !untrackedFileList.includes(filename);
if (shouldRefresh) {
untrackedFileList = getUntrackedFileList(staged, true);
}
const shouldBeProcessed =
process.env.VSCODE_CLI !== undefined ||
diffFileList.includes(filename) ||
Expand Down Expand Up @@ -66,7 +72,7 @@ const getUnstagedChangesError = (filename: string): [Linter.LintMessage] => {
};

const getPostProcessor =
(untrackedFileList: string[], staged = false) =>
(staged = false) =>
(
messages: Linter.LintMessage[][],
filename: string
Expand All @@ -75,7 +81,7 @@ const getPostProcessor =
// No need to filter, just return
return [];
}

const untrackedFileList = getUntrackedFileList(staged);
if (untrackedFileList.includes(filename)) {
// We don't need to filter the messages of untracked files because they
// would all be kept anyway, so we return them as-is.
Expand Down Expand Up @@ -111,12 +117,11 @@ const getProcessors = (
processorType: ProcessorType
): Required<Linter.Processor> => {
const staged = processorType === "staged";
const untrackedFileList = getUntrackedFileList(staged);
const diffFileList = getDiffFileList(staged);

return {
preprocess: getPreProcessor(untrackedFileList, diffFileList),
postprocess: getPostProcessor(untrackedFileList, staged),
preprocess: getPreProcessor(diffFileList, staged),
postprocess: getPostProcessor(staged),
supportsAutofix: true,
};
};
Expand Down

0 comments on commit 2f01120

Please sign in to comment.