Skip to content

Commit fbf6ab7

Browse files
author
Damian Sznajder
committed
feat: add removing react from imports in ts components
1 parent 3e62416 commit fbf6ab7

File tree

7 files changed

+375
-2229
lines changed

7 files changed

+375
-2229
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
*.log
66
.DS_Store
77
lib/
8+
src/snippets/generated.json

Diff for: CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All info about changes
44

55
# Released
66

7+
[4.3.0] - 2020-12-18
8+
9+
- BREAKING CHANGES:
10+
- From now on snippets have options for Language, React 17+ support, typescript and more.
11+
- Added experimental prettier support, for now it's kinda buggy and needs some work.
12+
- Refactored the codebase, moved snippets to ts files.
13+
714
[3.1.0] - 2020-12-18
815

916
- Add couple of snippets and React 17 components

Diff for: package.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"scripts": {
117117
"vscode:prepublish": "yarn compile",
118118
"compile": "rm -rf lib; tsc -p ./ --noEmit false --module commonjs --outDir lib",
119+
"compile:dev": "rm -rf lib; tsc -p ./ --noEmit false --module commonjs --outDir lib",
119120
"lint": "eslint --ext .js,.ts,.tsx ./src/",
120121
"watch": "tsc -watch -p ./",
121122
"typescript": "tsc --noEmit"
@@ -127,24 +128,24 @@
127128
"prettier": "^2"
128129
},
129130
"devDependencies": {
130-
"@babel/cli": "7.16.0",
131-
"@babel/eslint-parser": "7.16.5",
132-
"@babel/preset-typescript": "7.16.5",
133-
"@types/node": "17.0.5",
134-
"@types/prettier": "2.4.2",
135-
"@types/vscode": "1.60.0",
136-
"@typescript-eslint/eslint-plugin": "5.8.1",
137-
"@typescript-eslint/parser": "5.8.1",
138-
"eslint": "8.5.0",
131+
"@babel/cli": "7.17.0",
132+
"@babel/eslint-parser": "7.17.0",
133+
"@babel/preset-typescript": "7.16.7",
134+
"@types/node": "17.0.16",
135+
"@types/prettier": "2.4.3",
136+
"@types/vscode": "1.64.0",
137+
"@typescript-eslint/eslint-plugin": "5.11.0",
138+
"@typescript-eslint/parser": "5.11.0",
139+
"eslint": "8.8.0",
139140
"eslint-config-prettier": "8.3.0",
140141
"eslint-plugin-babel": "5.3.1",
141142
"eslint-plugin-eslint-comments": "3.2.0",
142-
"eslint-plugin-import": "2.25.3",
143-
"eslint-plugin-jest": "25.3.3",
143+
"eslint-plugin-import": "2.25.4",
144+
"eslint-plugin-jest": "26.1.0",
144145
"eslint-plugin-prettier": "4.0.0",
145146
"eslint-plugin-simple-import-sort": "7.0.0",
146147
"prettier": "2.5.1",
147-
"typescript": "4.5.4"
148+
"typescript": "4.5.5"
148149
},
149150
"prettier": {
150151
"bracketSameLine": false,

Diff for: src/helpers/parseSnippetToBody.ts

+4-22
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,17 @@ import { formatSnippet } from './formatters';
33
import { Snippet } from './generateSnippets';
44
import replaceOrRemoveReactImport from './replaceOrRemoveReactImport';
55

6-
const withReactImport = [
7-
'rfce',
8-
'rfc',
9-
'rfcp',
10-
'rafce',
11-
'rafc',
12-
'rafcp',
13-
'rnfe',
14-
'rnfes',
15-
'rnf',
16-
'rnfs',
17-
'stest',
18-
'sntest',
19-
'srtest',
20-
'snrtest',
21-
'hocredux',
22-
'hoc',
23-
];
24-
256
const parseSnippetToBody = (snippet: Snippet) => {
267
const { importReactOnTop } = extensionConfig();
278
const body =
289
typeof snippet.body === 'string' ? snippet.body : snippet.body.join('\n');
2910

3011
const snippetBody = importReactOnTop
3112
? body
32-
: withReactImport.includes(snippet.prefix)
33-
? replaceOrRemoveReactImport(snippet.body)
34-
: body;
13+
: replaceOrRemoveReactImport({
14+
prefix: snippet.prefix,
15+
body: snippet.body,
16+
});
3517

3618
const formattedSnippet = formatSnippet(snippetBody).split('\n');
3719

Diff for: src/helpers/replaceOrRemoveReactImport.ts

+53-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
1-
const replaceOrRemoveReactImport = (snippetBody: string[]) => {
2-
const reactImportIndex = snippetBody.findIndex((line) =>
1+
import { Snippet } from './generateSnippets';
2+
3+
const snippetWithReactImportPrefixes = [
4+
'rfce',
5+
'rfc',
6+
'rfcp',
7+
'rafce',
8+
'rafc',
9+
'rafcp',
10+
'rnfe',
11+
'rnfes',
12+
'rnf',
13+
'rnfs',
14+
'stest',
15+
'sntest',
16+
'srtest',
17+
'snrtest',
18+
'hocredux',
19+
'hoc',
20+
'tsrafc',
21+
'tsrafce',
22+
'tsrcc',
23+
'tsrcredux',
24+
'tsrce',
25+
'tsrpce',
26+
'tsrpc',
27+
'tsrfc',
28+
'tsrfce',
29+
'tsrnf',
30+
'tsrnfs',
31+
];
32+
33+
const replaceOrRemoveReactImport = ({
34+
body,
35+
prefix,
36+
}: {
37+
body: string[];
38+
prefix: Snippet['prefix'];
39+
}) => {
40+
if (!snippetWithReactImportPrefixes.includes(prefix)) {
41+
return body.join('\n');
42+
}
43+
44+
let bodyCopy = [...body];
45+
const reactImportIndex = bodyCopy.findIndex((line) =>
346
line.match(new RegExp(/import React/, 'g')),
447
);
548

649
if (reactImportIndex !== -1) {
7-
const line = snippetBody[reactImportIndex];
8-
snippetBody[reactImportIndex] = line
50+
const line = bodyCopy[reactImportIndex];
51+
const newLine = line
952
.replace(new RegExp(/^import React .*$/, 'g'), '')
1053
.replace(new RegExp(/^import React, /, 'g'), 'import ');
54+
55+
bodyCopy[reactImportIndex] = newLine;
56+
if (!newLine.length) {
57+
bodyCopy = bodyCopy.filter(Boolean);
58+
}
1159
}
1260

13-
return snippetBody.join('\n');
61+
return bodyCopy.join('\n');
1462
};
1563

1664
export default replaceOrRemoveReactImport;

0 commit comments

Comments
 (0)