1
1
/**
2
2
* @license
3
- * Copyright 2017 Palantir Technologies, Inc.
3
+ * Copyright 2019 Palantir Technologies, Inc.
4
4
*
5
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
6
* you may not use this file except in compliance with the License.
@@ -23,8 +23,9 @@ const RESERVED_ENTITY = " ";
23
23
24
24
export class Rule extends Lint . Rules . AbstractRule {
25
25
public static metadata : Lint . IRuleMetadata = {
26
- description : Lint . Utils . dedent
27
- `Warn if ' ' is used in JXS markup. Prefer {" "} over ' '` ,
26
+ description : Lint . Utils . dedent `
27
+ Warn if ' ' is used in JSX markup. Prefer {" "} over ' '
28
+ ` ,
28
29
optionExamples : [ "true" ] ,
29
30
options : null ,
30
31
optionsDescription : "" ,
@@ -40,49 +41,43 @@ export class Rule extends Lint.Rules.AbstractRule {
40
41
}
41
42
}
42
43
43
- function getSpaces ( numOfSpaces : number ) : string {
44
- return " " . repeat ( numOfSpaces ) ;
45
- }
46
-
47
44
function walk ( ctx : Lint . WalkContext < void > ) : void {
48
45
return ts . forEachChild ( ctx . sourceFile , function cb ( node : ts . Node ) : void {
49
46
if ( isJsxText ( node ) ) {
50
- if ( node . getText ( ) . indexOf ( RESERVED_ENTITY ) > - 1 ) {
51
- const text : string = node . getText ( ) ;
52
- const regex : RegExp = new RegExp ( RESERVED_ENTITY , "g" ) ;
47
+ const text = node . getText ( ) ;
48
+ if ( text . indexOf ( RESERVED_ENTITY ) > - 1 ) {
49
+ const regex = new RegExp ( RESERVED_ENTITY , "g" ) ;
53
50
const startIndices : number [ ] = [ ] ;
54
51
const endIndices : number [ ] = [ ] ;
55
- let countEnitiy : number = - 1 ;
56
- let result : RegExpExecArray | null ;
52
+ let countEnitiy = - 1 ;
53
+ let result = regex . exec ( text ) ;
57
54
58
- do {
59
- result = regex . exec ( text ) ;
60
- if ( result !== null ) {
61
- if (
62
- startIndices [ countEnitiy ] !== undefined &&
63
- endIndices [ countEnitiy ] !== undefined &&
64
- startIndices [ countEnitiy ] + endIndices [ countEnitiy ] === result . index
65
- ) {
66
- endIndices [ countEnitiy ] = endIndices [ countEnitiy ] + RESERVED_ENTITY . length ;
67
- } else {
68
- startIndices . push ( result . index ) ;
69
- endIndices . push ( RESERVED_ENTITY . length ) ;
70
- countEnitiy += 1 ;
71
- }
55
+ while ( result !== null ) {
56
+ if (
57
+ startIndices [ countEnitiy ] !== undefined &&
58
+ endIndices [ countEnitiy ] !== undefined &&
59
+ startIndices [ countEnitiy ] + endIndices [ countEnitiy ] === result . index
60
+ ) {
61
+ endIndices [ countEnitiy ] = endIndices [ countEnitiy ] + RESERVED_ENTITY . length ;
62
+ } else {
63
+ startIndices . push ( result . index ) ;
64
+ endIndices . push ( RESERVED_ENTITY . length ) ;
65
+ countEnitiy += 1 ;
72
66
}
73
- } while ( result !== null ) ;
67
+ result = regex . exec ( text ) ;
68
+ }
74
69
75
70
startIndices . forEach ( ( startIndex , index ) => {
76
71
const start = node . getStart ( ) + startIndex ;
77
72
const end = endIndices [ index ] ;
73
+ const spaces = " " . repeat ( end / RESERVED_ENTITY . length ) ;
78
74
const fix = Lint . Replacement . replaceFromTo (
79
75
start ,
80
76
start + end ,
81
- `{"${ getSpaces ( end / RESERVED_ENTITY . length ) } "}` ,
77
+ `{"${ spaces } "}` ,
82
78
) ;
83
79
84
80
ctx . addFailureAt ( start , end , Rule . FAILURE_STRING , fix ) ;
85
-
86
81
} ) ;
87
82
}
88
83
}
0 commit comments