Skip to content

Commit 0fb49e8

Browse files
authored
Merge pull request #3 from mat2ja/release/v0.4.1
Release/v0.4.1
2 parents bf7d703 + 992b7b5 commit 0fb49e8

File tree

10 files changed

+87
-124
lines changed

10 files changed

+87
-124
lines changed

README.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
<br>
66

7-
🚧 *Still a work in progress. Some snippets may be changed or removed, and many more will be added.*
7+
🚧 *Still a work in progress. Some snippets may be changed or removed, and more will be added.*
88

99
## Features
1010
- Contains over **180** snippets
@@ -61,7 +61,7 @@ It's highly recommended to use these snippets along with Prettier/ESLint to have
6161

6262
### Assignments
6363

64-
<table>
64+
<table width="100%">
6565

6666
<tr>
6767
<td>Prefix</td>
@@ -180,7 +180,7 @@ const [$0] = ${1:array}
180180

181181
### Flow control
182182

183-
<table>
183+
<table width="100%">
184184

185185
<tr>
186186
<td>Prefix</td>
@@ -368,7 +368,7 @@ try {
368368

369369
### Functions
370370

371-
<table>
371+
<table width="100%">
372372

373373
<tr>
374374
<td>Prefix</td>
@@ -497,7 +497,7 @@ async ($1) => {
497497

498498
### Loops
499499

500-
<table>
500+
<table width="100%">
501501

502502
<tr>
503503
<td>Prefix</td>
@@ -606,7 +606,7 @@ while (${1:true}) {
606606

607607
### Classes
608608

609-
<table>
609+
<table width="100%">
610610

611611
<tr>
612612
<td>Prefix</td>
@@ -762,7 +762,7 @@ async ${1:name}($2) {
762762

763763
### Promises
764764

765-
<table>
765+
<table width="100%">
766766

767767
<tr>
768768
<td>Prefix</td>
@@ -907,7 +907,7 @@ Promise.any($1)
907907

908908
### Modules
909909

910-
<table>
910+
<table width="100%">
911911

912912
<tr>
913913
<td>Prefix</td>
@@ -1102,7 +1102,7 @@ export const ${1:name} = ($2) => {$0}
11021102

11031103
### Array methods
11041104

1105-
<table>
1105+
<table width="100%">
11061106

11071107
<tr>
11081108
<td>Prefix</td>
@@ -1263,7 +1263,7 @@ $1.filter(Boolean)
12631263

12641264
### Objects
12651265

1266-
<table>
1266+
<table width="100%">
12671267

12681268
<tr>
12691269
<td>Prefix</td>
@@ -1322,7 +1322,7 @@ Object.values($0)
13221322

13231323
### Returns
13241324

1325-
<table>
1325+
<table width="100%">
13261326

13271327
<tr>
13281328
<td>Prefix</td>
@@ -1371,7 +1371,7 @@ return ({$0})
13711371

13721372
### Operators, Expressions, Literals
13731373
Grouping them all together for now
1374-
<table>
1374+
<table width="100%">
13751375

13761376
<tr>
13771377
<td>Prefix</td>
@@ -1610,7 +1610,7 @@ ${$1}$0
16101610
16111611
### Console
16121612
1613-
<table>
1613+
<table width="100%">
16141614
16151615
<tr>
16161616
<td>Prefix</td>
@@ -1791,7 +1791,7 @@ console.warn('$1 ->', ${2:$1})
17911791
17921792
### Timers
17931793
1794-
<table>
1794+
<table width="100%">
17951795
17961796
<tr>
17971797
<td>Prefix</td>
@@ -1858,7 +1858,7 @@ process.nextTick(() => {
18581858
18591859
### JSON
18601860
1861-
<table>
1861+
<table width="100%">
18621862
18631863
<tr>
18641864
<td>Prefix</td>
@@ -1917,7 +1917,7 @@ typeof ${1:value} === 'string' ? value : JSON.stringify($1)
19171917
19181918
### DOM
19191919
1920-
<table>
1920+
<table width="100%">
19211921
19221922
<tr>
19231923
<td>Prefix</td>
@@ -2014,7 +2014,7 @@ ${1:emitter}.on('${2:event}', (${3:arguments}) => {
20142014
20152015
### Dates
20162016
2017-
<table>
2017+
<table width="100%">
20182018
20192019
<tr>
20202020
<td>Prefix</td>
@@ -2049,7 +2049,7 @@ Date.now()
20492049
20502050
### Testing
20512051
2052-
<table>
2052+
<table width="100%">
20532053
20542054
<tr>
20552055
<td>Prefix</td>
@@ -2187,7 +2187,7 @@ afterEach(() => {
21872187
21882188
### Types
21892189
2190-
<table>
2190+
<table width="100%">
21912191
21922192
<tr>
21932193
<td>Prefix</td>
@@ -2282,7 +2282,7 @@ isNan($0)
22822282
22832283
### Misc
22842284
2285-
<table>
2285+
<table width="100%">
22862286
22872287
<tr>
22882288
<td>Prefix</td>
@@ -2353,7 +2353,7 @@ import.meta.env.$0
23532353
23542354
### Uncategorized
23552355
Will be sorted into appropriate categories in the future.
2356-
<table>
2356+
<table width="100%">
23572357
23582358
<tr>
23592359
<td>Prefix</td>
@@ -2452,7 +2452,7 @@ Available only in .ts and .tsx files
24522452
24532453
### Declarations
24542454
2455-
<table>
2455+
<table width="100%">
24562456
24572457
<tr>
24582458
<td>Prefix</td>
@@ -2511,7 +2511,7 @@ const $1: ${2:object} = { $0 }
25112511
25122512
### Types
25132513
2514-
<table>
2514+
<table width="100%">
25152515
25162516
<tr>
25172517
<td>Prefix</td>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "modern-js-snippets",
33
"displayName": "Modern JavaScript Snippets ⚡",
4-
"version": "0.4.0",
4+
"version": "0.4.1",
55
"license": "MIT",
66
"description": "Code snippets for modern JavaScript & TypeScript",
77
"icon": "assets/icon.png",

src/app.ts

+34-24
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
import { parse } from "./deps.ts";
22
import { generateDocs, populateDocsBlock } from "./docs-gen/snippets.ts";
3-
import { languages } from "./snippets/app.ts";
3+
import { variants } from "./snippets/app.ts";
44
import {
55
convertToVscSnippet,
66
generateSnippets,
77
groupSnippets,
88
} from "./utils/snippets.ts";
99

10-
const flags = parse(Deno.args, {
11-
boolean: ["snippets", "docs"],
12-
default: { snippets: false, docs: false },
13-
});
10+
const main = () => {
11+
const flags = parse(Deno.args, {
12+
boolean: ["snippets", "docs"],
13+
default: { snippets: false, docs: false },
14+
});
15+
16+
if (!flags.snippets && !flags.docs) {
17+
return console.log(
18+
"Please specify at least one flag: --snippets or --docs",
19+
);
20+
}
21+
22+
// Convert XDefinitions to VscDefinitions, for every variant
23+
const variantsAsVsc = variants.map((variant) => {
24+
const snippetDefinitions = variant.snippetDefinitions
25+
.map((def) => ({
26+
...def,
27+
snippets: convertToVscSnippet(def.snippets),
28+
}));
29+
30+
return { ...variant, snippetDefinitions };
31+
});
1432

15-
if (!flags.snippets && !flags.docs) {
16-
console.log("Please specify at least one flag: --snippets or --docs");
17-
} else {
1833
if (flags.snippets) {
1934
console.log("\nGenerating snippets...");
20-
languages.forEach((language) => {
21-
const categorizedVscSnippets = language
22-
.snippetDefinitions.map(
23-
(item) => {
24-
const snippets = convertToVscSnippet(item.snippets);
25-
return { ...item, snippets };
26-
},
27-
);
28-
29-
const variantVscSnippet = groupSnippets(
30-
categorizedVscSnippets.map((item) => item.snippets),
35+
36+
variantsAsVsc.forEach((variant) => {
37+
const vscSnippetDict = groupSnippets(
38+
variant.snippetDefinitions.map((def) => def.snippets),
3139
);
32-
generateSnippets(language.fileExtension, variantVscSnippet);
40+
generateSnippets(variant.fileExtension, vscSnippetDict);
3341
});
3442
}
3543

36-
// TODO: probably better to make it generate from vsc json
37-
// pass in meta, and snippets converted to vsc format
44+
// important to know it generates docs off of defined xSnippets
45+
// so .code-snippets could be out of date if you haven't run --snippets
3846
if (flags.docs) {
3947
console.log("\nGenerating docs...");
40-
const docs = generateDocs(languages);
48+
const docs = generateDocs(variantsAsVsc);
4149
populateDocsBlock(docs);
4250
}
43-
}
51+
};
52+
53+
main();

src/docs-gen/snippets.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { replaceInFile } from "../deps.ts";
2-
import { XSnippetDefinition, XSnippetVariant } from "../models/app.ts";
2+
import { VscSnippetDefinition, VscSnippetVariant } from "../models/app.ts";
3+
import { parseMultiline, replaceSymbol } from "../utils/general.ts";
34
import {
45
$col,
56
$colCode,
@@ -18,11 +19,10 @@ type SnippetRow = {
1819
};
1920

2021
const snippetRow = ({ prefix, name, body }: SnippetRow) => {
21-
const parsedBody = Array.isArray(body) ? body.join("\n") : body;
2222
const cols = joinByNewLine([
2323
$colCode(prefix),
2424
$col(name),
25-
$colCodeBlock(parsedBody),
25+
$colCodeBlock(parseMultiline(body)),
2626
]);
2727

2828
return $row(cols);
@@ -35,30 +35,30 @@ const generateSnippetTable = (items: SnippetRow[]) => {
3535
return $table(headings, rows);
3636
};
3737

38-
const generateSnippetSection = (
39-
{ meta, snippets }: XSnippetDefinition,
40-
) => {
38+
const generateSnippetSection = ({ meta, snippets }: VscSnippetDefinition) => {
4139
const title = `### ${meta.title}`;
4240
const description = meta.description ?? "";
4341
const table = generateSnippetTable(
44-
Object.entries(snippets).map(([prefix, value]) => ({
45-
...value,
46-
prefix,
42+
Object.entries(snippets).map(([name, { body, prefix, description }]) => ({
43+
name: replaceSymbol(name),
44+
body,
45+
prefix: parseMultiline(prefix),
46+
description,
4747
})),
4848
);
4949

5050
return joinByNewLine([title, description, table, ""]);
5151
};
5252

53-
const generateVariantSection = (variant: XSnippetVariant) => {
53+
const generateVariantSection = (variant: VscSnippetVariant) => {
5454
const title = `## ${variant.label}`;
5555
const description = variant.description ?? "";
5656
const sections = variant.snippetDefinitions.map(generateSnippetSection);
5757

5858
return joinByNewLine([title, description, "", ...sections]);
5959
};
6060

61-
export const generateDocs = (variants: XSnippetVariant[]) => {
61+
export const generateDocs = (variants: VscSnippetVariant[]) => {
6262
return joinByDoubleNewLine(variants.map(generateVariantSection));
6363
};
6464

src/docs-gen/table-html.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const $headerRow = (headers: string[]) => {
3838

3939
export const $table = (headings: string[], rows: string[]) => {
4040
return joinByNewLine([
41-
"<table>",
41+
'<table width="100%">',
4242
$headerRow(headings),
4343
joinByNewLine(rows),
4444
"</table>",

0 commit comments

Comments
 (0)