Skip to content
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

refactor(rule): analyze-desumasu-dearu 3.0.0へアップデート #5

Merged
merged 3 commits into from
May 5, 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
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
"es2015"
],
"env": {
"development": {
"presets": [
"power-assert"
]
}
}
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"url": "https://github.com/azu/textlint-rule-no-mix-dearu-desumasu/issues"
},
"version": "1.4.0",
"description": "textlint rule that no mix である and ですます.",
"description": "textlint rule that no mixed である and ですます.",
"main": "lib/no-mix-dearu-desumasu.js",
"files": [
"lib",
Expand All @@ -33,14 +33,16 @@
"japanese"
],
"devDependencies": {
"babel": "^5.8.23",
"espower-babel": "^3.3.0",
"mocha": "^2.3.0",
"power-assert": "^1.0.0",
"textlint-tester": "^0.2.0"
"babel-cli": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-power-assert": "^1.0.0",
"babel-register": "^6.8.0",
"mocha": "^2.4.5",
"power-assert": "^1.3.1",
"textlint-tester": "^1.2.0"
},
"dependencies": {
"analyze-desumasu-dearu": "^2.1.3",
"analyze-desumasu-dearu": "^3.0.0",
"textlint-rule-helper": "^1.1.3"
}
}
141 changes: 77 additions & 64 deletions src/no-mix-dearu-desumasu.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,101 @@
// LICENSE : MIT
"use strict";
import {RuleHelper} from "textlint-rule-helper";
import {analyzeDesumasu,analyzeDearu} from "analyze-desumasu-dearu";
export default function noMixDearuDesumasu(context) {
let {Syntax, RuleError, report, getSource} = context;
let helper = new RuleHelper(context);
let dearuCount;
let desumasuCount;

let dearuLastHit;
let desumasuLastHit;

function initialize() {
dearuCount = 0;
desumasuCount = 0;
import {analyze, isDearu, isDesumasu} from "analyze-desumasu-dearu";
export class MixedChecker {
/**
* @param context
*/
constructor(context) {
this.context = context;
this.dearuCount = 0;
this.desumasuCount = 0;
this.dearuLastHit = null;
this.desumasuLastHit = null;
this._queue = Promise.resolve();
}

dearuLastHit = null;
desumasuLastHit = null;
check(node, text) {
this._queue = this._queue.then(() => {
return analyze(text).then(results => {
const retDearu = results.filter(isDearu);
const retDesumasu = results.filter(isDesumasu);
const dearuCount = this.dearuCount + retDearu.length;
const desumasuCount = this.desumasuCount + retDesumasu.length;
if (this.dearuCount !== dearuCount) {
this.dearuCount = dearuCount;
this.dearuLastHit = {
node,
matches: retDearu
};
}
if (this.desumasuCount !== desumasuCount) {
this.desumasuCount = desumasuCount;
this.desumasuLastHit = {
node,
matches: retDesumasu
};
}
});
});
}

return {
[Syntax.Document]: initialize,
[Syntax.Str](node){
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
// 例外) 「です・ます」調の文中の「箇条書き」の部分に「である」調を使う場合
// http://www.p-press.jp/correct/mailmagazine/mailmagazine24.html
if (helper.isChildNode(node, [Syntax.ListItem])) {
return;
}
let beforeDearuCount = dearuCount;
let beforeDesumasuCount = desumasuCount;
let text = getSource(node);
let retDearu = analyzeDearu(text, {analyzeConjunction: false});
let retDesumasu = analyzeDesumasu(text, {analyzeConjunction: false});
dearuCount += retDearu.length;
desumasuCount += retDesumasu.length;
if (beforeDearuCount !== dearuCount) {
dearuLastHit = {
node,
matches: retDearu
};
}
if (beforeDesumasuCount !== desumasuCount) {
desumasuLastHit = {
node,
matches: retDesumasu
};
}
},
[Syntax.Document + ":exit"](node){
if (dearuCount === 0 || desumasuCount === 0) {
checkout() {
return this._queue.then(() => {
if (this.dearuCount === 0 || this.desumasuCount === 0) {
// No problem
return;
}
if (dearuCount > desumasuCount) {
const RuleError = this.context.RuleError;
const report = this.context.report;
if (this.dearuCount > this.desumasuCount) {
// である優先 => 最後の"ですます"を表示
let hitNode = desumasuLastHit.matches[0];
let ruleError = new RuleError(`"である"調 と "ですます"調 が混在
const hitNode = this.desumasuLastHit.matches[0];
const ruleError = new RuleError(`"である"調 と "ですます"調 が混在
=> "${hitNode.value}" がですます調
Total:
である : ${dearuCount}
ですます: ${desumasuCount}
である : ${this.dearuCount}
ですます: ${this.desumasuCount}
`, {
line: hitNode.lineNumber - 1,
column: hitNode.columnIndex
index: hitNode.index
});
report(desumasuLastHit.node, ruleError)
} else if (dearuLastHit.matches) {
report(this.desumasuLastHit.node, ruleError)
} else if (this.dearuLastHit.matches) {
// ですます優先 => 最後の"である"を表示
let hitNode = dearuLastHit.matches[0];
let ruleError = new RuleError(`"である"調 と "ですます"調 が混在
const hitNode = this.dearuLastHit.matches[0];
const ruleError = new RuleError(`"である"調 と "ですます"調 が混在
=> "${hitNode.value}" がである調
Total:
である : ${dearuCount}
ですます: ${desumasuCount}
である : ${this.dearuCount}
ですます: ${this.desumasuCount}
`, {
line: hitNode.lineNumber - 1,
column: hitNode.columnIndex
index: hitNode.index
});

report(dearuLastHit.node, ruleError);
report(this.dearuLastHit.node, ruleError);
}
});
}
}
export default function noMixedDearuDesumasu(context) {
const {Syntax, getSource} = context;
const helper = new RuleHelper(context);
const strChecker = new MixedChecker(context);
return {
[Syntax.Str](node){
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
// 例外) 「です・ます」調の文中の「箇条書き」の部分に「である」調を使う場合
// http://www.p-press.jp/correct/mailmagazine/mailmagazine24.html
if (helper.isChildNode(node, [Syntax.ListItem])) {
return;
}
const text = getSource(node);
strChecker.check(node, text);
},
[Syntax.Document + ":exit"](){
return strChecker.checkout();
}
}
}
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--compilers js:espower-babel/guess
--compilers js:babel-register