FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TypeScript/src/services/codeFixProvider.ts at main · unknownDev31/TypeScript · GitHub
unknownDev31
TypeScript
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TypeScript
/
src
/
services
/
codeFixProvider.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
132 lines (116 loc) · 5.4 KB
Breadcrumbs
TypeScript
/
src
/
services
/
codeFixProvider.ts
Copy path
File metadata and controls
132 lines (116 loc) · 5.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import
{
arrayFrom
,
cast
,
CodeActionCommand
,
CodeFixAction
,
CodeFixAllContext
,
CodeFixContext
,
CodeFixContextBase
,
CodeFixRegistration
,
CombinedCodeActions
,
computeSuggestionDiagnostics
,
contains
,
createMultiMap
,
Debug
,
Diagnostic
,
DiagnosticOrDiagnosticAndArguments
,
diagnosticToString
,
DiagnosticWithLocation
,
FileTextChanges
,
flatMap
,
isString
,
map
,
TextChange
,
textChanges
,
}
from
"./_namespaces/ts"
;
const
errorCodeToFixes
=
createMultiMap
<
string
,
CodeFixRegistration
>
(
)
;
const
fixIdToRegistration
=
new
Map
<
string
,
CodeFixRegistration
>
(
)
;
/**
@internal
*/
export
function
createCodeFixActionWithoutFixAll
(
fixName
:
string
,
changes
:
FileTextChanges
[
]
,
description
:
DiagnosticOrDiagnosticAndArguments
)
{
return
createCodeFixActionWorker
(
fixName
,
diagnosticToString
(
description
)
,
changes
,
/*fixId*/
undefined
,
/*fixAllDescription*/
undefined
)
;
}
/**
@internal
*/
export
function
createCodeFixAction
(
fixName
:
string
,
changes
:
FileTextChanges
[
]
,
description
:
DiagnosticOrDiagnosticAndArguments
,
fixId
:
{
}
,
fixAllDescription
:
DiagnosticOrDiagnosticAndArguments
,
command
?:
CodeActionCommand
)
:
CodeFixAction
{
return
createCodeFixActionWorker
(
fixName
,
diagnosticToString
(
description
)
,
changes
,
fixId
,
diagnosticToString
(
fixAllDescription
)
,
command
)
;
}
/**
@internal
*/
export
function
createCodeFixActionMaybeFixAll
(
fixName
:
string
,
changes
:
FileTextChanges
[
]
,
description
:
DiagnosticOrDiagnosticAndArguments
,
fixId
?:
{
}
,
fixAllDescription
?:
DiagnosticOrDiagnosticAndArguments
,
command
?:
CodeActionCommand
)
{
return
createCodeFixActionWorker
(
fixName
,
diagnosticToString
(
description
)
,
changes
,
fixId
,
fixAllDescription
&&
diagnosticToString
(
fixAllDescription
)
,
command
)
;
}
function
createCodeFixActionWorker
(
fixName
:
string
,
description
:
string
,
changes
:
FileTextChanges
[
]
,
fixId
?:
{
}
,
fixAllDescription
?:
string
,
command
?:
CodeActionCommand
)
:
CodeFixAction
{
return
{
fixName
,
description
,
changes
,
fixId
,
fixAllDescription
,
commands
:
command
?
[
command
]
:
undefined
}
;
}
/**
@internal
*/
export
function
registerCodeFix
(
reg
:
CodeFixRegistration
)
{
for
(
const
error
of
reg
.
errorCodes
)
{
errorCodeToFixesArray
=
undefined
;
errorCodeToFixes
.
add
(
String
(
error
)
,
reg
)
;
}
if
(
reg
.
fixIds
)
{
for
(
const
fixId
of
reg
.
fixIds
)
{
Debug
.
assert
(
!
fixIdToRegistration
.
has
(
fixId
)
)
;
fixIdToRegistration
.
set
(
fixId
,
reg
)
;
}
}
}
let
errorCodeToFixesArray
:
readonly
string
[
]
|
undefined
;
/**
@internal
*/
export
function
getSupportedErrorCodes
(
)
:
readonly
string
[
]
{
return
errorCodeToFixesArray
??=
arrayFrom
(
errorCodeToFixes
.
keys
(
)
)
;
}
function
removeFixIdIfFixAllUnavailable
(
registration
:
CodeFixRegistration
,
diagnostics
:
Diagnostic
[
]
)
{
const
{
errorCodes
}
=
registration
;
let
maybeFixableDiagnostics
=
0
;
for
(
const
diag
of
diagnostics
)
{
if
(
contains
(
errorCodes
,
diag
.
code
)
)
maybeFixableDiagnostics
++
;
if
(
maybeFixableDiagnostics
>
1
)
break
;
}
const
fixAllUnavailable
=
maybeFixableDiagnostics
<
2
;
return
(
{
fixId
,
fixAllDescription
,
...
action
}
:
CodeFixAction
)
:
CodeFixAction
=>
{
return
fixAllUnavailable
?
action
:
{
...
action
,
fixId
,
fixAllDescription
}
;
}
;
}
/**
@internal
*/
export
function
getFixes
(
context
:
CodeFixContext
)
:
readonly
CodeFixAction
[
]
{
const
diagnostics
=
getDiagnostics
(
context
)
;
const
registrations
=
errorCodeToFixes
.
get
(
String
(
context
.
errorCode
)
)
;
return
flatMap
(
registrations
,
f
=>
map
(
f
.
getCodeActions
(
context
)
,
removeFixIdIfFixAllUnavailable
(
f
,
diagnostics
)
)
)
;
}
/**
@internal
*/
export
function
getAllFixes
(
context
:
CodeFixAllContext
)
:
CombinedCodeActions
{
// Currently fixId is always a string.
return
fixIdToRegistration
.
get
(
cast
(
context
.
fixId
,
isString
)
)
!
.
getAllCodeActions
!
(
context
)
;
}
/**
@internal
*/
export
function
createCombinedCodeActions
(
changes
:
FileTextChanges
[
]
,
commands
?:
CodeActionCommand
[
]
)
:
CombinedCodeActions
{
return
{
changes
,
commands
}
;
}
/**
@internal
*/
export
function
createFileTextChanges
(
fileName
:
string
,
textChanges
:
TextChange
[
]
)
:
FileTextChanges
{
return
{
fileName
,
textChanges
}
;
}
/**
@internal
*/
export
function
codeFixAll
(
context
:
CodeFixAllContext
,
errorCodes
:
number
[
]
,
use
:
(
changes
:
textChanges
.
ChangeTracker
,
error
:
DiagnosticWithLocation
,
commands
:
CodeActionCommand
[
]
)
=>
void
,
)
:
CombinedCodeActions
{
const
commands
:
CodeActionCommand
[
]
=
[
]
;
const
changes
=
textChanges
.
ChangeTracker
.
with
(
context
,
t
=>
eachDiagnostic
(
context
,
errorCodes
,
diag
=>
use
(
t
,
diag
,
commands
)
)
)
;
return
createCombinedCodeActions
(
changes
,
commands
.
length
===
0
?
undefined
:
commands
)
;
}
/**
@internal
*/
export
function
eachDiagnostic
(
context
:
CodeFixAllContext
,
errorCodes
:
readonly
number
[
]
,
cb
:
(
diag
:
DiagnosticWithLocation
)
=>
void
)
:
void
{
for
(
const
diag
of
getDiagnostics
(
context
)
)
{
if
(
contains
(
errorCodes
,
diag
.
code
)
)
{
cb
(
diag
as
DiagnosticWithLocation
)
;
}
}
}
function
getDiagnostics
(
{
program
,
sourceFile
,
cancellationToken
}
:
CodeFixContextBase
)
{
return
[
...
program
.
getSemanticDiagnostics
(
sourceFile
,
cancellationToken
)
,
...
program
.
getSyntacticDiagnostics
(
sourceFile
,
cancellationToken
)
,
...
computeSuggestionDiagnostics
(
sourceFile
,
program
,
cancellationToken
)
]
;
}
Back
|
FazBrowse Home
|
New Git URL