Skip to content

Commit ff84f0f

Browse files
Damian SznajderDamian Sznajder
Damian Sznajder
authored and
Damian Sznajder
committed
feat: properly generate snippets from search
1 parent 1f0d717 commit ff84f0f

File tree

6 files changed

+117
-112
lines changed

6 files changed

+117
-112
lines changed

README.md

+60-66
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ React 17 is currently supported by `_` prefix.
137137
| `ssf→` | `this.setState((state, props) => return { })` |
138138
| `props→` | `this.props.propName` |
139139
| `state→` | `this.state.stateName` |
140-
| `rcontext→` | `const $1 = React.createContext()` |
141-
| `cref→` | `this.$1Ref = React.createRef()` |
140+
| `rcontext→` | `const $1 = React.createContext()` |
141+
| `cref→` | `this.$1Ref = React.createRef()` |
142142
| `fref→` | `const ref = React.createRef()` |
143143
| `bnd→` | `this.methodName = this.methodName.bind(this)` |
144144
@@ -679,117 +679,111 @@ it("should $1", () => {
679679
### `stest`
680680
681681
```javascript
682-
import React from 'react'
683-
import renderer from 'react-test-renderer'
682+
import React from "react";
683+
import renderer from "react-test-renderer";
684684

685-
import { $1 } from '../$1'
685+
import { $1 } from "../$1";
686686

687-
describe('<$1 />', () => {
688-
const defaultProps = {}
689-
const wrapper = renderer.create(<$1 {...defaultProps} />)
687+
describe("<$1 />", () => {
688+
const defaultProps = {};
689+
const wrapper = renderer.create(<$1 {...defaultProps} />);
690690

691-
test('render', () => {
692-
expect(wrapper).toMatchSnapshot()
693-
})
694-
})
691+
test("render", () => {
692+
expect(wrapper).toMatchSnapshot();
693+
});
694+
});
695695
```
696696
697697
### `srtest`
698698
699699
```javascript
700-
import React from 'react'
701-
import renderer from 'react-test-renderer'
702-
import { Provider } from 'react-redux'
700+
import React from "react";
701+
import renderer from "react-test-renderer";
702+
import { Provider } from "react-redux";
703703

704-
import store from 'src/store'
705-
import { $1 } from '../$1'
704+
import store from "src/store";
705+
import { $1 } from "../$1";
706706

707-
describe('<$1 />', () => {
708-
const defaultProps = {}
707+
describe("<$1 />", () => {
708+
const defaultProps = {};
709709
const wrapper = renderer.create(
710710
<Provider store={store}>
711-
<${1TM_FILENAME_BASE}} {...defaultProps} />)
711+
<$1 {...defaultProps} />)
712712
</Provider>,
713-
)
713+
);
714714

715-
test('render', () => {
716-
expect(wrapper).toMatchSnapshot()
717-
})
718-
})
715+
test("render", () => {
716+
expect(wrapper).toMatchSnapshot();
717+
});
718+
});
719719
```
720720
721721
### `sntest`
722722
723723
```javascript
724-
import 'react-native'
725-
import React from 'react'
726-
import renderer from 'react-test-renderer'
727-
728-
import $1 from '../$1'
724+
import "react-native";
725+
import React from "react";
726+
import renderer from "react-test-renderer";
729727

730-
describe('<$1 />', () => {
731-
const defaultProps = {
728+
import $1 from "../$1";
732729

733-
}
730+
describe("<$1 />", () => {
731+
const defaultProps = {};
734732

735-
const wrapper = renderer.create(<$1 {...defaultProps} />)
733+
const wrapper = renderer.create(<$1 {...defaultProps} />);
736734

737-
test('render', () => {
738-
expect(wrapper).toMatchSnapshot()
739-
})
740-
})
735+
test("render", () => {
736+
expect(wrapper).toMatchSnapshot();
737+
});
738+
});
741739
```
742740
743741
### `snrtest`
744742
745743
```javascript
746-
import 'react-native'
747-
import React from 'react'
748-
import renderer from 'react-test-renderer'
749-
import { Provider } from 'react-redux'
744+
import "react-native";
745+
import React from "react";
746+
import renderer from "react-test-renderer";
747+
import { Provider } from "react-redux";
750748

751-
import store from 'src/store/configureStore'
752-
import $1 from '../$1'
749+
import store from "src/store/configureStore";
750+
import $1 from "../$1";
753751

754-
describe('<$1 />', () => {
755-
const defaultProps = {}
752+
describe("<$1 />", () => {
753+
const defaultProps = {};
756754
const wrapper = renderer.create(
757755
<Provider store={store}>
758756
<$1 {...defaultProps} />
759757
</Provider>,
760-
)
758+
);
761759

762-
test('render', () => {
763-
expect(wrapper).toMatchSnapshot()
764-
})
765-
})
760+
test("render", () => {
761+
expect(wrapper).toMatchSnapshot();
762+
});
763+
});
766764
```
767765
768766
### `hocredux`
769767
770768
```javascript
771-
import React from 'react'
772-
import PropTypes from 'prop-types'
773-
import { connect } from 'react-redux'
774-
775-
export const mapStateToProps = state => ({
776-
777-
})
769+
import React from "react";
770+
import PropTypes from "prop-types";
771+
import { connect } from "react-redux";
778772

779-
export const mapDispatchToProps = {
773+
export const mapStateToProps = (state) => ({});
780774

781-
}
775+
export const mapDispatchToProps = {};
782776

783777
export const $1 = (WrappedComponent) => {
784-
const hocComponent = ({ ...props }) => <WrappedComponent {...props} />
778+
const hocComponent = ({ ...props }) => <WrappedComponent {...props} />;
785779

786-
hocComponent.propTypes = {
787-
}
780+
hocComponent.propTypes = {};
788781

789-
return hocComponent
790-
}
782+
return hocComponent;
783+
};
791784

792-
export default WrapperComponent => connect(mapStateToProps, mapDispatchToProps)($1(WrapperComponent))
785+
export default (WrapperComponent) =>
786+
connect(mapStateToProps, mapDispatchToProps)($1(WrapperComponent));
793787
```
794788
795789
### `hoc`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "es7-react-js-snippets",
33
"displayName": "ES7+ React/Redux/React-Native snippets",
44
"description": "Extensions for React (React-Native) and Redux in JS/TS with ES7+ syntax. Customizable. Built-in integration with prettier.",
5-
"version": "3.2.0",
5+
"version": "4.0.0",
66
"publisher": "dsznajder",
77
"icon": "images/logo.png",
88
"browser": "./lib/index.js",

src/generateSnippets.ts

+5-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import reactNativeSnippets, {
99
import typescriptSnippets, {
1010
TypescriptSnippet,
1111
} from "./sourceSnippets/typescript";
12-
import { ExtensionSettings } from "./helpers";
12+
import { ExtensionSettings, replaceSnippetPlaceholders } from "./helpers";
1313
import reduxSnippets, { ReduxSnippet } from "./sourceSnippets/redux";
1414
import componentsSnippets, {
1515
ComponentsSnippet,
@@ -19,7 +19,6 @@ import propTypesSnippets, {
1919
PropTypesSnippet,
2020
} from "./sourceSnippets/propTypes";
2121
import testsSnippets, { TestsSnippet } from "./sourceSnippets/tests";
22-
import { SnippetPlaceholderMapping, SnippetPlaceholders } from "./types";
2322

2423
type SnippetKeys =
2524
| HooksSnippet["key"]
@@ -71,31 +70,10 @@ const getSnippets = () => {
7170
return acc;
7271
}, {} as Snippets);
7372

74-
const jsonSnippets = String(JSON.stringify(snippets, null, 2))
75-
.replace(
76-
new RegExp(SnippetPlaceholders.FileName, "g"),
77-
SnippetPlaceholderMapping[SnippetPlaceholders.FileName],
78-
)
79-
.replace(
80-
new RegExp(SnippetPlaceholders.FirstTab, "g"),
81-
SnippetPlaceholderMapping[SnippetPlaceholders.FirstTab],
82-
)
83-
.replace(
84-
new RegExp(SnippetPlaceholders.SecondTab, "g"),
85-
SnippetPlaceholderMapping[SnippetPlaceholders.SecondTab],
86-
)
87-
.replace(
88-
new RegExp(SnippetPlaceholders.ThirdTab, "g"),
89-
SnippetPlaceholderMapping[SnippetPlaceholders.ThirdTab],
90-
)
91-
.replace(
92-
new RegExp(SnippetPlaceholders.LastTab, "g"),
93-
SnippetPlaceholderMapping[SnippetPlaceholders.LastTab],
94-
)
95-
.replace(
96-
new RegExp(SnippetPlaceholders.TypeInterface, "g"),
97-
config.typescriptComponentPropsStatePrefix || "type",
98-
);
73+
const jsonSnippets = replaceSnippetPlaceholders(
74+
JSON.stringify(snippets, null, 2),
75+
config,
76+
);
9977

10078
snippetsCache = jsonSnippets;
10179
return jsonSnippets;
@@ -104,7 +82,6 @@ const getSnippets = () => {
10482
const generateSnippets = () => {
10583
console.time("generate");
10684
const jsonSnippets = getSnippets();
107-
console.log(jsonSnippets);
10885
console.timeEnd("generate");
10986

11087
writeFileSync(__dirname + "/snippets/generated.json", jsonSnippets);

src/helpers.ts

+50-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import prettier, { Options } from "prettier";
22
import { workspace } from "vscode";
3+
import { SnippetPlaceholders } from "./types";
34

45
export type ExtensionSettings = {
56
prettierEnabled: boolean;
@@ -11,6 +12,35 @@ export type ExtensionSettings = {
1112
typescriptComponentPropsStatePrefix: "type" | "interface";
1213
};
1314

15+
export const replaceSnippetPlaceholders = (
16+
snippetString: string,
17+
extensionConfig: ExtensionSettings,
18+
) =>
19+
String(snippetString)
20+
.replace(
21+
new RegExp(SnippetPlaceholders.FileName, "g"),
22+
"${1:${TM_FILENAME_BASE}}",
23+
)
24+
.replace(new RegExp(SnippetPlaceholders.FirstTab, "g"), "${1:first}")
25+
.replace(new RegExp(SnippetPlaceholders.SecondTab, "g"), "${2:second}")
26+
.replace(new RegExp(SnippetPlaceholders.ThirdTab, "g"), "${3:third}")
27+
.replace(new RegExp(SnippetPlaceholders.LastTab, "g"), "$0")
28+
.replace(
29+
new RegExp(SnippetPlaceholders.TypeInterface, "g"),
30+
extensionConfig.typescriptComponentPropsStatePrefix || "type",
31+
);
32+
33+
export const revertSnippetPlaceholders = (snippetString: string) =>
34+
String(snippetString)
35+
.replace(
36+
new RegExp(/\${1:${TM_FILENAME_BASE}}/, "g"),
37+
SnippetPlaceholders.FileName,
38+
)
39+
.replace(new RegExp(/\${1:first}/, "g"), SnippetPlaceholders.FirstTab)
40+
.replace(new RegExp(/\${2:second}/, "g"), SnippetPlaceholders.SecondTab)
41+
.replace(new RegExp(/\${3:third}/, "g"), SnippetPlaceholders.ThirdTab)
42+
.replace(new RegExp(/\$0/, "g"), SnippetPlaceholders.LastTab);
43+
1444
export const getPrettierConfig = async (): Promise<Options> => {
1545
const settings = workspace.getConfiguration(
1646
"esReactSnippets.settings",
@@ -23,19 +53,34 @@ export const getPrettierConfig = async (): Promise<Options> => {
2353
return {
2454
semi: settings.semiColons,
2555
singleQuote: settings.quotes,
26-
tabWidth: 2,
56+
tabWidth: settings.tabWidth,
2757
...(settings.prettierEnabled && prettierConfig),
2858
};
2959
};
3060

31-
export const parseSnippet = (snippet: {
61+
export const parseSnippet = async (snippet: {
3262
id: number;
3363
description: any;
3464
label: string;
3565
value: string;
3666
body: string | string[];
3767
}) => {
38-
return typeof snippet.body === "string"
39-
? snippet.body
40-
: snippet.body.join("\n");
68+
const workspaceConfig = workspace.getConfiguration(
69+
"esReactSnippets",
70+
) as unknown as ExtensionSettings;
71+
const snippetBody =
72+
typeof snippet.body === "string" ? snippet.body : snippet.body.join("\n");
73+
74+
const prettierConfig = await getPrettierConfig();
75+
const parsedBody = prettier.format(
76+
revertSnippetPlaceholders(snippetBody),
77+
prettierConfig,
78+
);
79+
80+
console.log(
81+
parsedBody,
82+
replaceSnippetPlaceholders(parsedBody, workspaceConfig),
83+
);
84+
85+
return replaceSnippetPlaceholders(parsedBody, workspaceConfig);
4186
};

src/snippetSearch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const snippetSearch = async () => {
2525
placeHolder: "Search snippet by prefix or description",
2626
});
2727

28-
const body = rawSnippet ? parseSnippet(rawSnippet) : "";
28+
const body = rawSnippet ? await parseSnippet(rawSnippet) : "";
2929

3030
if (activeTextEditor) {
3131
activeTextEditor.insertSnippet(new SnippetString(body));

src/types.ts

-11
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,3 @@ export enum SnippetPlaceholders {
1313
LastTab = "LAST_TAB",
1414
TypeInterface = "TYPE_INTERFACE",
1515
}
16-
17-
export const SnippetPlaceholderMapping: {
18-
[key in SnippetPlaceholders]: string;
19-
} = {
20-
[SnippetPlaceholders.FileName]: "${1:TM_FILENAME_BASE}",
21-
[SnippetPlaceholders.FirstTab]: "$1",
22-
[SnippetPlaceholders.SecondTab]: "$2",
23-
[SnippetPlaceholders.ThirdTab]: "$3",
24-
[SnippetPlaceholders.LastTab]: "$0",
25-
[SnippetPlaceholders.TypeInterface]: "TYPE_INTERFACE",
26-
};

0 commit comments

Comments
 (0)