-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
232 lines (206 loc) · 6.78 KB
/
extension.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log(
'Congratulations, your extension "lodash-import-what-you-need" is now active!'
);
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand(
"lodash-import-what-you-need.lodash-treeshake",
function () {
// The code you place here will be executed every time your command is executed
let lodashFunctions = require("./lodashFunctions.js");
const editor = vscode.window.activeTextEditor;
let importLineOne = -1;
let importLodashLine = -1;
let lodashText = "";
let lodashLines = [];
let usedLodashFunctions = [];
if (editor) {
let document = editor.document;
if (document.isDirty) document.save();
for (let index = 0; index < document.lineCount; index++) {
let x = document.lineAt(index).text;
let lcx = x.toLowerCase();
if (lcx.includes("import") && importLineOne === -1) {
importLineOne = index;
}
if (
lcx.includes("import") &&
lcx.includes("lodash") &&
!lcx.includes("lodash/")
) {
importLodashLine = index;
if (lcx.includes("{")) {
vscode.window.showInformationMessage(
"[Lodash Treeshake] Cannot run with lodash imported with curly brases. E.g. `import {filter} from 'lodash';`"
);
return;
}
// if imported as `import lodash from 'lodash';`
else if(lcx.includes("* as ")) {
let spaces = x.split(" ");
lodashText = spaces[3];
break;
}
else{
let spaces = x.split(" ");
lodashText = spaces[1];
break;
}
}
}
//If lodash import was not found then alert user
if (importLodashLine === -1) {
vscode.window.showInformationMessage(
`[Lodash Treeshake] Lodash was not found!`
);
return;
}
//Iterate through document and find where lodash functions are used
for (let index = 0; index < document.lineCount; index++) {
let x = document.lineAt(index).text;
//Check if line includes the name of lodash from the import
if (x.includes(`${lodashText}.`)) {
//If the name was not found strip out and only get the function name
let func_line = x.trim().split(lodashText);
let func = func_line[1].split("(")[0].replaceAll(".", "");
//Check filter out matching function names
let foundFunction = lodashFunctions.filter((y) => y.includes(func));
//If the name was found
if (foundFunction.length >= 1) {
lodashLines.push(index);
usedLodashFunctions.push(func);
} else {
//Show message saying that the lodash function is unknown
vscode.window.showInformationMessage(
`[Lodash Treeshake] Unknown lodash function ${lodashText}${func}() at line ${
index + 1
}`
);
}
}
}
//Filter out duplicates and replace .
usedLodashFunctions = usedLodashFunctions.filter(
(item, i, ar) => ar.indexOf(item) === i
);
usedLodashFunctions = usedLodashFunctions.map((x) =>
x.replaceAll(".", "")
);
usedLodashFunctions = usedLodashFunctions.sort(
(x, y) => x.length - y.length
);
let newImports = [];
for (let index = 0; index < usedLodashFunctions.length; index++) {
const element = usedLodashFunctions[index];
newImports.push(`import ${element} from "lodash/${element}";`);
}
//Replace lodash imports
editor
.edit((selectedText) => {
//Delete existing import
selectedText.delete(
new vscode.Range(
new vscode.Position(importLodashLine, 0),
new vscode.Position(
importLodashLine,
document.lineAt(importLodashLine).text.length
)
)
);
//Insert new lodash import text for all of the imports needed
for (let index = 0; index < newImports.length; index++) {
const element = newImports[index];
selectedText.insert(
new vscode.Position(importLodashLine + index, 0),
element
);
}
//Add new line after imports
selectedText.insert(
new vscode.Position(importLodashLine + newImports.length - 1, 0),
"\r\n"
);
//Update function calls
for (let index = 0; index < lodashLines.length; index++) {
const lineNumber = lodashLines[index];
let currentText = document.lineAt(lineNumber).text;
let newText = currentText.replace(lodashText + ".", "");
console.log(lineNumber + 1, newText);
selectedText.replace(
new vscode.Range(
new vscode.Position(lineNumber, 0),
new vscode.Position(
lineNumber,
document.lineAt(lineNumber).text.length
)
),
newText
);
// editor.edit((selectedText) => {
// selectedText.delete(
// new vscode.Range(
// new vscode.Position(lineNumber, 0),
// new vscode.Position(
// lineNumber,
// document.lineAt(lineNumber).text.length
// )
// )
// );
// selectedText.insert(new vscode.Position(lineNumber, 0), newText);
// });
}
})
.then((res) => {
vscode.window.showInformationMessage(
`[Lodash Treeshake] Edits have ${
res ? "sucessfully been" : "failed to be"
} applied`
);
});
// .then((res) => {
// if (!res) {
// vscode.window.showInformationMessage(
// `[Lodash Treeshake] Import edits failed`
// );
// return;
// }
// //Replace lodash function calls
// editor
// .edit((selectedText) => {
// })
// .then((res) => {
// vscode.window.showInformationMessage(
// `[Lodash Treeshake] Edits have ${
// res ? "sucessfully been" : "failed to be"
// } applied`
// );
// });
// });
} else {
// Display a message box to the user
vscode.window.showInformationMessage(
"[Lodash Treeshake] There is no file currently open!"
);
}
}
);
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate,
};