@@ -25,41 +25,46 @@ const getName = char => {
25
25
26
26
const DEFAULT_OPTION = {
27
27
// Define allow char code like `\u0019`
28
- allow : [ ]
28
+ allow : [ ] ,
29
+ // Check code if it is true
30
+ checkCode : false
29
31
} ;
30
- /**
31
- * @param {TextlintRuleContext } context
32
- * @param {{
33
- * allow?:string[]
34
- * }} options
35
- * @returns {TextlintRuleCreator }
36
- */
32
+
37
33
const reporter = ( context , options = { } ) => {
38
34
const { Syntax, RuleError, getSource, fixer, report } = context ;
39
35
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
+ } ;
40
60
return {
41
61
[ 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
+ }
63
68
}
64
69
} ;
65
70
} ;
0 commit comments