Skip to content

Commit c5d9af3

Browse files
authored
fix: remove unexpected space in output format string (#281)
1 parent 3f8e7b7 commit c5d9af3

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

internal/plugins/typescript/rules/no_unnecessary_type_assertion/no_unnecessary_type_assertion.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,24 @@ var NoUnnecessaryTypeAssertionRule = rule.CreateRule(rule.Rule{
219219
if node.Kind == ast.KindAsExpression {
220220
s := scanner.GetScannerForSourceFile(ctx.SourceFile, expression.End())
221221
asKeywordRange := s.TokenRange()
222+
223+
sourceText := ctx.SourceFile.Text()
224+
startPos := asKeywordRange.Pos()
225+
226+
if startPos > expression.End() && sourceText[startPos-1] == ' ' {
227+
if startPos-1 == expression.End() || (startPos-2 >= 0 && sourceText[startPos-2] != ' ') {
228+
startPos--
229+
}
230+
}
222231

223-
ctx.ReportNodeWithFixes(node, msg, rule.RuleFixRemoveRange(asKeywordRange), rule.RuleFixRemove(ctx.SourceFile, typeNode))
232+
fixRange := asKeywordRange.WithPos(startPos).WithEnd(typeNode.End())
233+
ctx.ReportNodeWithFixes(node, msg, rule.RuleFixRemoveRange(fixRange))
224234
} else {
225235
s := scanner.GetScannerForSourceFile(ctx.SourceFile, node.Pos())
226236
openingAngleBracket := s.TokenRange()
227-
s.ResetPos(typeNode.End())
228-
s.Scan()
229-
closingAngleBracket := s.TokenRange()
230237

231-
ctx.ReportNodeWithFixes(node, msg, rule.RuleFixRemoveRange(openingAngleBracket.WithEnd(closingAngleBracket.End())))
238+
fixRange := openingAngleBracket.WithEnd(expression.Pos())
239+
ctx.ReportNodeWithFixes(node, msg, rule.RuleFixRemoveRange(fixRange))
232240
}
233241
// TODO - add contextually unnecessary check for this
234242
}

internal/plugins/typescript/rules/no_unnecessary_type_assertion/no_unnecessary_type_assertion_test.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ foo!;
405405
}, []rule_tester.InvalidTestCase{
406406
{
407407
Code: "const a = `a` as const;",
408-
Output: []string{"const a = `a` ;"},
408+
Output: []string{"const a = `a`;"},
409409
Errors: []rule_tester.InvalidTestCaseError{
410410
{
411411
MessageId: "unnecessaryAssertion",
@@ -415,7 +415,7 @@ foo!;
415415
},
416416
{
417417
Code: "const a = 'a' as const;",
418-
Output: []string{"const a = 'a' ;"},
418+
Output: []string{"const a = 'a';"},
419419
Errors: []rule_tester.InvalidTestCaseError{
420420
{
421421
MessageId: "unnecessaryAssertion",
@@ -446,7 +446,7 @@ foo!;
446446
},
447447
{
448448
Code: "const foo = 3 as 3;",
449-
Output: []string{"const foo = 3 ;"},
449+
Output: []string{"const foo = 3;"},
450450
Errors: []rule_tester.InvalidTestCaseError{
451451
{
452452
MessageId: "unnecessaryAssertion",
@@ -480,7 +480,7 @@ foo!;
480480
`,
481481
Output: []string{`
482482
type Foo = 3;
483-
const foo = 3 ;
483+
const foo = 3;
484484
`,
485485
},
486486
Errors: []rule_tester.InvalidTestCaseError{
@@ -514,7 +514,7 @@ const bar = foo;
514514
const foo = (3 + 5) as number;
515515
`,
516516
Output: []string{`
517-
const foo = (3 + 5) ;
517+
const foo = (3 + 5);
518518
`,
519519
},
520520
Errors: []rule_tester.InvalidTestCaseError{
@@ -548,7 +548,7 @@ const foo = (3 + 5) as Foo;
548548
`,
549549
Output: []string{`
550550
type Foo = number;
551-
const foo = (3 + 5) ;
551+
const foo = (3 + 5);
552552
`,
553553
},
554554
Errors: []rule_tester.InvalidTestCaseError{
@@ -903,7 +903,7 @@ const a = foo() as number;
903903
`,
904904
Output: []string{`
905905
declare function foo(): number;
906-
const a = foo() ;
906+
const a = foo();
907907
`,
908908
},
909909
Errors: []rule_tester.InvalidTestCaseError{
@@ -940,7 +940,7 @@ declare function foo(): RT;
940940
Output: []string{`
941941
type RT = { log: () => void };
942942
declare function foo(): RT;
943-
(foo() ).log;
943+
(foo()).log;
944944
`,
945945
},
946946
Errors: []rule_tester.InvalidTestCaseError{
@@ -970,7 +970,7 @@ const item = arr[0];
970970
const foo = ( 3 + 5 ) as number;
971971
`,
972972
Output: []string{`
973-
const foo = ( 3 + 5 ) ;
973+
const foo = ( 3 + 5 );
974974
`,
975975
},
976976
Errors: []rule_tester.InvalidTestCaseError{
@@ -986,7 +986,7 @@ const foo = ( 3 + 5 ) ;
986986
const foo = ( 3 + 5 ) /*as*/ as number;
987987
`,
988988
Output: []string{`
989-
const foo = ( 3 + 5 ) /*as*/ ;
989+
const foo = ( 3 + 5 ) /*as*/;
990990
`,
991991
},
992992
Errors: []rule_tester.InvalidTestCaseError{
@@ -1007,8 +1007,7 @@ const foo = ( 3 + 5
10071007
`,
10081008
Output: []string{`
10091009
const foo = ( 3 + 5
1010-
) /*as*/ //as
1011-
;
1010+
) /*as*/;
10121011
`,
10131012
},
10141013
Errors: []rule_tester.InvalidTestCaseError{
@@ -1024,7 +1023,7 @@ const foo = ( 3 + 5
10241023
const foo = (3 + (5 as number) ) as number;
10251024
`,
10261025
Output: []string{`
1027-
const foo = (3 + (5 as number) ) ;
1026+
const foo = (3 + (5 as number) );
10281027
`,
10291028
},
10301029
Errors: []rule_tester.InvalidTestCaseError{
@@ -1040,7 +1039,7 @@ const foo = (3 + (5 as number) ) ;
10401039
const foo = 3 + 5/*as*/ as number;
10411040
`,
10421041
Output: []string{`
1043-
const foo = 3 + 5/*as*/ ;
1042+
const foo = 3 + 5/*as*/;
10441043
`,
10451044
},
10461045
Errors: []rule_tester.InvalidTestCaseError{
@@ -1056,7 +1055,7 @@ const foo = 3 + 5/*as*/ ;
10561055
const foo = 3 + 5/*a*/ /*b*/ as number;
10571056
`,
10581057
Output: []string{`
1059-
const foo = 3 + 5/*a*/ /*b*/ ;
1058+
const foo = 3 + 5/*a*/ /*b*/;
10601059
`,
10611060
},
10621061
Errors: []rule_tester.InvalidTestCaseError{
@@ -1168,7 +1167,7 @@ const bar = foo.a as string | undefined;
11681167
declare const foo: {
11691168
a?: string;
11701169
};
1171-
const bar = foo.a ;
1170+
const bar = foo.a;
11721171
`,
11731172
},
11741173
TSConfig: "./tsconfig.exactOptionalPropertyTypes.json",
@@ -1191,7 +1190,7 @@ const bar = foo.a as string | undefined;
11911190
declare const foo: {
11921191
a?: string | undefined;
11931192
};
1194-
const bar = foo.a ;
1193+
const bar = foo.a;
11951194
`,
11961195
},
11971196
TSConfig: "./tsconfig.exactOptionalPropertyTypes.json",
@@ -1264,7 +1263,7 @@ class T {
12641263
`,
12651264
Output: []string{`
12661265
class T {
1267-
readonly a = 'a' ;
1266+
readonly a = 'a';
12681267
}
12691268
`,
12701269
},
@@ -1283,7 +1282,7 @@ class T {
12831282
`,
12841283
Output: []string{`
12851284
class T {
1286-
readonly a = 3 ;
1285+
readonly a = 3;
12871286
}
12881287
`,
12891288
},
@@ -1306,7 +1305,7 @@ class T {
13061305
type S = 10;
13071306
13081307
class T {
1309-
readonly a = 10 ;
1308+
readonly a = 10;
13101309
}
13111310
`,
13121311
},
@@ -1325,7 +1324,7 @@ class T {
13251324
`,
13261325
Output: []string{`
13271326
class T {
1328-
readonly a = (3 + 5) ;
1327+
readonly a = (3 + 5);
13291328
}
13301329
`,
13311330
},
@@ -1369,7 +1368,7 @@ enum T {
13691368
}
13701369
13711370
declare const a: T.Value1;
1372-
const b = a ;
1371+
const b = a;
13731372
`,
13741373
},
13751374
Errors: []rule_tester.InvalidTestCaseError{
@@ -1395,7 +1394,7 @@ enum T {
13951394
}
13961395
13971396
declare const a: T.Value1;
1398-
const b = a ;
1397+
const b = a;
13991398
`,
14001399
},
14011400
Errors: []rule_tester.InvalidTestCaseError{

0 commit comments

Comments
 (0)