Skip to content

Commit 9fd3b6e

Browse files
committed
feat(rule): add textlint-rule-ja-space-around-code
1 parent ac996b8 commit 9fd3b6e

File tree

5 files changed

+205
-3
lines changed

5 files changed

+205
-3
lines changed

packages/textlint-rule-ja-no-space-between-half-and-full-width/src/textlint-rule-ja-no-space-between-half-and-full-width.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ function reporter(context) {
1515
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
1616
return;
1717
}
18-
let text = getSource(node);
18+
const text = getSource(node);
1919
// アルファベットと全角の間は半角スペースではない
20-
let betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/);
21-
let betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])([  ])[A-Za-z0-9]/);
20+
const betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/);
21+
const betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])([  ])[A-Za-z0-9]/);
2222
const reportMatch = (match) => {
2323
const {index} = match;
2424
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "textlint-rule-ja-space-around-code",
3+
"version": "1.0.0",
4+
"description": "インラインコードの周りをスペースで囲むかどうかを決めるtextlintルール",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
8+
"watch": "babel src --out-dir lib --watch --source-maps",
9+
"prepublish": "npm run --if-present build",
10+
"test": "mocha"
11+
},
12+
"keywords": [
13+
"textlint"
14+
],
15+
"author": "azu",
16+
"license": "MIT",
17+
"devDependencies": {
18+
"babel-cli": "^6.0.0",
19+
"babel-preset-es2015": "^6.0.0",
20+
"textlint-tester": "^2.0.0",
21+
"mocha": "^2.3.3"
22+
},
23+
"dependencies": {
24+
"match-index": "^1.0.1",
25+
"textlint-rule-helper": "^2.0.0"
26+
}
27+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
const isJapaneseChar = (text) => {
4+
return /^(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])$/.test(text);
5+
};
6+
const defaultOptions = {
7+
"before": false,
8+
"after": false
9+
};
10+
function reporter(context, options) {
11+
const {Syntax, RuleError, report, fixer, getSource} = context;
12+
const allowBeforeSpace = options.before || defaultOptions.before;
13+
const allowAfterSpace = options.after || defaultOptions.after;
14+
return {
15+
[Syntax.Code](node){
16+
const nodeText = getSource(node);
17+
// | `code` |
18+
// InlineCodeの前後2文字文を取得
19+
// スペース + 前後の文字を取るため
20+
// 文字が日本語以外はチェック対象にしないようにするため
21+
const textWithPadding = getSource(node, 2, 2);
22+
if (!textWithPadding) {
23+
return;
24+
}
25+
const beforeChar = textWithPadding[1];
26+
const beforeBeforeChar = textWithPadding[0];
27+
const existBeforeChar = nodeText[0] !== beforeChar;
28+
const afterChar = textWithPadding[textWithPadding.length - 2];
29+
const afterAfterChar = textWithPadding[textWithPadding.length - 1];
30+
const existAfterChar = nodeText[textWithPadding.length - 1] !== afterChar;
31+
// InlineCodeの前に文字が存在している時のみチェック
32+
if (existBeforeChar) {
33+
if (allowBeforeSpace) {
34+
if (beforeChar !== " " && isJapaneseChar(beforeChar)) {
35+
report(node, new RuleError("インラインコードの前にスペースを入れてください。", {
36+
index: -1,// before `
37+
fix: fixer.insertTextBeforeRange([0, 0], " ")
38+
}));
39+
}
40+
} else {
41+
if (beforeChar === " " && isJapaneseChar(beforeBeforeChar)) {
42+
report(node, new RuleError("インラインコードの前にスペースを入れません。", {
43+
index: -1, // before `
44+
fix: fixer.replaceTextRange([-1, 0], "")
45+
}));
46+
}
47+
}
48+
}
49+
// InlineCodeの後に文字が存在している時のみチェック
50+
if (existAfterChar) {
51+
if (allowAfterSpace) {
52+
if (afterChar !== " " && isJapaneseChar(beforeChar)) {
53+
report(node, new RuleError("インラインコードの後にスペースを入れてください。", {
54+
index: nodeText.length,
55+
fix: fixer.insertTextAfterRange([0, nodeText.length], " ")
56+
}));
57+
}
58+
} else {
59+
if (afterChar === " " && isJapaneseChar(afterAfterChar)) {
60+
report(node, new RuleError("インラインコードの後にスペースを入れません。", {
61+
index: nodeText.length + 1,
62+
fix: fixer.replaceTextRange([nodeText.length, nodeText.length + 1], "")
63+
}));
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
module.exports = {
71+
linter: reporter,
72+
fixer: reporter
73+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import TextLintTester from "textlint-tester";
4+
import rule from "../src/textlint-rule-ja-space-around-code";
5+
var tester = new TextLintTester();
6+
tester.run("InlineCode周りのスペース", rule, {
7+
valid: [
8+
9+
{
10+
text: "`code` と日本語の間はスペースを空ける",
11+
options: {
12+
before: true,
13+
after: true
14+
},
15+
},
16+
{
17+
text: "`code`と日本語の間はスペースを空けない",
18+
options: {
19+
before: false,
20+
after: false
21+
},
22+
},
23+
{
24+
text: "`code` is good in english text.",
25+
options: {
26+
before: false,
27+
after: false
28+
},
29+
}
30+
],
31+
invalid: [
32+
// before only
33+
{
34+
text: "これは `code` おかしい",
35+
output: "これは `code`おかしい",
36+
options: {
37+
before: true,
38+
after: false
39+
},
40+
errors: [
41+
{
42+
message: "インラインコードの後にスペースを入れません。",
43+
column: 12
44+
}
45+
]
46+
},
47+
// after only
48+
{
49+
text: "これは `code` おかしい",
50+
output: "これは`code` おかしい",
51+
options: {
52+
before: false,
53+
after: true
54+
},
55+
errors: [
56+
{
57+
message: "インラインコードの前にスペースを入れません。",
58+
column: 4
59+
}
60+
]
61+
},
62+
{
63+
text: "これは `code` おかしい",
64+
output: "これは`code`おかしい",
65+
options: {
66+
before: false,
67+
after: false
68+
},
69+
errors: [
70+
{
71+
message: "インラインコードの前にスペースを入れません。",
72+
column: 4
73+
},
74+
{
75+
message: "インラインコードの後にスペースを入れません。",
76+
column: 12
77+
}
78+
]
79+
},
80+
{
81+
text: "これは`code`おかしい",
82+
output: "これは `code` おかしい",
83+
options: {
84+
before: true,
85+
after: true
86+
},
87+
errors: [
88+
{
89+
message: "インラインコードの前にスペースを入れてください。",
90+
column: 3
91+
},
92+
{
93+
message: "インラインコードの後にスペースを入れてください。",
94+
column: 10
95+
}
96+
]
97+
}
98+
]
99+
});

0 commit comments

Comments
 (0)