-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-eof.ts
More file actions
67 lines (53 loc) · 1.58 KB
/
validate-eof.ts
File metadata and controls
67 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* End-of-file fixer — ensures files end with exactly one newline.
*
* @module
*/
import * as standards from "@eserstack/standards";
import { createFileTool, type FileTool, withGoValidator } from "./file-tool.ts";
export const tool: FileTool = withGoValidator(createFileTool({
name: "validate-eof",
description: "Ensure files end with exactly one newline",
canFix: true,
stacks: [],
defaults: {},
checkFile(file, content) {
if (content === undefined) {
return [];
}
if (content.length === 0) {
return [];
}
if (!content.endsWith("\n")) {
return [{ path: file.path, message: "file does not end with a newline" }];
}
if (content.endsWith("\n\n")) {
return [{
path: file.path,
message: "file has multiple trailing newlines",
}];
}
return [];
},
fixFile(file, content) {
if (content.length === 0) {
return undefined;
}
const trimmed = content.replace(/\n+$/, "");
const fixed = `${trimmed}\n`;
if (fixed === content) {
return undefined;
}
return { path: file.path, oldContent: content, newContent: fixed };
},
}), "eof");
export const run: FileTool["run"] = tool.run;
export const validator: FileTool["validator"] = tool.validator;
export const main: FileTool["main"] = tool.main;
if (import.meta.main) {
const { runCliMain } = await import("./cli-support.ts");
runCliMain(
await main(standards.crossRuntime.runtime.process.args as string[]),
);
}