Skip to content

Commit

Permalink
fix: support nested context (#15)
Browse files Browse the repository at this point in the history
* fix: support nested context

* test: add test

* CI: update node versions
  • Loading branch information
azu committed Nov 10, 2022
1 parent fa5c001 commit 832c626
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12, 14]
node-version: [ 16, 18 ]
steps:
- name: checkout
uses: actions/checkout@v2
Expand Down
33 changes: 30 additions & 3 deletions src/parser/PairMaker.js
Expand Up @@ -69,17 +69,44 @@ const PAIR_MARKS = [
}
];

// create entries
// [start.key, mark]
// [end.key, mark]
const PAIR_MARKS_ENTRIES = PAIR_MARKS.map(mark => {
return [
[mark.start, mark],
[mark.end, mark]
];
}).flat(1);

/**
* Optimized Map
* @type Map<string, {key:string,start:string,end:string}>
*/
const PAIR_MARKS_KEY_Map = new Map(PAIR_MARKS_ENTRIES);
const matchPair = (string) => {
return PAIR_MARKS_KEY_Map.get(string);
}
// For readme
// console.log(PAIR_MARKS.map(pair => `- ${pair.key}: \`${pair.start}\` and \`${pair.end}\``).join("\n"));
export class PairMaker {
/**
* @param {import("./SourceCode").SourceCode} sourceCode
* @returns
*/
mark(sourceCode) {
const string = sourceCode.read();
if (!string) {
return;
}
// if current is in a context, should not start other context.
// PairMaker does not support nest context by design.
if (sourceCode.isInContext()) {

const matchedPair = matchPair(string)
if (!matchedPair){
return;
}
// support nested pair
// {"{test}"}
if (sourceCode.isInContext(matchedPair)) {
// check that string is end mark?
const pair = PAIR_MARKS.find(pair => pair.end === string);
if (pair) {
Expand Down
2 changes: 2 additions & 0 deletions test/textlint-rule-no-unmatched-pair-test.js
Expand Up @@ -7,6 +7,8 @@ const rule = require("../src/textlint-rule-no-unmatched-pair.js");
// ruleName, rule, { valid, invalid }
tester.run("textlint-rule-no-unmatched-pair", rule, {
valid: [
`{"{ABC}"}`,
'test {"{ABC`{"{ABC}"}`}"} ok.',
"これは(秘密)です。",
`John said "Hello World!".`,
"`(` is ok.", "文字列リテラルには3種類ありますが、まずは`\"`(ダブルクオート)と`'`(シングルクオート)について見ていきます。",
Expand Down

0 comments on commit 832c626

Please sign in to comment.