Skip to content

Commit 19cd32d

Browse files
keikazu
authored andcommitted
feat(rule): add no-space-around-parentheses (#4)
1 parent 456172a commit 19cd32d

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-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-around-parentheses
2+
3+
かっこ類と隣接する文字のスペースについてのtextlintルール
4+
5+
## Install
6+
7+
Install with [npm](https://www.npmjs.com/):
8+
9+
npm install textlint-rule-ja-no-space-around-parentheses
10+
11+
## Usage
12+
13+
Via `.textlintrc`(Recommended)
14+
15+
```json
16+
{
17+
"rules": {
18+
"ja-no-space-around-parentheses": true
19+
}
20+
}
21+
```
22+
23+
Via CLI
24+
25+
```
26+
textlint --rule ja-no-space-around-parentheses 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "textlint-rule-ja-no-space-around-parentheses",
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+
"textlint-rule-helper": "^2.0.0"
35+
}
36+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
/*
4+
かっこ類と隣接する文字の間のスペースの有無
5+
かっこの外側、内側ともにスペースを入れません。
6+
*/
7+
import {RuleHelper} from "textlint-rule-helper";
8+
import {matchCaptureGroupAll} from "match-index";
9+
10+
const brackets = [
11+
"\\[", "\\]", "(", ")", "[", "]", "「", "」", "『", "』"
12+
];
13+
14+
const leftBrackets = brackets.map(bracket => {
15+
return new RegExp("\([  ]\)" + bracket, "g");
16+
});
17+
const rightBrackets = brackets.map(bracket => {
18+
return new RegExp(bracket + "\([  ])", "g");
19+
});
20+
function reporter(context) {
21+
const {Syntax, RuleError, report, fixer, getSource} = context;
22+
const helper = new RuleHelper();
23+
return {
24+
[Syntax.Str](node){
25+
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
26+
return;
27+
}
28+
const text = getSource(node);
29+
// 左にスペース
30+
leftBrackets.forEach(pattern => {
31+
matchCaptureGroupAll(text, pattern).forEach(match => {
32+
const {index} = match;
33+
report(node, new RuleError("かっこの外側、内側ともにスペースを入れません。", {
34+
index: index,
35+
fix: fixer.replaceTextRange([index, index + 1], "")
36+
}));
37+
});
38+
});
39+
// 右にスペース
40+
rightBrackets.forEach(pattern => {
41+
matchCaptureGroupAll(text, pattern).forEach(match => {
42+
const {index, text} = match;
43+
report(node, new RuleError("かっこの外側、内側ともにスペースを入れません。", {
44+
index: index,
45+
fix: fixer.replaceTextRange([index, index + 1], "")
46+
}));
47+
});
48+
});
49+
}
50+
};
51+
}
52+
export default {
53+
linter: reporter,
54+
fixer: reporter
55+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"テスト[文章]です",
10+
`
11+
実装をみてもらうと分かりますが、JavaScriptの\`prototype\`の仕組みをそのまま利用しています。
12+
そのため、特別な実装は必要なく
13+
「拡張する時は\`calculator.prototype\`の代わりに\`calculator.fn\`を拡張してください」
14+
というルールがあるだけとも言えます。
15+
`
16+
],
17+
invalid: [
18+
{
19+
text: "「 ダメ」",
20+
output: "「ダメ」",
21+
errors: [
22+
{message: "かっこの外側、内側ともにスペースを入れません。"}
23+
]
24+
},
25+
{
26+
text: "これは 「ダメ」です",
27+
output: "これは「ダメ」です",
28+
errors: [
29+
{
30+
message: "かっこの外側、内側ともにスペースを入れません。",
31+
line: 1,
32+
column: 4
33+
}
34+
]
35+
},{
36+
text: `TEST
37+
38+
- TODO
39+
40+
これは 「ダメ」です
41+
`,
42+
output: `TEST
43+
44+
- TODO
45+
46+
これは「ダメ」です
47+
`,
48+
errors: [
49+
{
50+
message: "かっこの外側、内側ともにスペースを入れません。",
51+
line: 5,
52+
column: 4
53+
}
54+
]
55+
}
56+
]
57+
});

0 commit comments

Comments
 (0)