Skip to content

Commit 832c626

Browse files
authored
fix: support nested context (#15)
* fix: support nested context * test: add test * CI: update node versions
1 parent fa5c001 commit 832c626

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
runs-on: ubuntu-latest
77
strategy:
88
matrix:
9-
node-version: [12, 14]
9+
node-version: [ 16, 18 ]
1010
steps:
1111
- name: checkout
1212
uses: actions/checkout@v2

src/parser/PairMaker.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,44 @@ const PAIR_MARKS = [
6969
}
7070
];
7171

72+
// create entries
73+
// [start.key, mark]
74+
// [end.key, mark]
75+
const PAIR_MARKS_ENTRIES = PAIR_MARKS.map(mark => {
76+
return [
77+
[mark.start, mark],
78+
[mark.end, mark]
79+
];
80+
}).flat(1);
81+
82+
/**
83+
* Optimized Map
84+
* @type Map<string, {key:string,start:string,end:string}>
85+
*/
86+
const PAIR_MARKS_KEY_Map = new Map(PAIR_MARKS_ENTRIES);
87+
const matchPair = (string) => {
88+
return PAIR_MARKS_KEY_Map.get(string);
89+
}
7290
// For readme
7391
// console.log(PAIR_MARKS.map(pair => `- ${pair.key}: \`${pair.start}\` and \`${pair.end}\``).join("\n"));
7492
export class PairMaker {
93+
/**
94+
* @param {import("./SourceCode").SourceCode} sourceCode
95+
* @returns
96+
*/
7597
mark(sourceCode) {
7698
const string = sourceCode.read();
7799
if (!string) {
78100
return;
79101
}
80-
// if current is in a context, should not start other context.
81-
// PairMaker does not support nest context by design.
82-
if (sourceCode.isInContext()) {
102+
103+
const matchedPair = matchPair(string)
104+
if (!matchedPair){
105+
return;
106+
}
107+
// support nested pair
108+
// {"{test}"}
109+
if (sourceCode.isInContext(matchedPair)) {
83110
// check that string is end mark?
84111
const pair = PAIR_MARKS.find(pair => pair.end === string);
85112
if (pair) {

test/textlint-rule-no-unmatched-pair-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const rule = require("../src/textlint-rule-no-unmatched-pair.js");
77
// ruleName, rule, { valid, invalid }
88
tester.run("textlint-rule-no-unmatched-pair", rule, {
99
valid: [
10+
`{"{ABC}"}`,
11+
'test {"{ABC`{"{ABC}"}`}"} ok.',
1012
"これは(秘密)です。",
1113
`John said "Hello World!".`,
1214
"`(` is ok.", "文字列リテラルには3種類ありますが、まずは`\"`(ダブルクオート)と`'`(シングルクオート)について見ていきます。",

0 commit comments

Comments
 (0)