@textlint/fixer-formatter 15.8.0 → 16.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/formatters/diff.d.ts +2 -1
- package/lib/src/formatters/diff.d.ts.map +1 -1
- package/lib/src/formatters/diff.js +6 -51
- package/lib/src/formatters/diff.js.map +1 -1
- package/lib/src/formatters/stylish.d.ts.map +1 -1
- package/lib/src/formatters/stylish.js +4 -18
- package/lib/src/formatters/stylish.js.map +1 -1
- package/lib/src/helper/align-columns.d.ts +13 -0
- package/lib/src/helper/align-columns.d.ts.map +1 -0
- package/lib/src/helper/align-columns.js +45 -0
- package/lib/src/helper/align-columns.js.map +1 -0
- package/lib/src/helper/string-width.d.ts +21 -0
- package/lib/src/helper/string-width.d.ts.map +1 -0
- package/lib/src/helper/string-width.js +97 -0
- package/lib/src/helper/string-width.js.map +1 -0
- package/lib/src/index.d.ts +19 -1
- package/lib/src/index.d.ts.map +1 -1
- package/lib/src/index.js +42 -13
- package/lib/src/index.js.map +1 -1
- package/package.json +9 -12
- package/src/formatters/diff.ts +8 -13
- package/src/formatters/stylish.ts +4 -18
- package/src/helper/align-columns.ts +41 -0
- package/src/helper/string-width.ts +97 -0
- package/src/index.ts +65 -14
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TextlintFixResult } from "@textlint/types";
|
|
2
|
-
export default function (results: TextlintFixResult[], options
|
|
2
|
+
export default function (results: TextlintFixResult[], options: {
|
|
3
3
|
color?: boolean;
|
|
4
|
+
getSource: (filePath: string) => string | undefined;
|
|
4
5
|
}): string;
|
|
5
6
|
//# sourceMappingURL=diff.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../../src/formatters/diff.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../../../src/formatters/diff.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAyCzD,MAAM,CAAC,OAAO,WACV,OAAO,EAAE,iBAAiB,EAAE,EAC5B,OAAO,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAA;CAAE,UAyGpF"}
|
|
@@ -1,55 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.default = default_1;
|
|
40
|
-
const fs = __importStar(require("node:fs"));
|
|
41
4
|
const diff_1 = require("diff");
|
|
42
5
|
const node_util_1 = require("node:util");
|
|
43
|
-
const
|
|
44
|
-
const isFile = (filePath) => {
|
|
45
|
-
try {
|
|
46
|
-
const stats = fs.statSync(filePath);
|
|
47
|
-
return stats.isFile();
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
6
|
+
const node_util_2 = require("node:util");
|
|
53
7
|
/**
|
|
54
8
|
* Given a word and a count, append an s if count is not one.
|
|
55
9
|
* @param {string} word A word in its singular form.
|
|
@@ -77,7 +31,7 @@ function addMarkEachLine(mark, text) {
|
|
|
77
31
|
});
|
|
78
32
|
return `${markedLines.join("\n")}\n`;
|
|
79
33
|
}
|
|
80
|
-
function default_1(results, options
|
|
34
|
+
function default_1(results, options) {
|
|
81
35
|
// default: true
|
|
82
36
|
const useColor = options.color !== undefined ? options.color : true;
|
|
83
37
|
let output = "\n";
|
|
@@ -95,11 +49,12 @@ function default_1(results, options = {}) {
|
|
|
95
49
|
if (messages.length === 0) {
|
|
96
50
|
return;
|
|
97
51
|
}
|
|
98
|
-
|
|
52
|
+
// The result does not have the content before fixing
|
|
53
|
+
const originalContent = options.getSource(filePath);
|
|
54
|
+
if (originalContent === undefined) {
|
|
99
55
|
return;
|
|
100
56
|
}
|
|
101
57
|
output += `${(0, node_util_1.styleText)("underline", result.filePath)}\n`;
|
|
102
|
-
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
103
58
|
const diff = (0, diff_1.diffLines)(originalContent, result.output);
|
|
104
59
|
diff.forEach(function (part, index) {
|
|
105
60
|
var _a;
|
|
@@ -168,7 +123,7 @@ function default_1(results, options = {}) {
|
|
|
168
123
|
}
|
|
169
124
|
const finalOutput = totalFixed > 0 ? output : "";
|
|
170
125
|
if (!useColor) {
|
|
171
|
-
return (0,
|
|
126
|
+
return (0, node_util_2.stripVTControlCharacters)(finalOutput);
|
|
172
127
|
}
|
|
173
128
|
return finalOutput;
|
|
174
129
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../../../src/formatters/diff.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC
|
|
1
|
+
{"version":3,"file":"diff.js","sourceRoot":"","sources":["../../../src/formatters/diff.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AA0Cb,4BA2GC;AAnJD,+BAAiC;AACjC,yCAAsC;AACtC,yCAAkE;AAQlE;;;;;GAKG;AACH,SAAS,SAAS,CAAC,IAAY,EAAE,KAAa;IAC1C,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;AAC3C,CAAC;AAED,SAAS,UAAU,CAAC,IAAc;IAC9B,IAAI,CAAC,IAAI,EAAE,CAAC;QACR,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACzF,CAAC;AAED,SAAS,eAAe,CAAC,IAAY,EAAE,IAAY;IAC/C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACpB,OAAO,IAAI,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AAS,EAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,WAAW,CAAC;AACvB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stylish.d.ts","sourceRoot":"","sources":["../../../src/formatters/stylish.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"stylish.d.ts","sourceRoot":"","sources":["../../../src/formatters/stylish.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAezD,MAAM,CAAC,OAAO,WAAW,OAAO,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,UAkGlF"}
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.default = default_1;
|
|
7
4
|
const node_util_1 = require("node:util");
|
|
8
|
-
|
|
9
|
-
const text_table_1 = __importDefault(require("text-table"));
|
|
10
|
-
const string_width_1 = __importDefault(require("string-width"));
|
|
11
|
-
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
5
|
+
const align_columns_js_1 = require("../helper/align-columns.js");
|
|
12
6
|
/**
|
|
13
7
|
* Given a word and a count, append an s if count is not one.
|
|
14
8
|
* @param {string} word A word in its singular form.
|
|
@@ -52,7 +46,7 @@ function default_1(results, options) {
|
|
|
52
46
|
return;
|
|
53
47
|
}
|
|
54
48
|
output += `${(0, node_util_1.styleText)("underline", result.filePath)}\n`;
|
|
55
|
-
output += `${(0,
|
|
49
|
+
output += `${(0, align_columns_js_1.alignColumns)(messages.map(function (message) {
|
|
56
50
|
// fixable
|
|
57
51
|
totalFixed++;
|
|
58
52
|
const messageType = (0, node_util_1.styleText)([greenColor, "bold"], "\u2714 ");
|
|
@@ -64,15 +58,7 @@ function default_1(results, options) {
|
|
|
64
58
|
message.message.replace(/\.$/, ""),
|
|
65
59
|
(0, node_util_1.styleText)("gray", message.ruleId || "")
|
|
66
60
|
];
|
|
67
|
-
}),
|
|
68
|
-
align: ["", "r", "l"],
|
|
69
|
-
stringLength: (str) => {
|
|
70
|
-
const lines = (0, strip_ansi_1.default)(str).split("\n");
|
|
71
|
-
return Math.max.apply(null, lines.map(function (line) {
|
|
72
|
-
return (0, string_width_1.default)(line);
|
|
73
|
-
}));
|
|
74
|
-
}
|
|
75
|
-
})
|
|
61
|
+
}), ["", "r", "l"])
|
|
76
62
|
.split("\n")
|
|
77
63
|
.map(function (el) {
|
|
78
64
|
return el.replace(/(\d+)\s+(\d+)/, function (_m, p1, p2) {
|
|
@@ -106,7 +92,7 @@ function default_1(results, options) {
|
|
|
106
92
|
}
|
|
107
93
|
const finalOutput = totalFixed > 0 ? output : "";
|
|
108
94
|
if (!useColor) {
|
|
109
|
-
return (0,
|
|
95
|
+
return (0, node_util_1.stripVTControlCharacters)(finalOutput);
|
|
110
96
|
}
|
|
111
97
|
return finalOutput;
|
|
112
98
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stylish.js","sourceRoot":"","sources":["../../../src/formatters/stylish.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC
|
|
1
|
+
{"version":3,"file":"stylish.js","sourceRoot":"","sources":["../../../src/formatters/stylish.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAgBb,4BAkGC;AA/GD,yCAA6E;AAC7E,iEAA0D;AAE1D;;;;;GAKG;AACH,SAAS,SAAS,CAAC,IAAY,EAAE,KAAa;IAC1C,OAAO,KAAK,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC;AAC3C,CAAC;AAED,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QAAQ,GAAG,KAAK,CAAC;IACjD,IAAI,cAAc,GAAG,CAAC,EAAE,CAAC;QACrB,MAAM,YAAY,GAAG;YACjB,GAAG,MAAM,IAAI,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE;YACzC,GAAG,QAAQ,IAAI,SAAS,CAAC,SAAS,EAAE,QAAQ,CAAC,EAAE;YAC/C,GAAG,KAAK,IAAI,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE;SACzC,CAAC;QACF,MAAM,IAAI,IAAA,qBAAS,EACf,CAAC,YAAY,EAAE,MAAM,CAAC,EACtB;YACI,8DAA8D;YAC9D,mBAAmB;YACnB,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;YACvB,IAAI;SACP,CAAC,IAAI,CAAC,EAAE,CAAC,CACb,CAAC;IACN,CAAC;IAED,MAAM,WAAW,GAAG,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;IACjD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACZ,OAAO,IAAA,oCAAS,EAAC,WAAW,CAAC,CAAC;IAClC,CAAC;IACD,OAAO,WAAW,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Align the columns of the rows with spaces, and join the columns with two spaces.
|
|
3
|
+
* The trailing whitespaces of each row are removed with `trimEnd`.
|
|
4
|
+
* A cell can have escape sequences and fullwidth characters.
|
|
5
|
+
*
|
|
6
|
+
* It is compatible with `text-table` package with the `stringLength` option that uses `stringWidth`.
|
|
7
|
+
* https://www.npmjs.com/package/text-table
|
|
8
|
+
*
|
|
9
|
+
* @param rows rows of the cells
|
|
10
|
+
* @param align "r" aligns the column to the right. Otherwise, the column is aligned to the left
|
|
11
|
+
*/
|
|
12
|
+
export declare const alignColumns: (rows: (string | number)[][], align?: string[]) => string;
|
|
13
|
+
//# sourceMappingURL=align-columns.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"align-columns.d.ts","sourceRoot":"","sources":["../../../src/helper/align-columns.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAAE,EAAE,QAAO,MAAM,EAAO,KAAG,MAmBhF,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.alignColumns = void 0;
|
|
4
|
+
const node_util_1 = require("node:util");
|
|
5
|
+
const string_width_js_1 = require("./string-width.js");
|
|
6
|
+
/**
|
|
7
|
+
* The width of the longest line of the text
|
|
8
|
+
*/
|
|
9
|
+
const maxLineWidth = (text) => {
|
|
10
|
+
return Math.max(...(0, node_util_1.stripVTControlCharacters)(text).split("\n").map(string_width_js_1.stringWidth));
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Align the columns of the rows with spaces, and join the columns with two spaces.
|
|
14
|
+
* The trailing whitespaces of each row are removed with `trimEnd`.
|
|
15
|
+
* A cell can have escape sequences and fullwidth characters.
|
|
16
|
+
*
|
|
17
|
+
* It is compatible with `text-table` package with the `stringLength` option that uses `stringWidth`.
|
|
18
|
+
* https://www.npmjs.com/package/text-table
|
|
19
|
+
*
|
|
20
|
+
* @param rows rows of the cells
|
|
21
|
+
* @param align "r" aligns the column to the right. Otherwise, the column is aligned to the left
|
|
22
|
+
*/
|
|
23
|
+
const alignColumns = (rows, align = []) => {
|
|
24
|
+
const textRows = rows.map((row) => row.map((cell) => String(cell)));
|
|
25
|
+
const columnWidths = [];
|
|
26
|
+
for (const row of textRows) {
|
|
27
|
+
row.forEach((cell, index) => {
|
|
28
|
+
var _a;
|
|
29
|
+
columnWidths[index] = Math.max((_a = columnWidths[index]) !== null && _a !== void 0 ? _a : 0, maxLineWidth(cell));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return textRows
|
|
33
|
+
.map((row) => {
|
|
34
|
+
return row
|
|
35
|
+
.map((cell, index) => {
|
|
36
|
+
const space = " ".repeat(Math.max(columnWidths[index] - maxLineWidth(cell), 0));
|
|
37
|
+
return align[index] === "r" ? space + cell : cell + space;
|
|
38
|
+
})
|
|
39
|
+
.join(" ")
|
|
40
|
+
.trimEnd();
|
|
41
|
+
})
|
|
42
|
+
.join("\n");
|
|
43
|
+
};
|
|
44
|
+
exports.alignColumns = alignColumns;
|
|
45
|
+
//# sourceMappingURL=align-columns.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"align-columns.js","sourceRoot":"","sources":["../../../src/helper/align-columns.ts"],"names":[],"mappings":";;;AAAA,yCAAqD;AACrD,uDAAgD;AAEhD;;GAEG;AACH,MAAM,YAAY,GAAG,CAAC,IAAY,EAAU,EAAE;IAC1C,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,IAAA,oCAAwB,EAAC,IAAI,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,6BAAW,CAAC,CAAC,CAAC;AACpF,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACI,MAAM,YAAY,GAAG,CAAC,IAA2B,EAAE,QAAkB,EAAE,EAAU,EAAE;IACtF,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpE,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QACzB,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;;YACxB,YAAY,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,MAAA,YAAY,CAAC,KAAK,CAAC,mCAAI,CAAC,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC;QACjF,CAAC,CAAC,CAAC;IACP,CAAC;IACD,OAAO,QAAQ;SACV,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACT,OAAO,GAAG;aACL,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;YACjB,MAAM,KAAK,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;YAChF,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC;QAC9D,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC;aACV,OAAO,EAAE,CAAC;IACnB,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC,CAAC;AAnBW,QAAA,YAAY,gBAmBvB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A text that has an emoji matches this pattern. It is a cheap check before creating the emoji pattern.
|
|
3
|
+
* Each emoji has one of them:
|
|
4
|
+
*
|
|
5
|
+
* - U+203C..U+2B55, U+3030, U+303D, U+3297, U+3299: the emoji of the Basic Multilingual Plane, and U+20E3 of a keycap
|
|
6
|
+
* - U+FE0F: the variation selector of an emoji that is a text character by default, such as "\u00A9\uFE0F"
|
|
7
|
+
* - U+D83C..U+D83E: the high surrogate of U+1F000..U+1FFFF
|
|
8
|
+
*/
|
|
9
|
+
export declare const MAY_HAVE_EMOJI_PATTERN: RegExp;
|
|
10
|
+
/**
|
|
11
|
+
* Get the number of the columns that the string uses in a terminal.
|
|
12
|
+
* It is based on https://github.com/sindresorhus/string-width v4
|
|
13
|
+
*
|
|
14
|
+
* Node.js has the same function internally, but it is not a public API yet.
|
|
15
|
+
* Replace this file with the function of Node.js when it is available in the supported versions of Node.js.
|
|
16
|
+
*
|
|
17
|
+
* - An escape sequence, a control character and a combining character use no column
|
|
18
|
+
* - A fullwidth character and an emoji use two columns
|
|
19
|
+
*/
|
|
20
|
+
export declare const stringWidth: (text: string) => number;
|
|
21
|
+
//# sourceMappingURL=string-width.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string-width.d.ts","sourceRoot":"","sources":["../../../src/helper/string-width.ts"],"names":[],"mappings":"AAiBA;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,QAAgE,CAAC;AAuCpG;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,KAAG,MAsB1C,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.stringWidth = exports.MAY_HAVE_EMOJI_PATTERN = void 0;
|
|
4
|
+
const node_util_1 = require("node:util");
|
|
5
|
+
/**
|
|
6
|
+
* `\p{RGI_Emoji}` is a large set of strings, and V8 takes about 15-20ms to parse the pattern.
|
|
7
|
+
* So the pattern is created when a text that may have an emoji comes first. Most outputs never create it.
|
|
8
|
+
*
|
|
9
|
+
* Do not write it as a regular expression literal, even in a function:
|
|
10
|
+
* V8 parses a literal when it parses the file, so only loading this file takes the time.
|
|
11
|
+
*/
|
|
12
|
+
const getEmojiPattern = (() => {
|
|
13
|
+
// Only this function refers to the created pattern
|
|
14
|
+
let emojiPattern;
|
|
15
|
+
return () => {
|
|
16
|
+
emojiPattern !== null && emojiPattern !== void 0 ? emojiPattern : (emojiPattern = new RegExp("\\p{RGI_Emoji}", "gv"));
|
|
17
|
+
return emojiPattern;
|
|
18
|
+
};
|
|
19
|
+
})();
|
|
20
|
+
/**
|
|
21
|
+
* A text that has an emoji matches this pattern. It is a cheap check before creating the emoji pattern.
|
|
22
|
+
* Each emoji has one of them:
|
|
23
|
+
*
|
|
24
|
+
* - U+203C..U+2B55, U+3030, U+303D, U+3297, U+3299: the emoji of the Basic Multilingual Plane, and U+20E3 of a keycap
|
|
25
|
+
* - U+FE0F: the variation selector of an emoji that is a text character by default, such as "\u00A9\uFE0F"
|
|
26
|
+
* - U+D83C..U+D83E: the high surrogate of U+1F000..U+1FFFF
|
|
27
|
+
*/
|
|
28
|
+
exports.MAY_HAVE_EMOJI_PATTERN = /[\u203c-\u2b55\u3030\u303d\u3297\u3299\ud83c-\ud83e]|\ufe0f/;
|
|
29
|
+
/**
|
|
30
|
+
* Code points are derived from https://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
|
|
31
|
+
*/
|
|
32
|
+
const isFullwidthCodePoint = (codePoint) => {
|
|
33
|
+
return (codePoint >= 0x1100 &&
|
|
34
|
+
(codePoint <= 0x115f || // Hangul Jamo
|
|
35
|
+
codePoint === 0x2329 || // LEFT-POINTING ANGLE BRACKET
|
|
36
|
+
codePoint === 0x232a || // RIGHT-POINTING ANGLE BRACKET
|
|
37
|
+
// CJK Radicals Supplement .. Enclosed CJK Letters and Months
|
|
38
|
+
(0x2e80 <= codePoint && codePoint <= 0x3247 && codePoint !== 0x303f) ||
|
|
39
|
+
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
|
|
40
|
+
(0x3250 <= codePoint && codePoint <= 0x4dbf) ||
|
|
41
|
+
// CJK Unified Ideographs .. Yi Radicals
|
|
42
|
+
(0x4e00 <= codePoint && codePoint <= 0xa4c6) ||
|
|
43
|
+
// Hangul Jamo Extended-A
|
|
44
|
+
(0xa960 <= codePoint && codePoint <= 0xa97c) ||
|
|
45
|
+
// Hangul Syllables
|
|
46
|
+
(0xac00 <= codePoint && codePoint <= 0xd7a3) ||
|
|
47
|
+
// CJK Compatibility Ideographs
|
|
48
|
+
(0xf900 <= codePoint && codePoint <= 0xfaff) ||
|
|
49
|
+
// Vertical Forms
|
|
50
|
+
(0xfe10 <= codePoint && codePoint <= 0xfe19) ||
|
|
51
|
+
// CJK Compatibility Forms .. Small Form Variants
|
|
52
|
+
(0xfe30 <= codePoint && codePoint <= 0xfe6b) ||
|
|
53
|
+
// Halfwidth and Fullwidth Forms
|
|
54
|
+
(0xff01 <= codePoint && codePoint <= 0xff60) ||
|
|
55
|
+
(0xffe0 <= codePoint && codePoint <= 0xffe6) ||
|
|
56
|
+
// Kana Supplement
|
|
57
|
+
(0x1b000 <= codePoint && codePoint <= 0x1b001) ||
|
|
58
|
+
// Enclosed Ideographic Supplement
|
|
59
|
+
(0x1f200 <= codePoint && codePoint <= 0x1f251) ||
|
|
60
|
+
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
|
|
61
|
+
(0x20000 <= codePoint && codePoint <= 0x3fffd)));
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Get the number of the columns that the string uses in a terminal.
|
|
65
|
+
* It is based on https://github.com/sindresorhus/string-width v4
|
|
66
|
+
*
|
|
67
|
+
* Node.js has the same function internally, but it is not a public API yet.
|
|
68
|
+
* Replace this file with the function of Node.js when it is available in the supported versions of Node.js.
|
|
69
|
+
*
|
|
70
|
+
* - An escape sequence, a control character and a combining character use no column
|
|
71
|
+
* - A fullwidth character and an emoji use two columns
|
|
72
|
+
*/
|
|
73
|
+
const stringWidth = (text) => {
|
|
74
|
+
if (text.length === 0) {
|
|
75
|
+
return 0;
|
|
76
|
+
}
|
|
77
|
+
const strippedText = (0, node_util_1.stripVTControlCharacters)(text);
|
|
78
|
+
const plainText = exports.MAY_HAVE_EMOJI_PATTERN.test(strippedText)
|
|
79
|
+
? strippedText.replace(getEmojiPattern(), " ")
|
|
80
|
+
: strippedText;
|
|
81
|
+
let width = 0;
|
|
82
|
+
for (const character of plainText) {
|
|
83
|
+
const codePoint = character.codePointAt(0);
|
|
84
|
+
// Ignore control characters
|
|
85
|
+
if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Ignore combining characters
|
|
89
|
+
if (codePoint >= 0x300 && codePoint <= 0x36f) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
width += isFullwidthCodePoint(codePoint) ? 2 : 1;
|
|
93
|
+
}
|
|
94
|
+
return width;
|
|
95
|
+
};
|
|
96
|
+
exports.stringWidth = stringWidth;
|
|
97
|
+
//# sourceMappingURL=string-width.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string-width.js","sourceRoot":"","sources":["../../../src/helper/string-width.ts"],"names":[],"mappings":";;;AAAA,yCAAqD;AAErD;;;;;;GAMG;AACH,MAAM,eAAe,GAAG,CAAC,GAAG,EAAE;IAC1B,mDAAmD;IACnD,IAAI,YAAgC,CAAC;IACrC,OAAO,GAAW,EAAE;QAChB,YAAY,aAAZ,YAAY,cAAZ,YAAY,IAAZ,YAAY,GAAK,IAAI,MAAM,CAAC,gBAAgB,EAAE,IAAI,CAAC,EAAC;QACpD,OAAO,YAAY,CAAC;IACxB,CAAC,CAAC;AACN,CAAC,CAAC,EAAE,CAAC;AACL;;;;;;;GAOG;AACU,QAAA,sBAAsB,GAAG,6DAA6D,CAAC;AAEpG;;GAEG;AACH,MAAM,oBAAoB,GAAG,CAAC,SAAiB,EAAW,EAAE;IACxD,OAAO,CACH,SAAS,IAAI,MAAM;QACnB,CAAC,SAAS,IAAI,MAAM,IAAI,cAAc;YAClC,SAAS,KAAK,MAAM,IAAI,8BAA8B;YACtD,SAAS,KAAK,MAAM,IAAI,+BAA+B;YACvD,6DAA6D;YAC7D,CAAC,MAAM,IAAI,SAAS,IAAI,SAAS,IAAI,MAAM,IAAI,SAAS,KAAK,MAAM,CAAC;YACpE,wEAAwE;YACxE,CAAC,MAAM,IAAI,SAAS,IAAI,SAAS,IAAI,MAAM,CAAC;YAC5C,wCAAwC;YACxC,CAAC,MAAM,IAAI,SAAS,IAAI,SAAS,IAAI,MAAM,CAAC;YAC5C,yBAAyB;YACzB,CAAC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SAAS,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC,CAAE,CAAC;QAC5C,4BAA4B;QAC5B,IAAI,SAAS,IAAI,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,IAAI,SAAS,IAAI,IAAI,CAAC,EAAE,CAAC;YAChE,SAAS;QACb,CAAC;QACD,8BAA8B;QAC9B,IAAI,SAAS,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;YAC3C,SAAS;QACb,CAAC;QACD,KAAK,IAAI,oBAAoB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC,CAAC;AAtBW,QAAA,WAAW,eAsBtB"}
|
package/lib/src/index.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import type { TextlintFixResult } from "@textlint/types";
|
|
2
|
+
/**
|
|
3
|
+
* Get the content of the source before fixing by `filePath` of the result.
|
|
4
|
+
* It returns `undefined` if the caller does not have the content.
|
|
5
|
+
*/
|
|
6
|
+
export type FormatterGetSource = (filePath: string) => string | undefined;
|
|
2
7
|
export type FormatterConfig = {
|
|
3
8
|
color?: boolean;
|
|
4
9
|
formatterName: string;
|
|
10
|
+
/**
|
|
11
|
+
* Get the content of the source before fixing by `filePath` of the result.
|
|
12
|
+
* For example, "diff" formatter uses it.
|
|
13
|
+
* If it is not given or returns `undefined`, the formatter gets the content of the file of `filePath`.
|
|
14
|
+
*/
|
|
15
|
+
getSource?: FormatterGetSource;
|
|
5
16
|
};
|
|
6
|
-
|
|
17
|
+
/**
|
|
18
|
+
* The options that the formatter receives.
|
|
19
|
+
* `loadFormatter()` always passes `getSource`.
|
|
20
|
+
*/
|
|
21
|
+
export type FormatterOptions = FormatterConfig & {
|
|
22
|
+
getSource: FormatterGetSource;
|
|
23
|
+
};
|
|
24
|
+
export declare function loadFormatter(config: FormatterConfig): Promise<{
|
|
7
25
|
format(results: TextlintFixResult[]): string;
|
|
8
26
|
}>;
|
|
9
27
|
export interface FixerFormatterDetail {
|
package/lib/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAqCzD;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAC1E,MAAM,MAAM,eAAe,GAAG;IAC1B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,SAAS,CAAC,EAAE,kBAAkB,CAAC;CAClC,CAAC;AACF;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,eAAe,GAAG;IAC7C,SAAS,EAAE,kBAAkB,CAAC;CACjC,CAAC;AAqBF,wBAAsB,aAAa,CAAC,MAAM,EAAE,eAAe;oBAO/B,iBAAiB,EAAE;GAiD9C;AAED,MAAM,WAAW,oBAAoB;IACjC,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,qBAAqB,IAAI,oBAAoB,EAAE,CAM9D"}
|
package/lib/src/index.js
CHANGED
|
@@ -11,32 +11,61 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
11
11
|
const node_path_1 = __importDefault(require("node:path"));
|
|
12
12
|
const debug_1 = __importDefault(require("debug"));
|
|
13
13
|
const resolver_1 = require("@textlint/resolver");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const json_js_1 = __importDefault(require("./formatters/json.js"));
|
|
19
|
-
const stylish_js_1 = __importDefault(require("./formatters/stylish.js"));
|
|
14
|
+
/**
|
|
15
|
+
* Each formatter is loaded when it is used.
|
|
16
|
+
* For example, `diff` formatter loads `diff`, but `stylish` formatter does not.
|
|
17
|
+
*/
|
|
20
18
|
const builtinFormatterList = {
|
|
21
|
-
compats:
|
|
22
|
-
diff:
|
|
23
|
-
"fixed-result":
|
|
24
|
-
json:
|
|
25
|
-
stylish:
|
|
19
|
+
compats: () => import("./formatters/compats.js"),
|
|
20
|
+
diff: () => import("./formatters/diff.js"),
|
|
21
|
+
"fixed-result": () => import("./formatters/fixed-result.js"),
|
|
22
|
+
json: () => import("./formatters/json.js"),
|
|
23
|
+
stylish: () => import("./formatters/stylish.js")
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Load the formatter function of a built-in formatter.
|
|
27
|
+
* A formatter file is a CommonJS module, so `import()` returns `{ default: module.exports }` in Node.js.
|
|
28
|
+
* `moduleInterop` gets the default export from `module.exports`.
|
|
29
|
+
*/
|
|
30
|
+
const loadBuiltinFormatter = async (name) => {
|
|
31
|
+
const mod = await builtinFormatterList[name]();
|
|
32
|
+
return (0, module_interop_1.moduleInterop)(mod.default);
|
|
26
33
|
};
|
|
27
34
|
const isFormatterFunction = (formatter) => {
|
|
28
35
|
return typeof formatter === "function";
|
|
29
36
|
};
|
|
30
37
|
const builtinFormatterNames = Object.keys(builtinFormatterList);
|
|
31
38
|
const debug = (0, debug_1.default)("textlint:textfix-formatter");
|
|
32
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Create `getSource` for the formatter.
|
|
41
|
+
* It returns the content from `getSource` of the caller, and reads the file of `filePath` if the caller does not have the content.
|
|
42
|
+
* It returns `undefined` if the file can not be read.
|
|
43
|
+
* A formatter gets the content only from this function, and does not read the file by itself.
|
|
44
|
+
*/
|
|
45
|
+
const createGetSource = (getSource) => {
|
|
46
|
+
return (filePath) => {
|
|
47
|
+
const source = getSource === null || getSource === void 0 ? void 0 : getSource(filePath);
|
|
48
|
+
if (source !== undefined) {
|
|
49
|
+
return source;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return node_fs_1.default.readFileSync(filePath, "utf-8");
|
|
53
|
+
}
|
|
54
|
+
catch (_a) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
async function loadFormatter(config) {
|
|
33
60
|
var _a;
|
|
61
|
+
const formatterConfig = { ...config, getSource: createGetSource(config.getSource) };
|
|
34
62
|
const formatterName = formatterConfig.formatterName;
|
|
35
63
|
debug(`formatterName: ${formatterName}`);
|
|
36
64
|
if (builtinFormatterNames.includes(formatterName)) {
|
|
65
|
+
const builtinFormatter = await loadBuiltinFormatter(formatterName);
|
|
37
66
|
return {
|
|
38
67
|
format(results) {
|
|
39
|
-
return
|
|
68
|
+
return builtinFormatter(results, formatterConfig);
|
|
40
69
|
}
|
|
41
70
|
};
|
|
42
71
|
}
|
package/lib/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,YAAY,CAAC;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,YAAY,CAAC;;;;;AAgFb,sCAwDC;AAMD,sDAMC;AAlJD,6DAAyD;AACzD,sDAAyB;AACzB,0DAA6B;AAC7B,kDAA2B;AAC3B,iDAA+D;AAI/D;;;GAGG;AACH,MAAM,oBAAoB,GAAG;IACzB,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,yBAAyB,CAAC;IAChD,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC;IAC1C,cAAc,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,8BAA8B,CAAC;IAC5D,IAAI,EAAE,GAAG,EAAE,CAAC,MAAM,CA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CACZ,0CAAE,OAAO,CAAC;QACX,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,yCAAyC,aAAa,YAAY,OAAO,GAAG,EAAE,CAAC,CAAC;QACpG,CAAC;QACD,SAAS,GAAG,GAAG,CAAC;IACpB,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,4BAA4B,aAAa;;EAE/D,EAAE,EAAE,CAAC,CAAC;IACJ,CAAC;IACD,KAAK,CAAC,kBAAkB,aAAa,EAAE,CAAC,CAAC;IACzC,OAAO;QACH,MAAM,CAAC,OAA4B;YAC/B,OAAO,SAAS,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QAC/C,CAAC;KACJ,CAAC;AACN,CAAC;AAMD,SAAgB,qBAAqB;IACjC,OAAO,qBAAqB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QACtC,OAAO;YACH,IAAI;SACP,CAAC;IACN,CAAC,CAAC,CAAC;AACP,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@textlint/fixer-formatter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "16.1.0",
|
|
4
4
|
"description": "textlint output formatter for fixer",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"AST",
|
|
@@ -39,19 +39,16 @@
|
|
|
39
39
|
"test:watch": "vitest watch"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@textlint/module-interop": "
|
|
43
|
-
"@textlint/resolver": "
|
|
44
|
-
"@textlint/types": "
|
|
42
|
+
"@textlint/module-interop": "16.1.0",
|
|
43
|
+
"@textlint/resolver": "16.1.0",
|
|
44
|
+
"@textlint/types": "16.1.0",
|
|
45
45
|
"debug": "^4.4.3",
|
|
46
|
-
"diff": "^8.0.4"
|
|
47
|
-
"string-width": "^4.2.3",
|
|
48
|
-
"strip-ansi": "^6.0.1",
|
|
49
|
-
"text-table": "^0.2.0"
|
|
46
|
+
"diff": "^8.0.4"
|
|
50
47
|
},
|
|
51
48
|
"devDependencies": {
|
|
52
49
|
"@types/debug": "^4.1.13",
|
|
53
|
-
"@types/node": "^24.
|
|
54
|
-
"rimraf": "^6.1.
|
|
50
|
+
"@types/node": "^24.19.2",
|
|
51
|
+
"rimraf": "^6.1.4",
|
|
55
52
|
"typescript": "~5.8.3",
|
|
56
53
|
"vitest": "^3.2.7"
|
|
57
54
|
},
|
|
@@ -59,7 +56,7 @@
|
|
|
59
56
|
"access": "public"
|
|
60
57
|
},
|
|
61
58
|
"engines": {
|
|
62
|
-
"node": ">=
|
|
59
|
+
"node": ">=22.12.0"
|
|
63
60
|
},
|
|
64
|
-
"gitHead": "
|
|
61
|
+
"gitHead": "3e5c98fa65201ad4650e935166cb63a1ccc8cf5a"
|
|
65
62
|
}
|
package/src/formatters/diff.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import type { TextlintFixResult } from "@textlint/types";
|
|
3
|
-
import * as fs from "node:fs";
|
|
4
3
|
import { diffLines } from "diff";
|
|
5
4
|
import { styleText } from "node:util";
|
|
6
|
-
import stripAnsi from "
|
|
5
|
+
import { stripVTControlCharacters as stripAnsi } from "node:util";
|
|
7
6
|
|
|
8
7
|
type DiffPart = {
|
|
9
8
|
value: string;
|
|
@@ -11,14 +10,6 @@ type DiffPart = {
|
|
|
11
10
|
added?: boolean;
|
|
12
11
|
removed?: boolean;
|
|
13
12
|
};
|
|
14
|
-
const isFile = (filePath: string) => {
|
|
15
|
-
try {
|
|
16
|
-
const stats = fs.statSync(filePath);
|
|
17
|
-
return stats.isFile();
|
|
18
|
-
} catch (error) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
13
|
/**
|
|
23
14
|
* Given a word and a count, append an s if count is not one.
|
|
24
15
|
* @param {string} word A word in its singular form.
|
|
@@ -49,7 +40,10 @@ function addMarkEachLine(mark: string, text: string) {
|
|
|
49
40
|
return `${markedLines.join("\n")}\n`;
|
|
50
41
|
}
|
|
51
42
|
|
|
52
|
-
export default function (
|
|
43
|
+
export default function (
|
|
44
|
+
results: TextlintFixResult[],
|
|
45
|
+
options: { color?: boolean; getSource: (filePath: string) => string | undefined }
|
|
46
|
+
) {
|
|
53
47
|
// default: true
|
|
54
48
|
const useColor = options.color !== undefined ? options.color : true;
|
|
55
49
|
let output = "\n";
|
|
@@ -68,12 +62,13 @@ export default function (results: TextlintFixResult[], options: { color?: boolea
|
|
|
68
62
|
if (messages.length === 0) {
|
|
69
63
|
return;
|
|
70
64
|
}
|
|
71
|
-
|
|
65
|
+
// The result does not have the content before fixing
|
|
66
|
+
const originalContent = options.getSource(filePath);
|
|
67
|
+
if (originalContent === undefined) {
|
|
72
68
|
return;
|
|
73
69
|
}
|
|
74
70
|
output += `${styleText("underline", result.filePath)}\n`;
|
|
75
71
|
|
|
76
|
-
const originalContent = fs.readFileSync(filePath, "utf-8");
|
|
77
72
|
const diff = diffLines(originalContent, result.output);
|
|
78
73
|
|
|
79
74
|
diff.forEach(function (part: DiffPart, index: number) {
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import type { TextlintFixResult } from "@textlint/types";
|
|
3
3
|
|
|
4
|
-
import { styleText } from "node:util";
|
|
5
|
-
|
|
6
|
-
import table from "text-table";
|
|
7
|
-
import widthOfString from "string-width";
|
|
8
|
-
import stripAnsi from "strip-ansi";
|
|
4
|
+
import { styleText, stripVTControlCharacters as stripAnsi } from "node:util";
|
|
5
|
+
import { alignColumns } from "../helper/align-columns.js";
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
8
|
* Given a word and a count, append an s if count is not one.
|
|
@@ -53,7 +50,7 @@ export default function (results: TextlintFixResult[], options: { color?: boolea
|
|
|
53
50
|
}
|
|
54
51
|
output += `${styleText("underline", result.filePath)}\n`;
|
|
55
52
|
|
|
56
|
-
output += `${
|
|
53
|
+
output += `${alignColumns(
|
|
57
54
|
messages.map(function (message) {
|
|
58
55
|
// fixable
|
|
59
56
|
totalFixed++;
|
|
@@ -68,18 +65,7 @@ export default function (results: TextlintFixResult[], options: { color?: boolea
|
|
|
68
65
|
styleText("gray", message.ruleId || "")
|
|
69
66
|
];
|
|
70
67
|
}),
|
|
71
|
-
|
|
72
|
-
align: ["", "r", "l"],
|
|
73
|
-
stringLength: (str: string) => {
|
|
74
|
-
const lines = stripAnsi(str).split("\n");
|
|
75
|
-
return Math.max.apply(
|
|
76
|
-
null,
|
|
77
|
-
lines.map(function (line: string) {
|
|
78
|
-
return widthOfString(line);
|
|
79
|
-
})
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
68
|
+
["", "r", "l"]
|
|
83
69
|
)
|
|
84
70
|
.split("\n")
|
|
85
71
|
.map(function (el: string) {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
2
|
+
import { stringWidth } from "./string-width.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The width of the longest line of the text
|
|
6
|
+
*/
|
|
7
|
+
const maxLineWidth = (text: string): number => {
|
|
8
|
+
return Math.max(...stripVTControlCharacters(text).split("\n").map(stringWidth));
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Align the columns of the rows with spaces, and join the columns with two spaces.
|
|
13
|
+
* The trailing whitespaces of each row are removed with `trimEnd`.
|
|
14
|
+
* A cell can have escape sequences and fullwidth characters.
|
|
15
|
+
*
|
|
16
|
+
* It is compatible with `text-table` package with the `stringLength` option that uses `stringWidth`.
|
|
17
|
+
* https://www.npmjs.com/package/text-table
|
|
18
|
+
*
|
|
19
|
+
* @param rows rows of the cells
|
|
20
|
+
* @param align "r" aligns the column to the right. Otherwise, the column is aligned to the left
|
|
21
|
+
*/
|
|
22
|
+
export const alignColumns = (rows: (string | number)[][], align: string[] = []): string => {
|
|
23
|
+
const textRows = rows.map((row) => row.map((cell) => String(cell)));
|
|
24
|
+
const columnWidths: number[] = [];
|
|
25
|
+
for (const row of textRows) {
|
|
26
|
+
row.forEach((cell, index) => {
|
|
27
|
+
columnWidths[index] = Math.max(columnWidths[index] ?? 0, maxLineWidth(cell));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return textRows
|
|
31
|
+
.map((row) => {
|
|
32
|
+
return row
|
|
33
|
+
.map((cell, index) => {
|
|
34
|
+
const space = " ".repeat(Math.max(columnWidths[index] - maxLineWidth(cell), 0));
|
|
35
|
+
return align[index] === "r" ? space + cell : cell + space;
|
|
36
|
+
})
|
|
37
|
+
.join(" ")
|
|
38
|
+
.trimEnd();
|
|
39
|
+
})
|
|
40
|
+
.join("\n");
|
|
41
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from "node:util";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `\p{RGI_Emoji}` is a large set of strings, and V8 takes about 15-20ms to parse the pattern.
|
|
5
|
+
* So the pattern is created when a text that may have an emoji comes first. Most outputs never create it.
|
|
6
|
+
*
|
|
7
|
+
* Do not write it as a regular expression literal, even in a function:
|
|
8
|
+
* V8 parses a literal when it parses the file, so only loading this file takes the time.
|
|
9
|
+
*/
|
|
10
|
+
const getEmojiPattern = (() => {
|
|
11
|
+
// Only this function refers to the created pattern
|
|
12
|
+
let emojiPattern: RegExp | undefined;
|
|
13
|
+
return (): RegExp => {
|
|
14
|
+
emojiPattern ??= new RegExp("\\p{RGI_Emoji}", "gv");
|
|
15
|
+
return emojiPattern;
|
|
16
|
+
};
|
|
17
|
+
})();
|
|
18
|
+
/**
|
|
19
|
+
* A text that has an emoji matches this pattern. It is a cheap check before creating the emoji pattern.
|
|
20
|
+
* Each emoji has one of them:
|
|
21
|
+
*
|
|
22
|
+
* - U+203C..U+2B55, U+3030, U+303D, U+3297, U+3299: the emoji of the Basic Multilingual Plane, and U+20E3 of a keycap
|
|
23
|
+
* - U+FE0F: the variation selector of an emoji that is a text character by default, such as "\u00A9\uFE0F"
|
|
24
|
+
* - U+D83C..U+D83E: the high surrogate of U+1F000..U+1FFFF
|
|
25
|
+
*/
|
|
26
|
+
export const MAY_HAVE_EMOJI_PATTERN = /[\u203c-\u2b55\u3030\u303d\u3297\u3299\ud83c-\ud83e]|\ufe0f/;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Code points are derived from https://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
|
|
30
|
+
*/
|
|
31
|
+
const isFullwidthCodePoint = (codePoint: number): boolean => {
|
|
32
|
+
return (
|
|
33
|
+
codePoint >= 0x1100 &&
|
|
34
|
+
(codePoint <= 0x115f || // Hangul Jamo
|
|
35
|
+
codePoint === 0x2329 || // LEFT-POINTING ANGLE BRACKET
|
|
36
|
+
codePoint === 0x232a || // RIGHT-POINTING ANGLE BRACKET
|
|
37
|
+
// CJK Radicals Supplement .. Enclosed CJK Letters and Months
|
|
38
|
+
(0x2e80 <= codePoint && codePoint <= 0x3247 && codePoint !== 0x303f) ||
|
|
39
|
+
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
|
|
40
|
+
(0x3250 <= codePoint && codePoint <= 0x4dbf) ||
|
|
41
|
+
// CJK Unified Ideographs .. Yi Radicals
|
|
42
|
+
(0x4e00 <= codePoint && codePoint <= 0xa4c6) ||
|
|
43
|
+
// Hangul Jamo Extended-A
|
|
44
|
+
(0xa960 <= codePoint && codePoint <= 0xa97c) ||
|
|
45
|
+
// Hangul Syllables
|
|
46
|
+
(0xac00 <= codePoint && codePoint <= 0xd7a3) ||
|
|
47
|
+
// CJK Compatibility Ideographs
|
|
48
|
+
(0xf900 <= codePoint && codePoint <= 0xfaff) ||
|
|
49
|
+
// Vertical Forms
|
|
50
|
+
(0xfe10 <= codePoint && codePoint <= 0xfe19) ||
|
|
51
|
+
// CJK Compatibility Forms .. Small Form Variants
|
|
52
|
+
(0xfe30 <= codePoint && codePoint <= 0xfe6b) ||
|
|
53
|
+
// Halfwidth and Fullwidth Forms
|
|
54
|
+
(0xff01 <= codePoint && codePoint <= 0xff60) ||
|
|
55
|
+
(0xffe0 <= codePoint && codePoint <= 0xffe6) ||
|
|
56
|
+
// Kana Supplement
|
|
57
|
+
(0x1b000 <= codePoint && codePoint <= 0x1b001) ||
|
|
58
|
+
// Enclosed Ideographic Supplement
|
|
59
|
+
(0x1f200 <= codePoint && codePoint <= 0x1f251) ||
|
|
60
|
+
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
|
|
61
|
+
(0x20000 <= codePoint && codePoint <= 0x3fffd))
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get the number of the columns that the string uses in a terminal.
|
|
67
|
+
* It is based on https://github.com/sindresorhus/string-width v4
|
|
68
|
+
*
|
|
69
|
+
* Node.js has the same function internally, but it is not a public API yet.
|
|
70
|
+
* Replace this file with the function of Node.js when it is available in the supported versions of Node.js.
|
|
71
|
+
*
|
|
72
|
+
* - An escape sequence, a control character and a combining character use no column
|
|
73
|
+
* - A fullwidth character and an emoji use two columns
|
|
74
|
+
*/
|
|
75
|
+
export const stringWidth = (text: string): number => {
|
|
76
|
+
if (text.length === 0) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
const strippedText = stripVTControlCharacters(text);
|
|
80
|
+
const plainText = MAY_HAVE_EMOJI_PATTERN.test(strippedText)
|
|
81
|
+
? strippedText.replace(getEmojiPattern(), " ")
|
|
82
|
+
: strippedText;
|
|
83
|
+
let width = 0;
|
|
84
|
+
for (const character of plainText) {
|
|
85
|
+
const codePoint = character.codePointAt(0)!;
|
|
86
|
+
// Ignore control characters
|
|
87
|
+
if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
// Ignore combining characters
|
|
91
|
+
if (codePoint >= 0x300 && codePoint <= 0x36f) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
width += isFullwidthCodePoint(codePoint) ? 2 : 1;
|
|
95
|
+
}
|
|
96
|
+
return width;
|
|
97
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -7,20 +7,29 @@ import path from "node:path";
|
|
|
7
7
|
import debug0 from "debug";
|
|
8
8
|
import { tryResolve, dynamicImport } from "@textlint/resolver";
|
|
9
9
|
// formatters
|
|
10
|
-
import compatsFormatter from "./formatters/compats.js";
|
|
11
|
-
import diffFormatter from "./formatters/diff.js";
|
|
12
|
-
import fixedResultFormatter from "./formatters/fixed-result.js";
|
|
13
|
-
import jsonFormatter from "./formatters/json.js";
|
|
14
|
-
import stylishFormatter from "./formatters/stylish.js";
|
|
15
10
|
|
|
11
|
+
type BuiltinFormatter = (results: TextlintFixResult[], options: FormatterOptions) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Each formatter is loaded when it is used.
|
|
14
|
+
* For example, `diff` formatter loads `diff`, but `stylish` formatter does not.
|
|
15
|
+
*/
|
|
16
16
|
const builtinFormatterList = {
|
|
17
|
-
compats:
|
|
18
|
-
diff:
|
|
19
|
-
"fixed-result":
|
|
20
|
-
json:
|
|
21
|
-
stylish:
|
|
17
|
+
compats: () => import("./formatters/compats.js"),
|
|
18
|
+
diff: () => import("./formatters/diff.js"),
|
|
19
|
+
"fixed-result": () => import("./formatters/fixed-result.js"),
|
|
20
|
+
json: () => import("./formatters/json.js"),
|
|
21
|
+
stylish: () => import("./formatters/stylish.js")
|
|
22
22
|
} as const;
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Load the formatter function of a built-in formatter.
|
|
25
|
+
* A formatter file is a CommonJS module, so `import()` returns `{ default: module.exports }` in Node.js.
|
|
26
|
+
* `moduleInterop` gets the default export from `module.exports`.
|
|
27
|
+
*/
|
|
28
|
+
const loadBuiltinFormatter = async (name: BuiltInFormatterName): Promise<BuiltinFormatter> => {
|
|
29
|
+
const mod: { default: unknown } = await builtinFormatterList[name]();
|
|
30
|
+
return moduleInterop(mod.default) as BuiltinFormatter;
|
|
31
|
+
};
|
|
32
|
+
type FormatterFunction = (results: TextlintFixResult[], formatterConfig: FormatterOptions) => string;
|
|
24
33
|
const isFormatterFunction = (formatter: unknown): formatter is FormatterFunction => {
|
|
25
34
|
return typeof formatter === "function";
|
|
26
35
|
};
|
|
@@ -28,15 +37,57 @@ type BuiltInFormatterName = keyof typeof builtinFormatterList;
|
|
|
28
37
|
const builtinFormatterNames = Object.keys(builtinFormatterList);
|
|
29
38
|
const debug = debug0("textlint:textfix-formatter");
|
|
30
39
|
|
|
31
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Get the content of the source before fixing by `filePath` of the result.
|
|
42
|
+
* It returns `undefined` if the caller does not have the content.
|
|
43
|
+
*/
|
|
44
|
+
export type FormatterGetSource = (filePath: string) => string | undefined;
|
|
45
|
+
export type FormatterConfig = {
|
|
46
|
+
color?: boolean;
|
|
47
|
+
formatterName: string;
|
|
48
|
+
/**
|
|
49
|
+
* Get the content of the source before fixing by `filePath` of the result.
|
|
50
|
+
* For example, "diff" formatter uses it.
|
|
51
|
+
* If it is not given or returns `undefined`, the formatter gets the content of the file of `filePath`.
|
|
52
|
+
*/
|
|
53
|
+
getSource?: FormatterGetSource;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The options that the formatter receives.
|
|
57
|
+
* `loadFormatter()` always passes `getSource`.
|
|
58
|
+
*/
|
|
59
|
+
export type FormatterOptions = FormatterConfig & {
|
|
60
|
+
getSource: FormatterGetSource;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Create `getSource` for the formatter.
|
|
64
|
+
* It returns the content from `getSource` of the caller, and reads the file of `filePath` if the caller does not have the content.
|
|
65
|
+
* It returns `undefined` if the file can not be read.
|
|
66
|
+
* A formatter gets the content only from this function, and does not read the file by itself.
|
|
67
|
+
*/
|
|
68
|
+
const createGetSource = (getSource?: FormatterGetSource): FormatterGetSource => {
|
|
69
|
+
return (filePath) => {
|
|
70
|
+
const source = getSource?.(filePath);
|
|
71
|
+
if (source !== undefined) {
|
|
72
|
+
return source;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
return fs.readFileSync(filePath, "utf-8");
|
|
76
|
+
} catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
32
81
|
|
|
33
|
-
export async function loadFormatter(
|
|
82
|
+
export async function loadFormatter(config: FormatterConfig) {
|
|
83
|
+
const formatterConfig: FormatterOptions = { ...config, getSource: createGetSource(config.getSource) };
|
|
34
84
|
const formatterName = formatterConfig.formatterName;
|
|
35
85
|
debug(`formatterName: ${formatterName}`);
|
|
36
86
|
if (builtinFormatterNames.includes(formatterName as BuiltInFormatterName)) {
|
|
87
|
+
const builtinFormatter = await loadBuiltinFormatter(formatterName as BuiltInFormatterName);
|
|
37
88
|
return {
|
|
38
89
|
format(results: TextlintFixResult[]) {
|
|
39
|
-
return
|
|
90
|
+
return builtinFormatter(results, formatterConfig);
|
|
40
91
|
}
|
|
41
92
|
};
|
|
42
93
|
}
|