Skip to content

Commit a518031

Browse files
keikazu
authored andcommitted
feat(rule): add ja-no-space-between-full-and-full-width (#2)
1 parent 0338d8e commit a518031

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# textlint-rule-ja-no-space-between-full-and-full-width
2+
3+
全角文字同士の間のスペースについてのtextlintルール
4+
5+
## Install
6+
7+
Install with [npm](https://www.npmjs.com/):
8+
9+
npm install textlint-rule-ja-no-space-between-full-and-full-width
10+
11+
## Usage
12+
13+
Via `.textlintrc`(Recommended)
14+
15+
```json
16+
{
17+
"rules": {
18+
"ja-no-space-between-full-and-full-width": true
19+
}
20+
}
21+
```
22+
23+
Via CLI
24+
25+
```
26+
textlint --rule ja-no-space-between-full-and-full-width README.md
27+
```
28+
29+
30+
## Changelog
31+
32+
See [Releases page](https://github.com/extlint-ja/textlint-rule-spacing/releases).
33+
34+
## Running tests
35+
36+
Install devDependencies and Run `npm test`:
37+
38+
npm i -d && npm test
39+
40+
## Contributing
41+
42+
Pull requests and stars are always welcome.
43+
44+
For bugs and feature requests, [please create an issue](https://github.com/extlint-ja/textlint-rule-spacing/issues).
45+
46+
1. Fork it!
47+
2. Create your feature branch: `git checkout -b my-new-feature`
48+
3. Commit your changes: `git commit -am 'Add some feature'`
49+
4. Push to the branch: `git push origin my-new-feature`
50+
5. Submit a pull request :D
51+
52+
## Author
53+
54+
- [github/azu](https://github.com/azu)
55+
- [twitter/azu_re](https://twitter.com/azu_re)
56+
57+
## License
58+
59+
MIT © azu
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "textlint-rule-ja-no-space-between-full-and-full-width",
3+
"version": "1.0.0",
4+
"description": "全角文字同士の間のスペースについてのtextlintルール",
5+
"main": "lib/index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/textlint-ja/textlint-rule-spacing.git"
9+
},
10+
"author": "azu",
11+
"email": "azuciao@gmail.com",
12+
"homepage": "https://github.com/extlint-ja/textlint-rule-spacing",
13+
"license": "MIT",
14+
"bugs": {
15+
"url": "https://github.com/extlint-ja/textlint-rule-spacing/issues"
16+
},
17+
"scripts": {
18+
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
19+
"watch": "babel src --out-dir lib --watch --source-maps",
20+
"prepublish": "npm run --if-present build",
21+
"test": "mocha"
22+
},
23+
"keywords": [
24+
"textlint"
25+
],
26+
"devDependencies": {
27+
"babel-cli": "^6.0.0",
28+
"babel-preset-es2015": "^6.0.0",
29+
"textlint-tester": "^2.0.0",
30+
"mocha": "^2.3.3"
31+
},
32+
"dependencies": {
33+
"match-index": "^1.0.1",
34+
"regx": "^1.0.4",
35+
"textlint-rule-helper": "^2.0.0"
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
/*
4+
全角文字と半角文字の間
5+
原則として、全角文字と半角文字の間にスペースを入れません。
6+
ただしカタカナ複合語の場合を除きます。
7+
*/
8+
import {RuleHelper} from "textlint-rule-helper";
9+
import {matchAll} from "match-index";
10+
import regx from "regx";
11+
const rx = regx("g");
12+
const japaneseRegExp = /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[--])/;
13+
14+
function reporter(context) {
15+
const {Syntax, RuleError, report, fixer, getSource} = context;
16+
const helper = new RuleHelper();
17+
return {
18+
[Syntax.Str](node){
19+
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
20+
return;
21+
}
22+
const text = getSource(node);
23+
// 全角同士の間は半角スペースを入れない
24+
const matchReg = rx`${japaneseRegExp}( )${japaneseRegExp}`;
25+
const katakakana = /[-]( )[-]/;
26+
matchAll(text, matchReg).forEach(match => {
27+
const {input, captureGroups} = match;
28+
// ただしカタカナ複合語の場合を除きます。
29+
if (katakakana.test(input)) {
30+
return;
31+
}
32+
captureGroups.forEach(captureGroup => {
33+
const index = captureGroup.index;
34+
report(node, new RuleError("原則として、全角文字どうしの間にスペースを入れません。", {
35+
index: index,
36+
fix: fixer.replaceTextRange([index, index + 1], "")
37+
}));
38+
});
39+
});
40+
}
41+
};
42+
}
43+
module.exports = {
44+
linter: reporter,
45+
fixer: reporter
46+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
"これは正解",
9+
"This is 大丈夫",
10+
"This is a pen.",
11+
"ユーザー インターフェース"//カタカナは例外
12+
],
13+
invalid: [
14+
{
15+
text: "これは ダメ",
16+
output: "これはダメ",
17+
errors: [
18+
{
19+
message: "原則として、全角文字どうしの間にスペースを入れません。",
20+
column: 4
21+
}
22+
]
23+
},
24+
{
25+
text: "これは どういうこと?",
26+
output: "これはどういうこと?",
27+
errors: [
28+
{
29+
message: "原則として、全角文字どうしの間にスペースを入れません。",
30+
column: 4
31+
}
32+
]
33+
}
34+
]
35+
});

0 commit comments

Comments
 (0)