Skip to content

Commit

Permalink
BREAKING CHANGE: require Node.js 18+ (#104)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
azu authored Feb 4, 2024
1 parent 5785d83 commit 52b043e
Show file tree
Hide file tree
Showing 7 changed files with 498 additions and 434 deletions.
30 changes: 30 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
changelog:
exclude:
labels:
- 'Type: Meta'
- 'Type: Question'
- 'Type: Release'

categories:
- title: Security Fixes
labels: ['Type: Security']
- title: Breaking Changes
labels: ['Type: Breaking Change']
- title: Features
labels: ['Type: Feature']
- title: Bug Fixes
labels: ['Type: Bug']
- title: Documentation
labels: ['Type: Documentation']
- title: Refactoring
labels: ['Type: Refactoring']
- title: Testing
labels: ['Type: Testing']
- title: Maintenance
labels: ['Type: Maintenance']
- title: CI
labels: ['Type: CI']
- title: Dependency Updates
labels: ['Type: Dependencies', "dependencies"]
- title: Other Changes
labels: ['*']
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16, 18 ]
node-version: [ 18, 20 ]
steps:
- name: checkout
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
"dependencies": {
"@babel/parser": "^7.23.9",
"prh": "^5.4.4",
"textlint-rule-helper": "^2.3.1",
"untildify": "^4.0.0"
"textlint-rule-helper": "^2.3.1"
},
"devDependencies": {
"lint-staged": "^13.3.0",
"prettier": "^2.8.3",
"textlint": "13.4.1",
"textlint-scripts": "^13.4.1"
"@textlint/legacy-textlint-core": "^14.0.1",
"lint-staged": "^15.2.1",
"prettier": "^3.2.4",
"textlint": "14.0.1",
"textlint-scripts": "^14.0.1"
},
"prettier": {
"singleQuote": false,
Expand Down
69 changes: 30 additions & 39 deletions src/textlint-rule-prh.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
// LICENSE : MIT
"use strict";
import { RuleHelper } from "textlint-rule-helper";

import { parse } from "@babel/parser";
/**
* RegExp#flags polyfill
*/
if (RegExp.prototype.flags === undefined) {
Object.defineProperty(RegExp.prototype, "flags", {
configurable: true,
get: function() {
return this.toString().match(/[gimuy]*$/)[0];
}
});
}
import { fromYAMLFilePath, fromYAML } from "prh";
import path from "node:path";
import os from "node:os";

const prh = require("prh");
const path = require("path");
const untildify = require("untildify");
const homeDirectory = os.homedir();

const untildify = (filePath) => {
return homeDirectory ? filePath.replace(/^~(?=$|\/|\\)/, homeDirectory) : filePath;
};
const defaultOptions = {
checkLink: false,
checkBlockQuote: false,
Expand All @@ -39,10 +30,10 @@ function createPrhEngine(rulePaths, baseDir) {
if (rulePaths.length === 0) {
return null;
}
const expandedRulePaths = rulePaths.map(rulePath => untildify(rulePath));
const prhEngine = prh.fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0]));
expandedRulePaths.slice(1).forEach(ruleFilePath => {
const config = prh.fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
const expandedRulePaths = rulePaths.map((rulePath) => untildify(rulePath));
const prhEngine = fromYAMLFilePath(path.resolve(baseDir, expandedRulePaths[0]));
expandedRulePaths.slice(1).forEach((ruleFilePath) => {
const config = fromYAMLFilePath(path.resolve(baseDir, ruleFilePath));
prhEngine.merge(config);
});
return prhEngine;
Expand All @@ -53,24 +44,24 @@ function createPrhEngineFromContents(yamlContents) {
return null;
}
const dummyFilePath = "";
const prhEngine = prh.fromYAML(dummyFilePath, yamlContents[0]);
yamlContents.slice(1).forEach(content => {
const config = prh.fromYAML(dummyFilePath, content);
const prhEngine = fromYAML(dummyFilePath, yamlContents[0]);
yamlContents.slice(1).forEach((content) => {
const config = fromYAML(dummyFilePath, content);
prhEngine.merge(config);
});
return prhEngine;
}

function mergePrh(...engines) {
const engines_ = engines.filter(engine => !!engine);
const engines_ = engines.filter((engine) => !!engine);
const mainEngine = engines_[0];
engines_.slice(1).forEach(engine => {
engines_.slice(1).forEach((engine) => {
mainEngine.merge(engine);
});
return mainEngine;
}

const assertOptions = options => {
const assertOptions = (options) => {
if (typeof options.ruleContents === "undefined" && typeof options.rulePaths === "undefined") {
throw new Error(`textlint-rule-prh require Rule Options.
Please set .textlintrc:
Expand Down Expand Up @@ -107,19 +98,19 @@ const createIgnoreNodeTypes = (options, Syntax) => {
* @param {ChangeSet} changeSet
* @param {string} str
* @param {function({
matchStartIndex: number,
matchEndIndex: number,
actual: string
expected: string
})}onChangeOfMatch
matchStartIndex: number,
matchEndIndex: number,
actual: string
expected: string
})}onChangeOfMatch
*/
const forEachChange = (changeSet, str, onChangeOfMatch) => {
const sortedDiffs = changeSet.diffs.sort(function(a, b) {
const sortedDiffs = changeSet.diffs.sort(function (a, b) {
return a.index - b.index;
});
let delta = 0;
sortedDiffs.forEach(function(diff) {
const result = diff.expected.replace(/\$([0-9]{1,2})/g, function(match, g1) {
sortedDiffs.forEach(function (diff) {
const result = diff.expected.replace(/\$([0-9]{1,2})/g, function (match, g1) {
const index = parseInt(g1);
if (index === 0 || diff.matches.length - 1 < index) {
return match;
Expand All @@ -144,7 +135,7 @@ const forEachChange = (changeSet, str, onChangeOfMatch) => {
delta += result.length - diff.matches[0].length;
});
};
const getConfigBaseDir = context => {
const getConfigBaseDir = (context) => {
if (typeof context.getConfigBaseDir === "function") {
return context.getConfigBaseDir() || process.cwd();
}
Expand Down Expand Up @@ -236,7 +227,7 @@ function reporter(context, userOptions = {}) {
if (!lang) {
return;
}
const checkLang = codeCommentTypes.some(type => {
const checkLang = codeCommentTypes.some((type) => {
return type === node.lang;
});
if (!checkLang) {
Expand All @@ -245,7 +236,7 @@ function reporter(context, userOptions = {}) {
const rawText = getSource(node);
const codeText = getUntrimmedCode(node, rawText);
const sourceBlockDiffIndex = rawText !== node.value ? rawText.indexOf(codeText) : 0;
const reportComment = comment => {
const reportComment = (comment) => {
// to get position from index
// https://github.com/prh/prh/issues/29
const dummyFilePath = "";
Expand Down Expand Up @@ -286,7 +277,7 @@ function reporter(context, userOptions = {}) {
if (!comments) {
return;
}
comments.forEach(comment => {
comments.forEach((comment) => {
reportComment(comment);
});
} catch (error) {
Expand All @@ -299,7 +290,7 @@ function reporter(context, userOptions = {}) {
};
}

module.exports = {
export default {
linter: reporter,
fixer: reporter
};
4 changes: 3 additions & 1 deletion test/prh-rule-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// LICENSE : MIT
"use strict";
import assert from "assert";
import { textlint } from "textlint";
import { TextLintCore } from "@textlint/legacy-textlint-core";
import rule from "../src/textlint-rule-prh";

const textlint = new TextLintCore();
describe("prh-rule-test", function () {
beforeEach(function () {
textlint.setupRules(
Expand Down
39 changes: 13 additions & 26 deletions test/textlintrc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@
"use strict";
import assert from "assert";
import fs from "fs";
import { TextLintEngine, TextLintCore } from "textlint";
import { TextLintCore } from "@textlint/legacy-textlint-core";
import rule from "../src/textlint-rule-prh";
import path from "path";

describe(".textlintrc test", function() {
context("when use .textlintrc", function() {
it("should resolve path to rule.yaml", function() {
const engine = new TextLintEngine({
configFile: path.join(__dirname, "fixtures/.textlintrc"),
rulesBaseDirectory: path.join(__dirname, "../src")
});
return engine.executeOnText("jquery").then(([result]) => {
assert(result.messages.length === 1);
assert(result.messages[0].line === 1);
assert(result.messages[0].column === 1);
});
});
});
describe(".textlintrc test", function () {
context("options", () => {
it("should resolve path to rule.yaml", function() {
it("should resolve path to rule.yaml", function () {
var textlint = new TextLintCore();
textlint.setupRules(
{
Expand All @@ -33,13 +20,13 @@ describe(".textlintrc test", function() {
}
}
);
return textlint.lintMarkdown("jquery").then(result => {
return textlint.lintMarkdown("jquery").then((result) => {
assert(result.messages.length === 1);
assert(result.messages[0].line === 1);
assert(result.messages[0].column === 1);
});
});
it("should resolve yaml content", function() {
it("should resolve yaml content", function () {
var textlint = new TextLintCore();
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
textlint.setupRules(
Expand All @@ -52,13 +39,13 @@ describe(".textlintrc test", function() {
}
}
);
return textlint.lintMarkdown("jquery").then(result => {
return textlint.lintMarkdown("jquery").then((result) => {
assert(result.messages.length === 1);
assert(result.messages[0].line === 1);
assert(result.messages[0].column === 1);
});
});
it("should resolve yaml file and content", function() {
it("should resolve yaml file and content", function () {
var textlint = new TextLintCore();
var content = fs.readFileSync(path.join(__dirname, "fixtures", "rule.yaml"), "utf-8");
textlint.setupRules(
Expand All @@ -74,7 +61,7 @@ describe(".textlintrc test", function() {
}
}
);
return textlint.lintMarkdown("jquery A").then(result => {
return textlint.lintMarkdown("jquery A").then((result) => {
assert(result.messages.length === 2);
assert(result.messages[0].line === 1);
assert(result.messages[0].column === 1);
Expand All @@ -84,9 +71,9 @@ describe(".textlintrc test", function() {
});
});

context("prh features", function() {
describe("import", function() {
it("should work import directive", function() {
context("prh features", function () {
describe("import", function () {
it("should work import directive", function () {
var textlint = new TextLintCore();
textlint.setupRules(
{
Expand All @@ -100,13 +87,13 @@ describe(".textlintrc test", function() {
);
return textlint
.lintMarkdown("A")
.then(result => {
.then((result) => {
assert(result.messages.length === 1);
var message = result.messages[0].message;
assert.equal(message, "A => a");
})
.then(() => {
return textlint.lintMarkdown("B").then(result => {
return textlint.lintMarkdown("B").then((result) => {
assert(result.messages.length === 1);
var messageB = result.messages[0].message;
assert.equal(messageB, "B => b");
Expand Down
Loading

0 comments on commit 52b043e

Please sign in to comment.