Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support nested context #15

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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