Skip to content

feat(rule): add textlint-rule-ja-space-between-half-and-full-width #10

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

Merged
merged 3 commits into from
Jul 17, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# textlint-rule-ja-space-between-half-and-full-width

半角文字と全角文字のスペースについてのtextlintルール

半角文字と全角文字にスペースを入れるかどうかを指定できます。

デフォルトでは、半角文字と全角文字の間にスペースを入れません。(`"never"`)

OK: これはUnicode
NG: これは Unicode

## Install

Install with [npm](https://www.npmjs.com/):

npm install textlint-rule-ja-space-between-half-and-full-width

## Usage

Via `.textlintrc`(Recommended)

```json
{
"rules": {
"ja-space-between-half-and-full-width": {
"space": "never"
}
}
}
```

Via CLI

```
textlint --rule ja-space-between-half-and-full-width README.md
```


## Options

- `space`: `"always"` || `"never"`
- デフォルト: `"never"`
- スペースを常に 入れる(`"always"`) or 入れない(`"never"`)

```json
{
"rules": {
"ja-space-between-half-and-full-width": {
"space": "always"
}
}
}
```

## Changelog

See [Releases page](https://github.com/textlint-ja/textlint-rule-spacing/releases).

## Running tests

Install devDependencies and Run `npm test`:

npm i -d && npm test

## Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, [please create an issue](https://github.com/textlint-ja/textlint-rule-spacing/issues).

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## Author

- [github/azu](https://github.com/azu)
- [twitter/azu_re](https://twitter.com/azu_re)

## License

MIT © azu
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "textlint-rule-ja-space-between-half-and-full-width",
"version": "1.0.2",
"description": "半角文字と全角文字のスペースについてのtextlintルール",
"main": "lib/index.js",
"files": [
"src/",
"lib/"
],
"repository": {
"type": "git",
"url": "git+https://github.com/textlint-ja/textlint-rule-spacing.git"
},
"author": "azu",
"email": "[email protected]",
"homepage": "https://github.com/textlint-ja/textlint-rule-spacing",
"license": "MIT",
"bugs": {
"url": "https://github.com/textlint-ja/textlint-rule-spacing/issues"
},
"scripts": {
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"prepublish": "npm run --if-present build",
"test": "mocha"
},
"keywords": [
"textlint"
],
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-preset-es2015": "^6.0.0",
"textlint-tester": "^2.0.0",
"mocha": "^2.3.3"
},
"dependencies": {
"match-index": "^1.0.1",
"textlint-rule-helper": "^2.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// LICENSE : MIT
"use strict";
const assert = require("assert");
/*
全角文字と半角文字の間にスペースを入れるかどうか
*/
import {RuleHelper} from "textlint-rule-helper";
import {matchCaptureGroupAll} from "match-index";
const defaultOptions = {
// スペースを入れるかどうか
// "never" or "always"
"space": "never"
};
function reporter(context, options = {}) {
const {Syntax, RuleError, report, fixer, getSource} = context;
const helper = new RuleHelper();
const spaceOption = options.space || defaultOptions.space;
assert(spaceOption === "always" || spaceOption === "never", `"space" options should be "always" or "never".`);
// アルファベットと全角の間はスペースを入れない
const noSpaceBetween = (node, text) => {
const betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/);
const betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])([  ])[A-Za-z0-9]/);
const reportMatch = (match) => {
const {index} = match;
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れません。", {
index: match.index,
fix: fixer.replaceTextRange([index, index + 1], "")
}));
};
betweenHanAndZen.forEach(reportMatch);
betweenZenAndHan.forEach(reportMatch);
};

// アルファベットと全角の間はスペースを入れる
const needSpaceBetween = (node, text) => {
const betweenHanAndZen = matchCaptureGroupAll(text, /[A-Za-z0-9]([^  ])(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])/);
const betweenZenAndHan = matchCaptureGroupAll(text, /(?:[\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF]|[\uD840-\uD87F][\uDC00-\uDFFF]|[ぁ-んァ-ヶ])([^  ])[A-Za-z0-9]/);
const reportMatch = (match) => {
const {index} = match;
report(node, new RuleError("原則として、全角文字と半角文字の間にスペースを入れます。", {
index: match.index,
fix: fixer.replaceTextRange([index + 1, index + 1], " ")
}));
};
betweenHanAndZen.forEach(reportMatch);
betweenZenAndHan.forEach(reportMatch);
};
return {
[Syntax.Str](node){
if (helper.isChildNode(node, [
Syntax.Header, Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis
])) {
return;
}
const text = getSource(node);

if (spaceOption === "always") {
needSpaceBetween(node, text)
} else if (spaceOption === "never") {
noSpaceBetween(node, text);
}

}
}
}
module.exports = {
linter: reporter,
fixer: reporter
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// LICENSE : MIT
"use strict";
import TextLintTester from "textlint-tester";
import rule from "../src/index";
var tester = new TextLintTester();
tester.run("全角文字と半角文字の間", rule, {
valid: [
// never
"JTF標準",
{
text: "JTF標準",
options: {
space: "never"
},
},
"This is a pen.",
"1. `./*.*`にマッチするファイルを取得 = Readable Stream",
`[CONTRIBUTING.md](./CONTRIBUTING.md)に、書籍で扱うべきプラグインアーキテクチャのProposalの書き方や
Pull Request、コミットのやりかたなどが書かれています。`,
// need space
{
text: "JTF 標準",
options: {
space: "always"
}
},
{
text: "This is a pen",
options: {
space: "always"
}
},
{
text: "日本語と english の間に半角スペースを入れる",
options: {
space: "always"
}
},
// ignore
{
text: "# JTF 標準",
options: {
space: "never"
},
},
],
invalid: [
{
text: "JTF 標準",
output: "JTF標準",
errors: [
{
message: "原則として、全角文字と半角文字の間にスペースを入れません。",
column: 4
}
]
},
{
text: "JTF 標準",
output: "JTF標準",
options: {
space: "never"
},
errors: [
{
message: "原則として、全角文字と半角文字の間にスペースを入れません。",
column: 4
}
]
},
{
text: "これは Unicode",
output: "これはUnicode",
errors: [
{message: "原則として、全角文字と半角文字の間にスペースを入れません。"}
]
},
{
text: "これは Unicode",
output: "これはUnicode",
errors: [
{message: "原則として、全角文字と半角文字の間にスペースを入れません。"}
]
},
// need space
{
text: "JTF標準",
output: "JTF 標準",
options: {
space: "always"
},
errors: [
{
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
column: 3
}
]
},
{
text: "これはUnicode",
output: "これは Unicode",
options: {
space: "always"
},
errors: [
{
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
column: 3
}
]
},
{
text: "日本語とenglishの間に半角スペースを入れる",
output: "日本語と english の間に半角スペースを入れる",
options: {
space: "always"
},
errors: [
{
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
column: 4
},
{
message: "原則として、全角文字と半角文字の間にスペースを入れます。",
column: 11
}
]
},
]
});
2 changes: 1 addition & 1 deletion packages/textlint-rule-preset-ja-spacing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"textlint-rule-ja-nakaguro-or-halfwidth-space-between-katakana": "^1.0.2",
"textlint-rule-ja-no-space-around-parentheses": "^1.0.2",
"textlint-rule-ja-no-space-between-full-width": "^1.0.2",
"textlint-rule-ja-no-space-between-half-and-full-width": "^1.0.2",
"textlint-rule-ja-space-between-half-and-full-width": "^1.0.2",
"textlint-rule-ja-space-after-exclamation": "^1.0.2",
"textlint-rule-ja-space-after-question": "^1.0.2",
"textlint-rule-ja-space-around-code": "^1.0.2"
Expand Down
6 changes: 4 additions & 2 deletions packages/textlint-rule-preset-ja-spacing/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
"ja-nakaguro-or-halfwidth-space-between-katakana": require("textlint-rule-ja-nakaguro-or-halfwidth-space-between-katakana"),
"ja-no-space-around-parentheses": require("textlint-rule-ja-no-space-around-parentheses"),
"ja-no-space-between-full-width": require("textlint-rule-ja-no-space-between-full-width"),
"ja-no-space-between-half-and-full-width": require("textlint-rule-ja-no-space-between-half-and-full-width"),
"ja-space-between-half-and-full-width": require("textlint-rule-ja-space-between-half-and-full-width"),
"ja-space-after-exclamation": require("textlint-rule-ja-space-after-exclamation"),
"ja-space-after-question": require("textlint-rule-ja-space-after-question"),
"ja-space-around-code": require("textlint-rule-ja-space-around-code"),
Expand All @@ -14,7 +14,9 @@ module.exports = {
"ja-nakaguro-or-halfwidth-space-between-katakana": true,
"ja-no-space-around-parentheses": true,
"ja-no-space-between-full-width": true,
"ja-no-space-between-half-and-full-width": true,
"ja-space-between-half-and-full-width": {
"space": "never"
},
"ja-space-after-exclamation": true,
"ja-space-after-question": true,
"ja-space-around-code": false,
Expand Down