Skip to content

Commit dcf3530

Browse files
author
Damian Sznajder
committed
fix: typescript dynamic generate
1 parent d410801 commit dcf3530

13 files changed

+1336
-314
lines changed

.vscodeignore

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
.vscode/**
2-
src/**
3-
.gitignore
41
**/tsconfig.json
52
*.ts
6-
webpack.config.js
3+
.github/**
4+
.gitignore
5+
.vscode/**
6+
docs/**
7+
node_modules/**
8+
src/**

LICENCE LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2019 Damian Sznajder
3+
Copyright (c) 2022 Damian Sznajder
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
}
4040
},
4141
"activationEvents": [
42+
"onLanguage:typescript",
43+
"onLanguage:javascript",
4244
"onCommand:reactSnippets.search"
4345
],
4446
"contributes": {
@@ -89,6 +91,11 @@
8991
"markdownDescription": "Controls how many spaces snippets will have.\nOnly applies when `#reactSnippets.settings.prettierEnabled#` is disabled",
9092
"default": 2
9193
},
94+
"reactSnippets.settings.languageScopes": {
95+
"type": "string",
96+
"markdownDescription": "defines the language scopes for which the snippets will be available.\nUse comma separated values.\nFor example: `typescript,typescriptreact,javascript,javascriptreact`",
97+
"default": "typescript,typescriptreact,javascript,javascriptreact"
98+
},
9299
"reactSnippets.settings.typescriptComponentPropsStatePrefix": {
93100
"type": "string",
94101
"markdownDescription": "Controls which prefix for typescript snippets should use for props/state.",
@@ -127,16 +134,15 @@
127134
"typescript": "tsc --noEmit"
128135
},
129136
"dependencies": {
130-
"prettier": "2.5.1",
131-
"vscode": "1.1.37"
137+
"prettier": "2.5.1"
132138
},
133139
"devDependencies": {
134140
"@babel/cli": "7.16.0",
135141
"@babel/eslint-parser": "7.16.5",
136142
"@babel/preset-typescript": "7.16.5",
137143
"@types/node": "17.0.5",
138144
"@types/prettier": "2.4.2",
139-
"@types/vscode": "1.63.1",
145+
"@types/vscode": "1.60.0",
140146
"@typescript-eslint/eslint-plugin": "5.8.1",
141147
"@typescript-eslint/parser": "5.8.1",
142148
"eslint": "8.5.0",

src/generateSnippets.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { writeFile } from 'fs';
2-
import { workspace } from 'vscode';
32

4-
import { ExtensionSettings, replaceSnippetPlaceholders } from './helpers';
3+
import {
4+
extensionConfig,
5+
formatSnippet,
6+
replaceSnippetPlaceholders,
7+
} from './helpers';
58
import componentsSnippets, {
69
ComponentsSnippet,
710
} from './sourceSnippets/components';
@@ -47,11 +50,10 @@ export type Snippets = {
4750
};
4851

4952
const getSnippets = () => {
50-
const config = workspace.getConfiguration(
51-
'reactSnippets',
52-
) as unknown as ExtensionSettings;
53+
const { typescript, languageScopes } = extensionConfig();
5354

5455
const snippets = [
56+
...(typescript ? typescriptSnippets : []),
5557
...componentsSnippets,
5658
...consoleSnippets,
5759
...hooksSnippets,
@@ -60,13 +62,17 @@ const getSnippets = () => {
6062
...reactNativeSnippets,
6163
...reduxSnippets,
6264
...testsSnippets,
63-
...(config.typescript ? typescriptSnippets : []),
6465
].reduce((acc, snippet) => {
65-
acc[snippet.key] = snippet;
66+
const snippetBody =
67+
typeof snippet.body === 'string' ? snippet.body : snippet.body.join('\n');
68+
acc[snippet.key] = Object.assign(snippet, {
69+
scope: languageScopes,
70+
body: formatSnippet(snippetBody).split('\n'),
71+
});
6672
return acc;
6773
}, {} as Snippets);
6874

69-
return replaceSnippetPlaceholders(JSON.stringify(snippets, null, 2), config);
75+
return replaceSnippetPlaceholders(JSON.stringify(snippets, null, 2));
7076
};
7177

7278
export const generateSnippets = () =>

src/helpers.ts

+33-38
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,40 @@ import { workspace } from 'vscode';
44
import { SnippetPlaceholders } from './types';
55

66
export type ExtensionSettings = {
7+
languageScopes: string;
78
prettierEnabled: boolean;
89
semiColons: boolean;
910
importReactOnTop: boolean;
10-
quotes: boolean;
11+
singleQuote: boolean;
1112
typescript: boolean;
1213
tabWidth: number;
1314
typescriptComponentPropsStatePrefix: 'type' | 'interface';
1415
};
1516

16-
export const replaceSnippetPlaceholders = (
17-
snippetString: string,
18-
extensionConfig: ExtensionSettings,
19-
) =>
17+
let prettierConfig: prettier.Options | null;
18+
prettier
19+
.resolveConfig('', { editorconfig: true })
20+
.then((config) => (prettierConfig = config));
21+
22+
export const extensionConfig = () =>
23+
workspace.getConfiguration(
24+
'reactSnippets.settings',
25+
) as unknown as ExtensionSettings;
26+
27+
const getPrettierConfig = (): Options => {
28+
const { semiColons, singleQuote, tabWidth, prettierEnabled } =
29+
extensionConfig();
30+
31+
return {
32+
parser: 'typescript',
33+
semi: semiColons,
34+
singleQuote,
35+
tabWidth,
36+
...(prettierEnabled && prettierConfig),
37+
};
38+
};
39+
40+
export const replaceSnippetPlaceholders = (snippetString: string) =>
2041
String(snippetString)
2142
.replace(
2243
new RegExp(SnippetPlaceholders.FileName, 'g'),
@@ -25,11 +46,7 @@ export const replaceSnippetPlaceholders = (
2546
.replace(new RegExp(SnippetPlaceholders.FirstTab, 'g'), '${1:first}')
2647
.replace(new RegExp(SnippetPlaceholders.SecondTab, 'g'), '${2:second}')
2748
.replace(new RegExp(SnippetPlaceholders.ThirdTab, 'g'), '${3:third}')
28-
.replace(new RegExp(SnippetPlaceholders.LastTab, 'g'), '$0')
29-
.replace(
30-
new RegExp(SnippetPlaceholders.TypeInterface, 'g'),
31-
extensionConfig.typescriptComponentPropsStatePrefix || 'type',
32-
);
49+
.replace(new RegExp(SnippetPlaceholders.LastTab, 'g'), '$0');
3350

3451
export const revertSnippetPlaceholders = (snippetString: string) =>
3552
String(snippetString)
@@ -42,37 +59,15 @@ export const revertSnippetPlaceholders = (snippetString: string) =>
4259
.replace(new RegExp(/\${3:third}/, 'g'), SnippetPlaceholders.ThirdTab)
4360
.replace(new RegExp(/\$0/, 'g'), SnippetPlaceholders.LastTab);
4461

45-
let prettierConfig: prettier.Options | null;
46-
47-
prettier
48-
.resolveConfig('', { editorconfig: true })
49-
.then((config) => (prettierConfig = config));
50-
51-
export const getPrettierConfig = (): Options => {
52-
const settings = workspace.getConfiguration(
53-
'reactSnippets.settings',
54-
) as unknown as ExtensionSettings;
55-
56-
return {
57-
semi: settings.semiColons,
58-
singleQuote: settings.quotes,
59-
tabWidth: settings.tabWidth,
60-
parser: 'typescript',
61-
...(settings.prettierEnabled && prettierConfig),
62-
};
62+
export const formatSnippet = (string: string) => {
63+
const prettierConfig = getPrettierConfig();
64+
return prettier.format(string, prettierConfig);
6365
};
6466

65-
export const parseSnippet = (body: string[]) => {
66-
const workspaceConfig = workspace.getConfiguration(
67-
'reactSnippets',
68-
) as unknown as ExtensionSettings;
67+
export const parseSnippet = (body: string | string[]) => {
6968
const snippetBody = typeof body === 'string' ? body : body.join('\n');
7069

71-
const prettierConfig = getPrettierConfig();
72-
const parsedBody = prettier.format(
73-
revertSnippetPlaceholders(snippetBody),
74-
prettierConfig,
70+
return replaceSnippetPlaceholders(
71+
formatSnippet(revertSnippetPlaceholders(snippetBody)),
7572
);
76-
77-
return replaceSnippetPlaceholders(parsedBody, workspaceConfig);
7873
};

src/index.ts

+11-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
commands,
33
ConfigurationChangeEvent,
44
ExtensionContext,
5-
window,
65
workspace,
76
} from 'vscode';
87

@@ -12,23 +11,19 @@ import snippetSearch from './snippetSearch';
1211
const showRestartMessage = async ({
1312
affectsConfiguration,
1413
}: ConfigurationChangeEvent) => {
15-
let timeoutId: NodeJS.Timeout | undefined;
1614
if (affectsConfiguration('reactSnippets')) {
1715
await generateSnippets();
18-
timeoutId && clearTimeout(timeoutId);
19-
timeoutId = setTimeout(() => {
20-
window
21-
.showWarningMessage(
22-
'React Snippets: Please restart VS Code to apply snippet formatting changes',
23-
'Restart VS Code',
24-
'Ignore',
25-
)
26-
.then((action?: string) => {
27-
if (action === 'Restart VS Code') {
28-
commands.executeCommand('workbench.action.reloadWindow');
29-
}
30-
});
31-
}, 3000);
16+
// window
17+
// .showWarningMessage(
18+
// 'React Snippets: Please restart VS Code to apply snippet formatting changes',
19+
// 'Restart VS Code',
20+
// 'Ignore',
21+
// )
22+
// .then((action?: string) => {
23+
// if (action === 'Restart VS Code') {
24+
// commands.executeCommand('workbench.action.reloadWindow');
25+
// }
26+
// });
3227
}
3328
};
3429

0 commit comments

Comments
 (0)