File tree Expand file tree Collapse file tree 9 files changed +253
-6
lines changed
tests/fixtures/type-assertion/assignment Expand file tree Collapse file tree 9 files changed +253
-6
lines changed Original file line number Diff line number Diff line change
1
+ import rule from 'eslint/lib/rules/prefer-const' ;
2
+ import { RuleTester } from '../RuleTester' ;
3
+
4
+ const ruleTester = new RuleTester ( {
5
+ parser : '@typescript-eslint/parser' ,
6
+ } ) ;
7
+
8
+ ruleTester . run ( 'prefer-const' , rule , {
9
+ valid : [
10
+ `
11
+ let x: number | undefined = 1;
12
+ x! += 1;
13
+ ` ,
14
+ `
15
+ let x: number | undefined = 1;
16
+ (<number>x) += 1;
17
+ ` ,
18
+ `
19
+ let x: number | undefined = 1;
20
+ (x as number) += 1;
21
+ ` ,
22
+ ] ,
23
+ invalid : [ ] ,
24
+ } ) ;
Original file line number Diff line number Diff line change @@ -797,7 +797,7 @@ declare module 'eslint/lib/rules/space-infix-ops' {
797
797
'missingSpace' ,
798
798
[
799
799
{
800
- int32Hint : boolean ;
800
+ int32Hint ? : boolean ;
801
801
} ,
802
802
] ,
803
803
{
@@ -812,6 +812,25 @@ declare module 'eslint/lib/rules/space-infix-ops' {
812
812
export = rule ;
813
813
}
814
814
815
+ declare module 'eslint/lib/rules/prefer-const' {
816
+ import { TSESLint , TSESTree } from '@typescript-eslint/experimental-utils' ;
817
+
818
+ const rule : TSESLint . RuleModule <
819
+ 'useConst' ,
820
+ [
821
+ {
822
+ destructuring ?: 'any' | 'all' ;
823
+ ignoreReadBeforeAssign ?: boolean ;
824
+ } ,
825
+ ] ,
826
+ {
827
+ 'Program:exit' ( node : TSESTree . Program ) : void ;
828
+ VariableDeclaration ( node : TSESTree . VariableDeclaration ) : void ;
829
+ }
830
+ > ;
831
+ export = rule ;
832
+ }
833
+
815
834
declare module 'eslint/lib/rules/utils/ast-utils' {
816
835
import { TSESLint , TSESTree } from '@typescript-eslint/experimental-utils' ;
817
836
Original file line number Diff line number Diff line change @@ -386,10 +386,22 @@ class Referencer extends Visitor {
386
386
}
387
387
388
388
protected AssignmentExpression ( node : TSESTree . AssignmentExpression ) : void {
389
- if ( PatternVisitor . isPattern ( node . left ) ) {
389
+ let left = node . left ;
390
+ switch ( left . type ) {
391
+ case AST_NODE_TYPES . TSAsExpression :
392
+ case AST_NODE_TYPES . TSTypeAssertion :
393
+ // explicitly visit the type annotation
394
+ this . visit ( left . typeAnnotation ) ;
395
+ // intentional fallthrough
396
+ case AST_NODE_TYPES . TSNonNullExpression :
397
+ // unwrap the expression
398
+ left = left . expression ;
399
+ }
400
+
401
+ if ( PatternVisitor . isPattern ( left ) ) {
390
402
if ( node . operator === '=' ) {
391
403
this . visitPattern (
392
- node . left ,
404
+ left ,
393
405
( pattern , info ) => {
394
406
const maybeImplicitGlobal = ! this . currentScope ( ) . isStrict
395
407
? {
@@ -413,15 +425,15 @@ class Referencer extends Visitor {
413
425
} ,
414
426
{ processRightHandNodes : true } ,
415
427
) ;
416
- } else if ( node . left . type === AST_NODE_TYPES . Identifier ) {
428
+ } else if ( left . type === AST_NODE_TYPES . Identifier ) {
417
429
this . currentScope ( ) . referenceValue (
418
- node . left ,
430
+ left ,
419
431
ReferenceFlag . ReadWrite ,
420
432
node . right ,
421
433
) ;
422
434
}
423
435
} else {
424
- this . visit ( node . left ) ;
436
+ this . visit ( left ) ;
425
437
}
426
438
this . visit ( node . right ) ;
427
439
}
Original file line number Diff line number Diff line change
1
+ let x : number | undefined = 1 ;
2
+ ( < number > x ) += 1 ;
Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`type-assertion assignment angle-bracket-assignment 1`] = `
4
+ ScopeManager {
5
+ variables: Array [
6
+ ImplicitGlobalConstTypeVariable,
7
+ Variable$2 {
8
+ defs: Array [
9
+ VariableDefinition$1 {
10
+ name: Identifier<"x">,
11
+ node: VariableDeclarator$1,
12
+ },
13
+ ],
14
+ name: "x",
15
+ references: Array [
16
+ Reference$1 {
17
+ identifier: Identifier<"x">,
18
+ init: true,
19
+ isRead: false,
20
+ isTypeReference: false,
21
+ isValueReference: true,
22
+ isWrite: true,
23
+ resolved: Variable$2,
24
+ writeExpr: Literal$2,
25
+ },
26
+ Reference$2 {
27
+ identifier: Identifier<"x">,
28
+ init: false,
29
+ isRead: true,
30
+ isTypeReference: false,
31
+ isValueReference: true,
32
+ isWrite: true,
33
+ resolved: Variable$2,
34
+ writeExpr: Literal$3,
35
+ },
36
+ ],
37
+ isValueVariable: true,
38
+ isTypeVariable: false,
39
+ },
40
+ ],
41
+ scopes: Array [
42
+ GlobalScope$1 {
43
+ block: Program$4,
44
+ isStrict: false,
45
+ references: Array [
46
+ Reference$1,
47
+ Reference$2,
48
+ ],
49
+ set: Map {
50
+ "const" => ImplicitGlobalConstTypeVariable,
51
+ "x" => Variable$2,
52
+ },
53
+ type: "global",
54
+ upper: null,
55
+ variables: Array [
56
+ ImplicitGlobalConstTypeVariable,
57
+ Variable$2,
58
+ ],
59
+ },
60
+ ],
61
+ }
62
+ `;
Original file line number Diff line number Diff line change
1
+ let x : number | undefined = 1 ;
2
+ ( x as number ) += 1 ;
Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`type-assertion assignment as-assignment 1`] = `
4
+ ScopeManager {
5
+ variables: Array [
6
+ ImplicitGlobalConstTypeVariable,
7
+ Variable$2 {
8
+ defs: Array [
9
+ VariableDefinition$1 {
10
+ name: Identifier<"x">,
11
+ node: VariableDeclarator$1,
12
+ },
13
+ ],
14
+ name: "x",
15
+ references: Array [
16
+ Reference$1 {
17
+ identifier: Identifier<"x">,
18
+ init: true,
19
+ isRead: false,
20
+ isTypeReference: false,
21
+ isValueReference: true,
22
+ isWrite: true,
23
+ resolved: Variable$2,
24
+ writeExpr: Literal$2,
25
+ },
26
+ Reference$2 {
27
+ identifier: Identifier<"x">,
28
+ init: false,
29
+ isRead: true,
30
+ isTypeReference: false,
31
+ isValueReference: true,
32
+ isWrite: true,
33
+ resolved: Variable$2,
34
+ writeExpr: Literal$3,
35
+ },
36
+ ],
37
+ isValueVariable: true,
38
+ isTypeVariable: false,
39
+ },
40
+ ],
41
+ scopes: Array [
42
+ GlobalScope$1 {
43
+ block: Program$4,
44
+ isStrict: false,
45
+ references: Array [
46
+ Reference$1,
47
+ Reference$2,
48
+ ],
49
+ set: Map {
50
+ "const" => ImplicitGlobalConstTypeVariable,
51
+ "x" => Variable$2,
52
+ },
53
+ type: "global",
54
+ upper: null,
55
+ variables: Array [
56
+ ImplicitGlobalConstTypeVariable,
57
+ Variable$2,
58
+ ],
59
+ },
60
+ ],
61
+ }
62
+ `;
Original file line number Diff line number Diff line change
1
+ let x : number | undefined = 1 ;
2
+ x ! += 1 ;
Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`type-assertion assignment non-null-assignment 1`] = `
4
+ ScopeManager {
5
+ variables: Array [
6
+ ImplicitGlobalConstTypeVariable,
7
+ Variable$2 {
8
+ defs: Array [
9
+ VariableDefinition$1 {
10
+ name: Identifier<"x">,
11
+ node: VariableDeclarator$1,
12
+ },
13
+ ],
14
+ name: "x",
15
+ references: Array [
16
+ Reference$1 {
17
+ identifier: Identifier<"x">,
18
+ init: true,
19
+ isRead: false,
20
+ isTypeReference: false,
21
+ isValueReference: true,
22
+ isWrite: true,
23
+ resolved: Variable$2,
24
+ writeExpr: Literal$2,
25
+ },
26
+ Reference$2 {
27
+ identifier: Identifier<"x">,
28
+ init: false,
29
+ isRead: true,
30
+ isTypeReference: false,
31
+ isValueReference: true,
32
+ isWrite: true,
33
+ resolved: Variable$2,
34
+ writeExpr: Literal$3,
35
+ },
36
+ ],
37
+ isValueVariable: true,
38
+ isTypeVariable: false,
39
+ },
40
+ ],
41
+ scopes: Array [
42
+ GlobalScope$1 {
43
+ block: Program$4,
44
+ isStrict: false,
45
+ references: Array [
46
+ Reference$1,
47
+ Reference$2,
48
+ ],
49
+ set: Map {
50
+ "const" => ImplicitGlobalConstTypeVariable,
51
+ "x" => Variable$2,
52
+ },
53
+ type: "global",
54
+ upper: null,
55
+ variables: Array [
56
+ ImplicitGlobalConstTypeVariable,
57
+ Variable$2,
58
+ ],
59
+ },
60
+ ],
61
+ }
62
+ `;
You can’t perform that action at this time.
0 commit comments