Skip to content

Commit d93e91b

Browse files
committed
Add line ending fixer
1 parent 648647f commit d93e91b

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎.editorconfig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
9+
[tsc/testdata/**]
10+
end_of_line = unset
11+
charset = unset
12+
trim_trailing_whitespace = unset
13+
insert_final_newline = unset
14+
15+
[tsc/internal/locale/lcl/**]
16+
end_of_line = unset
17+
charset = unset
18+
trim_trailing_whitespace = unset
19+
insert_final_newline = unset

‎Herebyfile.mjs‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,46 @@ export const format = task({
12691269

12701270
async function runFormat() {
12711271
await $`dprint fmt`;
1272+
await fixLineEndings();
1273+
}
1274+
1275+
const lineEndingExclusions = [
1276+
":(exclude)tsc/testdata/**",
1277+
// TODO: Remove this exclusion when this directory is converted or removed.
1278+
":(exclude)tsc/internal/locale/lcl/**",
1279+
];
1280+
1281+
export const fixLineEndingsTask = task({
1282+
name: "fix:line-endings",
1283+
description: "Converts tracked files that Git expects to use LF.",
1284+
run: fixLineEndings,
1285+
});
1286+
1287+
async function fixLineEndings() {
1288+
const result = await _$`git ls-files --eol -z -- . ${lineEndingExclusions}`;
1289+
const files = [];
1290+
for (const record of result.stdout.split("\0")) {
1291+
if (!record) {
1292+
continue;
1293+
}
1294+
const separator = record.indexOf("\t");
1295+
assert(separator !== -1);
1296+
const eolInfo = record.slice(0, separator);
1297+
if (!/\bw\/(?:crlf|mixed)\b/.test(eolInfo) || !eolInfo.includes("eol=lf")) {
1298+
continue;
1299+
}
1300+
files.push(record.slice(separator + 1));
1301+
}
1302+
1303+
await Promise.all(files.map(async file => {
1304+
const contents = await fs.promises.readFile(file);
1305+
const normalized = Buffer.from(contents.toString("latin1").replaceAll("\r\n", "\n"), "latin1");
1306+
await fs.promises.writeFile(file, normalized);
1307+
}));
1308+
1309+
if (files.length) {
1310+
console.log(`Converted ${files.length} file(s) to LF.`);
1311+
}
12721312
}
12731313

12741314
export const checkFormat = task({
@@ -1281,12 +1321,7 @@ export const checkFormat = task({
12811321
});
12821322

12831323
async function checkLineEndings() {
1284-
const exclusions = [
1285-
":(exclude)tsc/testdata/**",
1286-
// TODO: Remove this exclusion when this directory is converted or removed.
1287-
":(exclude)tsc/internal/locale/lcl/**",
1288-
];
1289-
const result = await _$({ reject: false })`git grep --cached -Il ${"\r"} -- . ${exclusions}`;
1324+
const result = await _$({ reject: false })`git grep --cached -Il ${"\r"} -- . ${lineEndingExclusions}`;
12901325
if (result.exitCode === 1) {
12911326
return;
12921327
}

0 commit comments

Comments
 (0)