Skip to content

Commit a0e8e5d

Browse files
committed
feat(rule): add checkCode option
1 parent 2cc42f8 commit a0e8e5d

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ textlint --rule @textlint-rule/no-invalid-control-character README.md
4646

4747
- `allow`: `string[]`
4848
- Define allow control characters
49+
- `checkCode`: `boolean`
50+
- Default: `false`
51+
- Check code if it is `true`
4952

5053
```json
5154
{

src/textlint-rule-no-invalid-control-character.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,46 @@ const getName = char => {
2525

2626
const DEFAULT_OPTION = {
2727
// Define allow char code like `\u0019`
28-
allow: []
28+
allow: [],
29+
// Check code if it is true
30+
checkCode: false
2931
};
30-
/**
31-
* @param {TextlintRuleContext} context
32-
* @param {{
33-
* allow?:string[]
34-
* }} options
35-
* @returns {TextlintRuleCreator}
36-
*/
32+
3733
const reporter = (context, options = {}) => {
3834
const { Syntax, RuleError, getSource, fixer, report } = context;
3935
const allow = options.allow || DEFAULT_OPTION.allow;
36+
const checkCode = options.checkCode !== undefined ? options.checkCode : DEFAULT_OPTION.checkCode;
37+
const checkNode = node => {
38+
const text = getSource(node);
39+
// Ignore \r \n \t
40+
const controlCharacterPattern = /([\x00-\x08\x0B\x0C\x0E-\x1F\x7F])/g;
41+
/**
42+
* @type {Array<{match:string, sub:string[], index:number}>}
43+
*/
44+
const results = execall(controlCharacterPattern, text);
45+
results.forEach(result => {
46+
const index = result.index;
47+
const char = result.sub[0];
48+
// if allow the `char`, ignore it
49+
if (allow.some(allowChar => allowChar === char)) {
50+
return;
51+
}
52+
const name = getName(char);
53+
const ruleError = new RuleError(`Found invalid control character(${name} ${unicodeEscape(char)})`, {
54+
index: index,
55+
fix: fixer.removeRange([index, index + 1])
56+
});
57+
report(node, ruleError);
58+
});
59+
};
4060
return {
4161
[Syntax.Str](node) {
42-
const text = getSource(node);
43-
// Ignore \r \n \t
44-
const controlCharacterPattern = /([\x00-\x08\x0B\x0C\x0E-\x1F\x7F])/g;
45-
/**
46-
* @type {Array<{match:string, sub:string[], index:number}>}
47-
*/
48-
const results = execall(controlCharacterPattern, text);
49-
results.forEach(result => {
50-
const index = result.index;
51-
const char = result.sub[0];
52-
// if allow the `char`, ignore it
53-
if (allow.some(allowChar => allowChar === char)) {
54-
return;
55-
}
56-
const name = getName(char);
57-
const ruleError = new RuleError(`Found invalid control character(${name} ${unicodeEscape(char)})`, {
58-
index: index,
59-
fix: fixer.removeRange([index, index + 1])
60-
});
61-
report(node, ruleError);
62-
});
62+
checkNode(node);
63+
},
64+
[Syntax.Code](node) {
65+
if (checkCode) {
66+
checkNode(node);
67+
}
6368
}
6469
};
6570
};

test/textlint-rule-no-invalid-control-character-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ tester.run("textlint-rule-no-invalid-control-character", rule, {
4646
}
4747
]
4848
},
49+
{
50+
text: `\`var value = "\u0008"\``,
51+
options: {
52+
checkCode: true
53+
},
54+
errors: [
55+
{
56+
message: "Found invalid control character(BACKSPACE \\u0008)",
57+
index: 14
58+
}
59+
]
60+
},
4961
...invalid
5062
]
5163
});

0 commit comments

Comments
 (0)