-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (177 loc) · 8.45 KB
/
index.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
//import { TypeChecker, Expression } from 'typescript/lib/tsserverlibrary'
function init(modules) {
const ts = modules.typescript;
function create(info) {
const proxy = Object.create(null);
for (let k of Object.keys(info.languageService)) {
const x = info.languageService[k];
proxy[k] = (...args) => x.apply(info.languageService, args);
}
function isEmptyCaseBlock(node) {
for (let c of node.caseBlock.clauses) {
for (let s of c.statements) {
if (!ts.isBreakStatement(s))
return false;
}
}
return node.caseBlock.getChildCount() === 3; // ===3 mease clauses is empty. only ['{', SyntaxList, '}']
}
function extractEnumInfo(fileName, positionOrRange, simple) {
const sourceFile = info.languageService.getProgram().getSourceFile(fileName);
if (!sourceFile)
return false;
let nodeAtCursor = findChildContainingPosition(sourceFile, positionOrRangeToNumber(positionOrRange));
while (nodeAtCursor &&
!ts.isSwitchStatement(nodeAtCursor)) {
nodeAtCursor = nodeAtCursor.parent;
}
//Is the node is an empty switch statement?
if (nodeAtCursor &&
ts.isSwitchStatement(nodeAtCursor) &&
isEmptyCaseBlock(nodeAtCursor)) // ===3 mease clauses is empty. only ['{', SyntaxList, '}']
{
let typeChecker = info.languageService.getProgram().getTypeChecker();
let expType = typeChecker.getTypeAtLocation(nodeAtCursor.expression);
//Is the exp type is an Enum type?
let list = extractEnumMemberList(expType, typeChecker, nodeAtCursor);
if (list) {
if (simple)
return true;
let pos = nodeAtCursor.caseBlock.getStart() + 1;
return { pos, caseBlockNode: nodeAtCursor.caseBlock, nodeList: list, switchNode: nodeAtCursor };
}
// if (expType.flags & ts.TypeFlags.Literal)
// {
// expType = (expType as ts.LiteralType).regularType;
// }
// if ((expType.flags & ts.TypeFlags.EnumLike) &&
// expType.aliasSymbol)
// {
// if (simple)
// {
// return true;
// }
// let t2 = expType;
// if (t2.aliasSymbol)
// {
// let pos = nodeAtCursor.caseBlock.getStart() + 1;
// let list: string[] = [];
// let nodeList: ts.Expression[] = [];
// t2.aliasSymbol.exports.forEach(t =>
// {
// list.push(typeChecker.symbolToString(t, nodeAtCursor));
// nodeList.push(typeChecker.symbolToExpression(t, 0, nodeAtCursor));
// });
// //let list = t2.types.map(t => typeChecker.typeToString(t));
// return { pos, list, caseBlockNode: nodeAtCursor.caseBlock, nodeList, switchNode: nodeAtCursor }
// }
}
}
// Here starts our second behavior: a refactor that will always be suggested no matter where is the cursor and does nothing
// overriding getApplicableRefactors we add our refactor metadata only if the user has the cursor on the place we desire, in our case a class or interface declaration identifier
proxy.getApplicableRefactors = (fileName, positionOrRange) => {
const refactors = info.languageService.getApplicableRefactors(fileName, positionOrRange, undefined) || [];
const sourceFile = info.languageService.getProgram().getSourceFile(fileName);
if (!sourceFile) {
return refactors;
}
if (extractEnumInfo(fileName, positionOrRange, true)) {
refactors.push({
name: 'generate-switch-case',
description: 'generate switch case desc',
actions: [{ name: 'generate-switch-case', description: 'Generate Switch Case' }],
});
}
return refactors;
};
proxy.getEditsForRefactor = (fileName, formatOptions, positionOrRange, refactorName, actionName, preferences) => {
const refactors = info.languageService.getEditsForRefactor(fileName, formatOptions, positionOrRange, refactorName, actionName, preferences);
if (actionName === 'generate-switch-case') {
let obj = extractEnumInfo(fileName, positionOrRange, false);
if (obj) {
const sourceFile = info.languageService.getProgram().getSourceFile(fileName);
let clause = [];
obj.nodeList.forEach(item => {
//ts.createPropertyAccessChain()
clause.push(ts.createCaseClause(item, [ts.createBreak()]));
});
clause.push(ts.createDefaultClause([ts.createBreak()]));
let caseBlockNode = ts.createCaseBlock(clause);
let switchNode = ts.createSwitch(ts.getMutableClone(obj.switchNode.expression), caseBlockNode);
let edits = ts['textChanges'].ChangeTracker.with({
host: info.languageServiceHost,
formatContext: ts['formatting'].getFormatContext(formatOptions),
preferences: preferences
}, (tracker) => {
//tracker.insertNodesAt(sourceFile, obj.pos, clause, {});
//tracker.replaceNode(sourceFile, obj.caseBlockNode, caseBlockNode, {});
tracker.replaceNode(sourceFile, obj.switchNode, switchNode, undefined);
});
return { edits };
}
}
return refactors;
};
return proxy;
}
function extractEnumMemberList(type, typeChecker, node) {
let list;
//enum is also a union
if (type.flags & ts.TypeFlags.Union) {
/*
support
class A{};
class B{};
type Union = 1|2|true|A|B;
boolean is a Union => true|false
*/
const trueType = typeChecker['getTrueType']();
const falseType = typeChecker['getFalseType']();
let unionType = type;
let isAllLiterial = unionType.types.every(t => {
let flag = t.flags;
return (flag & ts.TypeFlags.NumberLiteral) || (flag & ts.TypeFlags.StringLiteral) || t === trueType || t === falseType ||
(flag & ts.TypeFlags.Object) && (t.objectFlags & ts.ObjectFlags.Class); //class type. 'class A{}'
});
if (isAllLiterial) {
return unionType.types.map(t => {
let lt = t;
if (t.symbol)
return typeChecker.symbolToExpression(t.symbol, 0, node);
if (t === trueType)
return ts.createTrue();
if (t === falseType)
return ts.createFalse();
return ts.createLiteral(lt.value);
});
}
}
return;
}
// Helper functions used in this tutorial
/**normalize the parameter so we are sure is of type Range */
function positionOrRangeToRange(positionOrRange) {
return typeof positionOrRange === 'number'
? { pos: positionOrRange, end: positionOrRange }
: positionOrRange;
}
/**normalize the parameter so we are sure is of type number */
function positionOrRangeToNumber(positionOrRange) {
return typeof positionOrRange === 'number' ?
positionOrRange :
positionOrRange.pos;
}
/** from given position we find the child node that contains it */
function findChildContainingPosition(sourceFile, position) {
function find(node) {
if (position >= node.getStart() && position < node.getEnd()) {
//return ts.forEachChild(node, find) || node
return node.forEachChild(find) || node;
}
}
return find(sourceFile);
}
return { create };
}
module.exports = init;