-
Notifications
You must be signed in to change notification settings - Fork 119
/
suggestions.ts
104 lines (87 loc) · 2.79 KB
/
suggestions.ts
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
export interface Suggestion {
suggestionCode: string;
message: string;
}
export const FIX_INVALID_CONSTRUCTOR = {
suggestionCode: 'DS002',
message: 'Fix invalid constructor',
};
export const REMOVE_ARRAY_FROM = {
suggestionCode: 'DS101',
message: 'Remove unnecessary use of Array.from',
};
export const CLEAN_UP_IMPLICIT_RETURNS = {
suggestionCode: 'DS102',
message: 'Remove unnecessary code created because of implicit returns',
};
export const REMOVE_GUARD = {
suggestionCode: 'DS103',
message: 'Rewrite code to no longer use __guard__, or convert again using --optional-chaining',
};
export const AVOID_INLINE_ASSIGNMENTS = {
suggestionCode: 'DS104',
message: 'Avoid inline assignments',
};
export const SIMPLIFY_COMPLEX_ASSIGNMENTS = {
suggestionCode: 'DS201',
message: 'Simplify complex destructure assignments',
};
export const SIMPLIFY_DYNAMIC_RANGE_LOOPS = {
suggestionCode: 'DS202',
message: 'Simplify dynamic range loops',
};
export const CLEAN_UP_FOR_OWN_LOOPS = {
suggestionCode: 'DS203',
message: 'Remove `|| {}` from converted for-own loops',
};
export const FIX_INCLUDES_EVALUATION_ORDER = {
suggestionCode: 'DS204',
message: 'Change includes calls to have a more natural evaluation order',
};
export const AVOID_IIFES = {
suggestionCode: 'DS205',
message: 'Consider reworking code to avoid use of IIFEs',
};
export const AVOID_INITCLASS = {
suggestionCode: 'DS206',
message: 'Consider reworking classes to avoid initClass',
};
export const SHORTEN_NULL_CHECKS = {
suggestionCode: 'DS207',
message: 'Consider shorter variations of null checks',
};
export const AVOID_TOP_LEVEL_THIS = {
suggestionCode: 'DS208',
message: 'Avoid top-level this',
};
export const AVOID_TOP_LEVEL_RETURN = {
suggestionCode: 'DS209',
message: 'Avoid top-level return',
};
export function mergeSuggestions(suggestions: Array<Suggestion>): Array<Suggestion> {
const suggestionsByCode: { [key: string]: Suggestion } = {};
for (const suggestion of suggestions) {
suggestionsByCode[suggestion.suggestionCode] = suggestion;
}
return Object.keys(suggestionsByCode)
.sort()
.map((code) => suggestionsByCode[code]);
}
export function prependSuggestionComment(code: string, suggestions: Array<Suggestion>): string {
if (suggestions.length === 0) {
return code;
}
const commentLines = [
'/*',
' * decaffeinate suggestions:',
...suggestions.map(({ suggestionCode, message }) => ` * ${suggestionCode}: ${message}`),
' * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md',
' */',
];
const codeLines = code.split('\n');
if (codeLines[0].startsWith('#!')) {
return [codeLines[0], ...commentLines, ...codeLines.slice(1)].join('\n');
} else {
return [...commentLines, ...codeLines].join('\n');
}
}