Skip to content

Commit 52b043e

Browse files
authored
BREAKING CHANGE: require Node.js 18+ (#104)
* refactor!: remove polyfill for old Node.js - The output is ES2022 - It require Node.js 18+ * refactor: use named import for prh * refactor!: `export default` instead of `module.exports` * CI: add .github/release.yml * CI: update node versions
1 parent 5785d83 commit 52b043e

File tree

7 files changed

+498
-434
lines changed

7 files changed

+498
-434
lines changed

.github/release.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
changelog:
2+
exclude:
3+
labels:
4+
- 'Type: Meta'
5+
- 'Type: Question'
6+
- 'Type: Release'
7+
8+
categories:
9+
- title: Security Fixes
10+
labels: ['Type: Security']
11+
- title: Breaking Changes
12+
labels: ['Type: Breaking Change']
13+
- title: Features
14+
labels: ['Type: Feature']
15+
- title: Bug Fixes
16+
labels: ['Type: Bug']
17+
- title: Documentation
18+
labels: ['Type: Documentation']
19+
- title: Refactoring
20+
labels: ['Type: Refactoring']
21+
- title: Testing
22+
labels: ['Type: Testing']
23+
- title: Maintenance
24+
labels: ['Type: Maintenance']
25+
- title: CI
26+
labels: ['Type: CI']
27+
- title: Dependency Updates
28+
labels: ['Type: Dependencies', "dependencies"]
29+
- title: Other Changes
30+
labels: ['*']

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
node-version: [ 16, 18 ]
11+
node-version: [ 18, 20 ]
1212
steps:
1313
- name: checkout
1414
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
"dependencies": {
3737
"@babel/parser": "^7.23.9",
3838
"prh": "^5.4.4",
39-
"textlint-rule-helper": "^2.3.1",
40-
"untildify": "^4.0.0"
39+
"textlint-rule-helper": "^2.3.1"
4140
},
4241
"devDependencies": {
43-
"lint-staged": "^13.3.0",
44-
"prettier": "^2.8.3",
45-
"textlint": "13.4.1",
46-
"textlint-scripts": "^13.4.1"
42+
"@textlint/legacy-textlint-core": "^14.0.1",
43+
"lint-staged": "^15.2.1",
44+
"prettier": "^3.2.4",
45+
"textlint": "14.0.1",
46+
"textlint-scripts": "^14.0.1"
4747
},
4848
"prettier": {
4949
"singleQuote": false,

src/textlint-rule-prh.js

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
// LICENSE : MIT
2-
"use strict";
32
import { RuleHelper } from "textlint-rule-helper";
4-
53
import { parse } from "@babel/parser";
6-
/**
7-
* RegExp#flags polyfill
8-
*/
9-
if (RegExp.prototype.flags === undefined) {
10-
Object.defineProperty(RegExp.prototype, "flags", {
11-
configurable: true,
12-
get: function() {
13-
return this.toString().match(/[gimuy]*$/)[0];
14-
}
15-
});
16-
}
4+
import { fromYAMLFilePath, fromYAML } from "prh";
5+
import path from "node:path";
6+
import os from "node:os";
177

18-
const prh = require("prh");
19-
const path = require("path");
20-
const untildify = require("untildify");
8+
const homeDirectory = os.homedir();
219

10+
const untildify = (filePath) => {
11+
return homeDirectory ? filePath.replace(/^~(?=$|\/|\\)/, homeDirectory) : filePath;
12+
};
2213
const defaultOptions = {
2314
checkLink: false,
2415
checkBlockQuote: false,
@@ -39,10 +30,10 @@ function createPrhEngine(rulePaths, baseDir) {
3930
if (rulePaths.length === 0) {
4031
return null;
4132
}
42-
const expandedRulePaths = rulePaths.map(rulePath => untildify(rulePath));
43-
const prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0]));
44-
expandedRulePaths.slice(1).forEach(ruleFilePath => {
45-
const config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
33+
const expandedRulePaths = rulePaths.map((rulePath) => untildify(rulePath));
34+
const prhEngine = fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0]));
35+
expandedRulePaths.slice(1).forEach((ruleFilePath) => {
36+
const config = fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
4637
prhEngine.merge(config);
4738
});
4839
return prhEngine;
@@ -53,24 +44,24 @@ function createPrhEngineFromContents(yamlContents) {
5344
return null;
5445
}
5546
const dummyFilePath = "";
56-
const prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]);
57-
yamlContents.slice(1).forEach(content => {
58-
const config = prh.fromYAML(dummyFilePath, content);
47+
const prhEngine = fromYAML(dummyFilePath, yamlContents[0]);
48+
yamlContents.slice(1).forEach((content) => {
49+
const config = fromYAML(dummyFilePath, content);
5950
prhEngine.merge(config);
6051
});
6152
return prhEngine;
6253
}
6354

6455
function mergePrh(...engines) {
65-
const engines_ = engines.filter(engine => !!engine);
56+
const engines_ = engines.filter((engine) => !!engine);
6657
const mainEngine = engines_[0];
67-
engines_.slice(1).forEach(engine => {
58+
engines_.slice(1).forEach((engine) => {
6859
mainEngine.merge(engine);
6960
});
7061
return mainEngine;
7162
}
7263

73-
const assertOptions = options => {
64+
const assertOptions = (options) => {
7465
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") {
7566
throw new Error(`textlint-rule-prh require Rule Options.
7667
Please set .textlintrc:
@@ -107,19 +98,19 @@ const createIgnoreNodeTypes = (options, Syntax) => {
10798
* @param {ChangeSet} changeSet
10899
* @param {string} str
109100
* @param {function({
110-
matchStartIndex: number,
111-
matchEndIndex: number,
112-
actual: string
113-
expected: string
114-
})}onChangeOfMatch
101+
matchStartIndex: number,
102+
matchEndIndex: number,
103+
actual: string
104+
expected: string
105+
})}onChangeOfMatch
115106
*/
116107
const forEachChange = (changeSet, str, onChangeOfMatch) => {
117-
const sortedDiffs = changeSet.diffs.sort(function(a, b) {
108+
const sortedDiffs = changeSet.diffs.sort(function (a, b) {
118109
return a.index - b.index;
119110
});
120111
let delta = 0;
121-
sortedDiffs.forEach(function(diff) {
122-
const result = diff.expected.replace(/\$([0-9]{1,2})/g, function(match, g1) {
112+
sortedDiffs.forEach(function (diff) {
113+
const result = diff.expected.replace(/\$([0-9]{1,2})/g, function (match, g1) {
123114
const index = parseInt(g1);
124115
if (index === 0 || diff.matches.length - 1 < index) {
125116
return match;
@@ -144,7 +135,7 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
144135
delta += result.length - diff.matches[0].length;
145136
});
146137
};
147-
const getConfigBaseDir = context => {
138+
const getConfigBaseDir = (context) => {
148139
if (typeof context.getConfigBaseDir === "function") {
149140
return context.getConfigBaseDir() || process.cwd();
150141
}
@@ -236,7 +227,7 @@ function reporter(context, userOptions = {}) {
236227
if (!lang) {
237228
return;
238229
}
239-
const checkLang = codeCommentTypes.some(type => {
230+
const checkLang = codeCommentTypes.some((type) => {
240231
return type === node.lang;
241232
});
242233
if (!checkLang) {
@@ -245,7 +236,7 @@ function reporter(context, userOptions = {}) {
245236
const rawText = getSource(node);
246237
const codeText = getUntrimmedCode(node, rawText);
247238
const sourceBlockDiffIndex = rawText !== node.value ? rawText.indexOf(codeText) : 0;
248-
const reportComment = comment => {
239+
const reportComment = (comment) => {
249240
// to get position from index
250241
// https://github.com/prh/prh/issues/29
251242
const dummyFilePath = "";
@@ -286,7 +277,7 @@ function reporter(context, userOptions = {}) {
286277
if (!comments) {
287278
return;
288279
}
289-
comments.forEach(comment => {
280+
comments.forEach((comment) => {
290281
reportComment(comment);
291282
});
292283
} catch (error) {
@@ -299,7 +290,7 @@ function reporter(context, userOptions = {}) {
299290
};
300291
}
301292

302-
module.exports = {
293+
export default {
303294
linter: reporter,
304295
fixer: reporter
305296
};

test/prh-rule-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// LICENSE : MIT
22
"use strict";
33
import assert from "assert";
4-
import { textlint } from "textlint";
4+
import { TextLintCore } from "@textlint/legacy-textlint-core";
55
import rule from "../src/textlint-rule-prh";
6+
7+
const textlint = new TextLintCore();
68
describe("prh-rule-test", function () {
79
beforeEach(function () {
810
textlint.setupRules(

test/textlintrc-test.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22
"use strict";
33
import assert from "assert";
44
import fs from "fs";
5-
import { TextLintEngine, TextLintCore } from "textlint";
5+
import { TextLintCore } from "@textlint/legacy-textlint-core";
66
import rule from "../src/textlint-rule-prh";
77
import path from "path";
88

9-
describe(".textlintrc test", function() {
10-
context("when use .textlintrc", function() {
11-
it("should resolve path to rule.yaml", function() {
12-
const engine = new TextLintEngine({
13-
configFile: path.join(__dirname, "fixtures/.textlintrc"),
14-
rulesBaseDirectory: path.join(__dirname, "../src")
15-
});
16-
return engine.executeOnText("jquery").then(([result]) => {
17-
assert(result.messages.length === 1);
18-
assert(result.messages[0].line === 1);
19-
assert(result.messages[0].column === 1);
20-
});
21-
});
22-
});
9+
describe(".textlintrc test", function () {
2310
context("options", () => {
24-
it("should resolve path to rule.yaml", function() {
11+
it("should resolve path to rule.yaml", function () {
2512
var textlint = new TextLintCore();
2613
textlint.setupRules(
2714
{
@@ -33,13 +20,13 @@ describe(".textlintrc test", function() {
3320
}
3421
}
3522
);
36-
return textlint.lintMarkdown("jquery").then(result => {
23+
return textlint.lintMarkdown("jquery").then((result) => {
3724
assert(result.messages.length === 1);
3825
assert(result.messages[0].line === 1);
3926
assert(result.messages[0].column === 1);
4027
});
4128
});
42-
it("should resolve yaml content", function() {
29+
it("should resolve yaml content", function () {
4330
var textlint = new TextLintCore();
4431
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
4532
textlint.setupRules(
@@ -52,13 +39,13 @@ describe(".textlintrc test", function() {
5239
}
5340
}
5441
);
55-
return textlint.lintMarkdown("jquery").then(result => {
42+
return textlint.lintMarkdown("jquery").then((result) => {
5643
assert(result.messages.length === 1);
5744
assert(result.messages[0].line === 1);
5845
assert(result.messages[0].column === 1);
5946
});
6047
});
61-
it("should resolve yaml file and content", function() {
48+
it("should resolve yaml file and content", function () {
6249
var textlint = new TextLintCore();
6350
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
6451
textlint.setupRules(
@@ -74,7 +61,7 @@ describe(".textlintrc test", function() {
7461
}
7562
}
7663
);
77-
return textlint.lintMarkdown("jquery A").then(result => {
64+
return textlint.lintMarkdown("jquery A").then((result) => {
7865
assert(result.messages.length === 2);
7966
assert(result.messages[0].line === 1);
8067
assert(result.messages[0].column === 1);
@@ -84,9 +71,9 @@ describe(".textlintrc test", function() {
8471
});
8572
});
8673

87-
context("prh features", function() {
88-
describe("import", function() {
89-
it("should work import directive", function() {
74+
context("prh features", function () {
75+
describe("import", function () {
76+
it("should work import directive", function () {
9077
var textlint = new TextLintCore();
9178
textlint.setupRules(
9279
{
@@ -100,13 +87,13 @@ describe(".textlintrc test", function() {
10087
);
10188
return textlint
10289
.lintMarkdown("A")
103-
.then(result => {
90+
.then((result) => {
10491
assert(result.messages.length === 1);
10592
var message = result.messages[0].message;
10693
assert.equal(message, "A => a");
10794
})
10895
.then(() => {
109-
return textlint.lintMarkdown("B").then(result => {
96+
return textlint.lintMarkdown("B").then((result) => {
11097
assert(result.messages.length === 1);
11198
var messageB = result.messages[0].message;
11299
assert.equal(messageB, "B => b");

0 commit comments

Comments
 (0)