Skip to content

Commit efc7a1d

Browse files
committed
feat: format
1 parent 53cf96d commit efc7a1d

File tree

10 files changed

+105
-61
lines changed

10 files changed

+105
-61
lines changed

.prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"printWidth": 80
6+
}

package-lock.json

+19-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-7
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,22 @@
9696
"check-types": "tsc --noEmit",
9797
"lint": "eslint src --ext ts",
9898
"test": "vscode-test",
99-
"build": "vsce package"
99+
"build": "vsce package",
100+
"format": "prettier --write \"{src,test}/**/*.ts\"",
101+
"postformat": "npm run lint -- --fix"
100102
},
101103
"devDependencies": {
102-
"@types/vscode": "^1.92.0",
103104
"@types/mocha": "^10.0.7",
104105
"@types/node": "20.x",
106+
"@types/vscode": "^1.92.0",
105107
"@typescript-eslint/eslint-plugin": "^7.14.1",
106-
"@typescript-eslint/parser": "^7.11.0",
107-
"eslint": "^8.57.0",
108+
"@typescript-eslint/parser": "^7.11.0",
109+
"@vscode/test-cli": "^0.0.9",
110+
"@vscode/test-electron": "^2.4.0",
108111
"esbuild": "^0.21.5",
112+
"eslint": "^8.57.0",
109113
"npm-run-all": "^4.1.5",
110-
"typescript": "^5.4.5",
111-
"@vscode/test-cli": "^0.0.9",
112-
"@vscode/test-electron": "^2.4.0"
114+
"prettier": "^3.3.3",
115+
"typescript": "^5.4.5"
113116
}
114117
}

src/helper/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './snippets.helper';
1+
export * from './snippets.helper';

src/helper/snippets.helper.ts

+35-30
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { Snippets } from '../interface';
55
const extractAllRelatedHooksFromSnippets = (content: string[]): string[] => {
66
const hooks: string[] = [];
77
const hookPattern = /use[A-Z]\w*/g;
8-
content.forEach(line => {
8+
content.forEach((line) => {
99
const matches = line.match(hookPattern);
1010
if (matches) {
11-
matches.forEach(match => {
11+
matches.forEach((match) => {
1212
if (!hooks.includes(match)) {
1313
hooks.push(match);
1414
}
@@ -19,33 +19,38 @@ const extractAllRelatedHooksFromSnippets = (content: string[]): string[] => {
1919
return hooks.length > 0 ? hooks : [''];
2020
};
2121

22-
2322
export const loadSnippets = (dirPath: string): Snippets => {
24-
const snippets: Snippets = {};
25-
26-
fs.readdirSync(dirPath).forEach(file => {
27-
const filePath = joinPath(dirPath, file);
28-
const snippetName = path.basename(file, '.template');
29-
const content = fs.readFileSync(filePath, 'utf8').split('\n');
30-
31-
snippets[snippetName] = {
32-
prefix: snippetName,
33-
body: content,
34-
description: `A custom hook for ${snippetName.replace(/^use/, '').toLowerCase()}.`,
35-
relatedHooks: extractAllRelatedHooksFromSnippets(content).filter(d => d !== snippetName),
36-
relatedCustomHooks: [],
37-
relatedReactHooks: [],
38-
};
39-
});
40-
41-
for (const snippet of Object.keys(snippets)){
42-
snippets[snippet].relatedCustomHooks = snippets[snippet].relatedHooks.filter(h => Object.keys(snippets).includes(h));
43-
snippets[snippet].relatedReactHooks = snippets[snippet].relatedHooks.filter(h => !Object.keys(snippets).includes(h));
44-
}
45-
46-
return snippets;
47-
};
23+
const snippets: Snippets = {};
24+
25+
fs.readdirSync(dirPath).forEach((file) => {
26+
const filePath = joinPath(dirPath, file);
27+
const snippetName = path.basename(file, '.template');
28+
const content = fs.readFileSync(filePath, 'utf8').split('\n');
29+
30+
snippets[snippetName] = {
31+
prefix: snippetName,
32+
body: content,
33+
description: `A custom hook for ${snippetName.replace(/^use/, '').toLowerCase()}.`,
34+
relatedHooks: extractAllRelatedHooksFromSnippets(content).filter(
35+
(d) => d !== snippetName,
36+
),
37+
relatedCustomHooks: [],
38+
relatedReactHooks: [],
39+
};
40+
});
4841

49-
export const joinPath = (dirPath: string, file: string) => {
50-
return path.join(dirPath, file);
51-
};
42+
for (const snippet of Object.keys(snippets)) {
43+
snippets[snippet].relatedCustomHooks = snippets[
44+
snippet
45+
].relatedHooks.filter((h) => Object.keys(snippets).includes(h));
46+
snippets[snippet].relatedReactHooks = snippets[snippet].relatedHooks.filter(
47+
(h) => !Object.keys(snippets).includes(h),
48+
);
49+
}
50+
51+
return snippets;
52+
};
53+
54+
export const joinPath = (dirPath: string, file: string) => {
55+
return path.join(dirPath, file);
56+
};

src/interface/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ export interface Snippet {
22
prefix: string;
33
body: string[];
44
description: string;
5-
relatedHooks: string[]
6-
relatedCustomHooks: string[]
7-
relatedReactHooks : string []
5+
relatedHooks: string[];
6+
relatedCustomHooks: string[];
7+
relatedReactHooks: string[];
88
}
99

10-
export type Snippets = Record<string, Snippet>;
10+
export type Snippets = Record<string, Snippet>;

src/languageServer.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@ export function registerCompletionProvider(context: vscode.ExtensionContext) {
1919
for (const [key, value] of Object.entries(snippets)) {
2020
const completionItem = new vscode.CompletionItem(
2121
key,
22-
vscode.CompletionItemKind.Snippet
22+
vscode.CompletionItemKind.Snippet,
23+
);
24+
completionItem.insertText = new vscode.SnippetString(
25+
value.body.join('\n'),
26+
);
27+
completionItem.documentation = new vscode.MarkdownString(
28+
value.description,
2329
);
24-
completionItem.insertText = new vscode.SnippetString(value.body.join('\n'));
25-
completionItem.documentation = new vscode.MarkdownString(value.description);
2630
completionItem.detail = `Snippet: ${value.prefix}`;
2731
completionItem.command = {
2832
command: 'extension.showSnippetMessage',
2933
title: 'Show Snippet Message',
30-
arguments: [key, value]
34+
arguments: [key, value],
3135
};
3236
completionItems.push(completionItem);
3337
}
3438

3539
return completionItems;
36-
}
40+
},
3741
},
38-
'use'
42+
'use',
3943
);
4044

4145
context.subscriptions.push(provider);
@@ -48,14 +52,14 @@ export function registerCompletionProvider(context: vscode.ExtensionContext) {
4852
'extension.showSnippetMessage',
4953
(snippetName: string, snippet: Snippet) => {
5054
let message = `"${snippetName}" snippet:`;
51-
if(snippet.relatedCustomHooks.length > 0){
55+
if (snippet.relatedCustomHooks.length > 0) {
5256
message += ` The custom hooks ${snippet.relatedCustomHooks} are available in our library. Just type ${snippet.relatedCustomHooks} to use them, and make sure to include them to avoid any errors.`;
5357
}
54-
if(snippet.relatedReactHooks.length > 0) {
58+
if (snippet.relatedReactHooks.length > 0) {
5559
message += ` You can add ${snippet.relatedReactHooks} from react`;
5660
}
5761
vscode.window.showInformationMessage(message);
58-
}
62+
},
5963
);
6064
context.subscriptions.push(showSnippetMessageCommand);
6165
}

src/test/extension.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
22
import * as assert from 'assert';
33
describe('Extension Tests', () => {
4-
54
it('should activate extension', async () => {
6-
const extension = vscode.extensions.getExtension('your-publisher.your-extension-id');
5+
const extension = vscode.extensions.getExtension(
6+
'your-publisher.your-extension-id',
7+
);
78
assert.ok(extension, 'Extension is not installed.');
89
assert.strictEqual(extension.isActive, true, 'Extension did not activate.');
910
});

src/test/snippets/javascript/useFetch.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ const expectedUseFetchSnippet = {
2525
' }, [url]);',
2626
'',
2727
' return [data, loading, error];',
28-
'};'
28+
'};',
2929
],
30-
description: 'A custom hook for fetching data from a URL in JavaScript.'
30+
description: 'A custom hook for fetching data from a URL in JavaScript.',
3131
};
3232

3333
describe('JavaScript useFetch Snippet Tests', () => {
3434
it('should load the JavaScript useFetch snippet correctly', () => {
3535
const snippets = loadSnippets(javascriptSnippetsDir);
3636

37-
assert.deepStrictEqual(snippets['useFetch'], expectedUseFetchSnippet, 'JavaScript useFetch snippet did not load correctly');
37+
assert.deepStrictEqual(
38+
snippets['useFetch'],
39+
expectedUseFetchSnippet,
40+
'JavaScript useFetch snippet did not load correctly',
41+
);
3842
});
3943
});

src/test/snippets/typescript/useFetch.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ const expectedUseFetchSnippet = {
2525
' }, [url]);',
2626
'',
2727
' return [data, loading, error];',
28-
'};'
28+
'};',
2929
],
30-
description: 'A custom hook for fetching data from a URL in TypeScript.'
30+
description: 'A custom hook for fetching data from a URL in TypeScript.',
3131
};
3232

3333
describe('TypeScript useFetch Snippet Tests', () => {
3434
it('should load the TypeScript useFetch snippet correctly', () => {
3535
const snippets = loadSnippets(typescriptSnippetsDir);
3636

37-
assert.deepStrictEqual(snippets['useFetch'], expectedUseFetchSnippet, 'TypeScript useFetch snippet did not load correctly');
37+
assert.deepStrictEqual(
38+
snippets['useFetch'],
39+
expectedUseFetchSnippet,
40+
'TypeScript useFetch snippet did not load correctly',
41+
);
3842
});
3943
});

0 commit comments

Comments
 (0)