Skip to content

Commit 1581f12

Browse files
authored
feat(rule): add textlint-rule-ja-space-between-half-and-full-width (#10)
* feat(rule): add textlint-rule-ja-space-between-half-and-full-width close #8 * chore(preset): use never option * feat(rule): ignore text in header close #7
1 parent cda8a49 commit 1581f12

File tree

7 files changed

+333
-3
lines changed

7 files changed

+333
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"es2015"
4+
]
5+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# textlint-rule-ja-space-between-half-and-full-width
2+
3+
半角文字と全角文字のスペースについてのtextlintルール
4+
5+
半角文字と全角文字にスペースを入れるかどうかを指定できます。
6+
7+
デフォルトでは、半角文字と全角文字の間にスペースを入れません。(`"never"`)
8+
9+
OK: これはUnicode
10+
NG: これは Unicode
11+
12+
## Install
13+
14+
Install with [npm](https://www.npmjs.com/):
15+
16+
npm install textlint-rule-ja-space-between-half-and-full-width
17+
18+
## Usage
19+
20+
Via `.textlintrc`(Recommended)
21+
22+
```json
23+
{
24+
"rules": {
25+
"ja-space-between-half-and-full-width": {
26+
"space": "never"
27+
}
28+
}
29+
}
30+
```
31+
32+
Via CLI
33+
34+
```
35+
textlint --rule ja-space-between-half-and-full-width README.md
36+
```
37+
38+
39+
## Options
40+
41+
- `space`: `"always"` || `"never"`
42+
- デフォルト: `"never"`
43+
- スペースを常に 入れる(`"always"`) or 入れない(`"never"`)
44+
45+
```json
46+
{
47+
"rules": {
48+
"ja-space-between-half-and-full-width": {
49+
"space": "always"
50+
}
51+
}
52+
}
53+
```
54+
55+
## Changelog
56+
57+
See [Releases page](https://github.com/textlint-ja/textlint-rule-spacing/releases).
58+
59+
## Running tests
60+
61+
Install devDependencies and Run `npm test`:
62+
63+
npm i -d && npm test
64+
65+
## Contributing
66+
67+
Pull requests and stars are always welcome.
68+
69+
For bugs and feature requests, [please create an issue](https://github.com/textlint-ja/textlint-rule-spacing/issues).
70+
71+
1. Fork it!
72+
2. Create your feature branch: `git checkout -b my-new-feature`
73+
3. Commit your changes: `git commit -am 'Add some feature'`
74+
4. Push to the branch: `git push origin my-new-feature`
75+
5. Submit a pull request :D
76+
77+
## Author
78+
79+
- [github/azu](https://github.com/azu)
80+
- [twitter/azu_re](https://twitter.com/azu_re)
81+
82+
## License
83+
84+
MIT © azu
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "textlint-rule-ja-space-between-half-and-full-width",
3+
"version": "1.0.2",
4+
"description": "半角文字と全角文字のスペースについてのtextlintルール",
5+
"main": "lib/index.js",
6+
"files": [
7+
"src/",
8+
"lib/"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/textlint-ja/textlint-rule-spacing.git"
13+
},
14+
"author": "azu",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/textlint-ja/textlint-rule-spacing",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/textlint-ja/textlint-rule-spacing/issues"
20+
},
21+
"scripts": {
22+
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
23+
"watch": "babel src --out-dir lib --watch --source-maps",
24+
"prepublish": "npm run --if-present build",
25+
"test": "mocha"
26+
},
27+
"keywords": [
28+
"textlint"
29+
],
30+
"devDependencies": {
31+
"babel-cli": "^6.0.0",
32+
"babel-preset-es2015": "^6.0.0",
33+
"textlint-tester": "^2.0.0",
34+
"mocha": "^2.3.3"
35+
},
36+
"dependencies": {
37+
"match-index": "^1.0.1",
38+
"textlint-rule-helper": "^2.0.0"
39+
}
40+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
const assert = require("assert");
4+
/*
5+
全角文字と半角文字の間にスペースを入れるかどうか
6+
*/
7+
import {RuleHelper} from "textlint-rule-helper";
8+
import {matchCaptureGroupAll} from "match-index";
9+
const defaultOptions = {
10+
// スペースを入れるかどうか
11+
// "never" or "always"
12+
"space": "never"
13+
};
14+
function reporter(context, options = {}) {
15+
const {Syntax, RuleError, report, fixer, getSource} = context;
16+
const helper = new RuleHelper();
17+
const spaceOption = options.space || defaultOptions.space;
18+
assert(spaceOption === "always" || spaceOption === "never", `"space" options should be "always" or "never".`);
19+
// アルファベットと全角の間はスペースを入れない
20+
const noSpaceBetween = (node, text) => {
21+
const betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/);
22+
const betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])([  ])[A-Za-z0-9]/);
23+
const reportMatch = (match) => {
24+
const {index} = match;
25+
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
26+
index: match.index,
27+
fix: fixer.replaceTextRange([index, index + 1], "")
28+
}));
29+
};
30+
betweenHanAndZen.forEach(reportMatch);
31+
betweenZenAndHan.forEach(reportMatch);
32+
};
33+
34+
// アルファベットと全角の間はスペースを入れる
35+
const needSpaceBetween = (node, text) => {
36+
const betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([^  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/);
37+
const betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])([^  ])[A-Za-z0-9]/);
38+
const reportMatch = (match) => {
39+
const {index} = match;
40+
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れます。", {
41+
index: match.index,
42+
fix: fixer.replaceTextRange([index + 1, index + 1], " ")
43+
}));
44+
};
45+
betweenHanAndZen.forEach(reportMatch);
46+
betweenZenAndHan.forEach(reportMatch);
47+
};
48+
return {
49+
[Syntax.Str](node){
50+
if (helper.isChildNode(node, [
51+
Syntax.Header, Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis
52+
])) {
53+
return;
54+
}
55+
const text = getSource(node);
56+
57+
if (spaceOption === "always") {
58+
needSpaceBetween(node, text)
59+
} else if (spaceOption === "never") {
60+
noSpaceBetween(node, text);
61+
}
62+
63+
}
64+
}
65+
}
66+
module.exports = {
67+
linter: reporter,
68+
fixer: reporter
69+
};
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import TextLintTester from "textlint-tester";
4+
import rule from "../src/index";
5+
var tester = new TextLintTester();
6+
tester.run("全角文字と半角文字の間", rule, {
7+
valid: [
8+
// never
9+
"JTF標準",
10+
{
11+
text: "JTF標準",
12+
options: {
13+
space: "never"
14+
},
15+
},
16+
"This is a pen.",
17+
"1. `./*.*`にマッチするファイルを取得 = Readable Stream",
18+
`[CONTRIBUTING.md](./CONTRIBUTING.md)に、書籍で扱うべきプラグインアーキテクチャのProposalの書き方や
19+
Pull Request、コミットのやりかたなどが書かれています。`,
20+
// need space
21+
{
22+
text: "JTF 標準",
23+
options: {
24+
space: "always"
25+
}
26+
},
27+
{
28+
text: "This is a pen",
29+
options: {
30+
space: "always"
31+
}
32+
},
33+
{
34+
text: "日本語と english の間に半角スペースを入れる",
35+
options: {
36+
space: "always"
37+
}
38+
},
39+
// ignore
40+
{
41+
text: "# JTF 標準",
42+
options: {
43+
space: "never"
44+
},
45+
},
46+
],
47+
invalid: [
48+
{
49+
text: "JTF 標準",
50+
output: "JTF標準",
51+
errors: [
52+
{
53+
message: "原則として、全角文字と半角文字の間にスペースを入れません。",
54+
column: 4
55+
}
56+
]
57+
},
58+
{
59+
text: "JTF 標準",
60+
output: "JTF標準",
61+
options: {
62+
space: "never"
63+
},
64+
errors: [
65+
{
66+
message: "原則として、全角文字と半角文字の間にスペースを入れません。",
67+
column: 4
68+
}
69+
]
70+
},
71+
{
72+
text: "これは Unicode",
73+
output: "これはUnicode",
74+
errors: [
75+
{message: "原則として、全角文字と半角文字の間にスペースを入れません。"}
76+
]
77+
},
78+
{
79+
text: "これは Unicode",
80+
output: "これはUnicode",
81+
errors: [
82+
{message: "原則として、全角文字と半角文字の間にスペースを入れません。"}
83+
]
84+
},
85+
// need space
86+
{
87+
text: "JTF標準",
88+
output: "JTF 標準",
89+
options: {
90+
space: "always"
91+
},
92+
errors: [
93+
{
94+
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
95+
column: 3
96+
}
97+
]
98+
},
99+
{
100+
text: "これはUnicode",
101+
output: "これは Unicode",
102+
options: {
103+
space: "always"
104+
},
105+
errors: [
106+
{
107+
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
108+
column: 3
109+
}
110+
]
111+
},
112+
{
113+
text: "日本語とenglishの間に半角スペースを入れる",
114+
output: "日本語と english の間に半角スペースを入れる",
115+
options: {
116+
space: "always"
117+
},
118+
errors: [
119+
{
120+
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
121+
column: 4
122+
},
123+
{
124+
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
125+
column: 11
126+
}
127+
]
128+
},
129+
]
130+
});

packages/textlint-rule-preset-ja-spacing/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"textlint-rule-ja-nakaguro-or-halfwidth-space-between-katakana": "^1.0.2",
3838
"textlint-rule-ja-no-space-around-parentheses": "^1.0.2",
3939
"textlint-rule-ja-no-space-between-full-width": "^1.0.2",
40-
"textlint-rule-ja-no-space-between-half-and-full-width": "^1.0.2",
40+
"textlint-rule-ja-space-between-half-and-full-width": "^1.0.2",
4141
"textlint-rule-ja-space-after-exclamation": "^1.0.2",
4242
"textlint-rule-ja-space-after-question": "^1.0.2",
4343
"textlint-rule-ja-space-around-code": "^1.0.2"

packages/textlint-rule-preset-ja-spacing/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
"ja-nakaguro-or-halfwidth-space-between-katakana": require("textlint-rule-ja-nakaguro-or-halfwidth-space-between-katakana"),
66
"ja-no-space-around-parentheses": require("textlint-rule-ja-no-space-around-parentheses"),
77
"ja-no-space-between-full-width": require("textlint-rule-ja-no-space-between-full-width"),
8-
"ja-no-space-between-half-and-full-width": require("textlint-rule-ja-no-space-between-half-and-full-width"),
8+
"ja-space-between-half-and-full-width": require("textlint-rule-ja-space-between-half-and-full-width"),
99
"ja-space-after-exclamation": require("textlint-rule-ja-space-after-exclamation"),
1010
"ja-space-after-question": require("textlint-rule-ja-space-after-question"),
1111
"ja-space-around-code": require("textlint-rule-ja-space-around-code"),
@@ -14,7 +14,9 @@ module.exports = {
1414
"ja-nakaguro-or-halfwidth-space-between-katakana": true,
1515
"ja-no-space-around-parentheses": true,
1616
"ja-no-space-between-full-width": true,
17-
"ja-no-space-between-half-and-full-width": true,
17+
"ja-space-between-half-and-full-width": {
18+
"space": "never"
19+
},
1820
"ja-space-after-exclamation": true,
1921
"ja-space-after-question": true,
2022
"ja-space-around-code": false,

0 commit comments

Comments
 (0)