Skip to content

Commit 2a4d3eb

Browse files
committed
Format code using Prettier
1 parent 234a9ba commit 2a4d3eb

9 files changed

+287
-173
lines changed

.eslintrc.json

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
"sourceType": "module"
77
},
88
"plugins": [
9-
"@typescript-eslint"
9+
"@typescript-eslint",
10+
"prettier"
11+
],
12+
"extends": [
13+
"eslint:recommended",
14+
"plugin:@typescript-eslint/eslint-recommended",
15+
"plugin:@typescript-eslint/recommended",
16+
"prettier"
1017
],
11-
"rules": {
12-
"@typescript-eslint/naming-convention": "warn",
13-
"@typescript-eslint/semi": "warn",
14-
"curly": "warn",
15-
"eqeqeq": "warn",
16-
"no-throw-literal": "warn",
17-
"semi": "off"
18-
},
1918
"ignorePatterns": [
2019
"out",
2120
"dist",
2221
"**/*.d.ts"
2322
]
24-
}
23+
}

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"printWidth": 80
3+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ A git commit message highlighter being nicer than the built-in one.
3030
- Make an icon
3131
- Move all tests under the same top subdirectory
3232
- `npm test` in CI
33+
- Validate code formatting in CI
3334
- Mark commit message subject line in bold, it _is_ a heading after all.
3435
- Add a print margin. Can we [configure the default setting for our
3536
language](https://stackoverflow.com/questions/42607666/how-to-add-a-right-margin-to-the-visual-studio-code-editor)?

package-lock.json

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

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"publisher": "walles",
2020
"main": "./out/extension.js",
2121
"scripts": {
22+
"format": "prettier --config .prettierrc 'src/**/*.ts' --write",
2223
"compile": "tsc -p ./",
2324
"lint": "eslint src --ext ts",
2425
"package": "vsce package",
@@ -55,12 +56,15 @@
5556
"@vscode/test-electron": "^2.2.0",
5657
"@vscode/vsce": "^2.16.0",
5758
"eslint": "^8.28.0",
59+
"eslint-config-prettier": "^8.6.0",
60+
"eslint-plugin-prettier": "^4.2.1",
5861
"glob": "^8.0.3",
5962
"mocha": "^10.1.0",
63+
"prettier": "^2.8.3",
6064
"typescript": "^4.9.3",
6165
"vscode-tmgrammar-test": "^0.1.1"
6266
},
6367
"engines": {
6468
"vscode": "^1.74.0"
6569
}
66-
}
70+
}

src/extension.ts

+60-50
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,92 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
3-
import * as vscode from 'vscode';
3+
import * as vscode from "vscode";
44

55
const maxSubjectLineLength = 72;
6-
const maxSubjectLineLengthUrl = vscode.Uri.parse("https://www.gitkraken.com/learn/git/best-practices/git-commit-message");
6+
const maxSubjectLineLengthUrl = vscode.Uri.parse(
7+
"https://www.gitkraken.com/learn/git/best-practices/git-commit-message"
8+
);
79

810
/** Subset of vscode.TextLine, for simplifying test writing. */
911
export interface TextLineLite {
10-
text: string;
12+
text: string;
1113
}
1214

1315
/** Subset of vscode.TextDocument, for simplifying test writing. */
1416
export interface TextDocumentLite {
15-
lineCount: number;
16-
lineAt(line: number): TextLineLite;
17+
lineCount: number;
18+
lineAt(line: number): TextLineLite;
1719
}
1820

1921
let diagnosticCollection: vscode.DiagnosticCollection;
2022

2123
// This method is called when your extension is activated
2224
// Your extension is activated the very first time the command is executed
2325
export function activate(context: vscode.ExtensionContext) {
24-
console.log('Git Commit Message Plus says hello!');
25-
26-
diagnosticCollection = vscode.languages.createDiagnosticCollection('git-commit-message');
27-
context.subscriptions.push(diagnosticCollection);
28-
29-
const documentChangeListener = vscode.workspace.onDidChangeTextDocument(event => {
30-
const doc = event.document;
31-
if (doc.languageId !== 'git-commit') {
32-
return;
33-
}
34-
35-
diagnosticCollection.set(doc.uri, getDiagnostics(doc));
36-
}, null, context.subscriptions);
37-
context.subscriptions.push(documentChangeListener);
38-
39-
// FIXME: Make sure linting is triggered when we open a commit message, not
40-
// just when it changes. I tried to react to onDidOpenTextDocument(), but
41-
// documents seem to default to plain-text when loaded so that didn't work.
26+
console.log("Git Commit Message Plus says hello!");
27+
28+
diagnosticCollection =
29+
vscode.languages.createDiagnosticCollection("git-commit-message");
30+
context.subscriptions.push(diagnosticCollection);
31+
32+
const documentChangeListener = vscode.workspace.onDidChangeTextDocument(
33+
(event) => {
34+
const doc = event.document;
35+
if (doc.languageId !== "git-commit") {
36+
return;
37+
}
38+
39+
diagnosticCollection.set(doc.uri, getDiagnostics(doc));
40+
},
41+
null,
42+
context.subscriptions
43+
);
44+
context.subscriptions.push(documentChangeListener);
45+
46+
// FIXME: Make sure linting is triggered when we open a commit message, not
47+
// just when it changes. I tried to react to onDidOpenTextDocument(), but
48+
// documents seem to default to plain-text when loaded so that didn't work.
4249
}
4350

4451
// This method is called when your extension is deactivated
4552
export function deactivate() {
46-
console.log('Git Commit Message Plus says good bye!');
53+
console.log("Git Commit Message Plus says good bye!");
4754
}
4855

4956
function getDiagnostics(doc: TextDocumentLite): vscode.Diagnostic[] {
50-
if (doc.lineCount < 1) {
51-
return [];
52-
}
53-
54-
let returnMe: vscode.Diagnostic[] = [];
55-
56-
const firstLine = doc.lineAt(0).text;
57-
if (firstLine.length > maxSubjectLineLength) {
58-
const range = new vscode.Range(new vscode.Position(0, 72), new vscode.Position(0, firstLine.length));
59-
60-
let diagnostic = new vscode.Diagnostic(
61-
range,
62-
`Keep the subject line to at most ${maxSubjectLineLength} characters`,
63-
vscode.DiagnosticSeverity.Warning
64-
);
65-
diagnostic.code = {
66-
"target": maxSubjectLineLengthUrl,
67-
"value": "Git Commit Message Structure"
68-
};
69-
70-
returnMe.push(diagnostic);
71-
}
72-
73-
return returnMe;
57+
if (doc.lineCount < 1) {
58+
return [];
59+
}
60+
61+
const returnMe: vscode.Diagnostic[] = [];
62+
63+
const firstLine = doc.lineAt(0).text;
64+
if (firstLine.length > maxSubjectLineLength) {
65+
const range = new vscode.Range(
66+
new vscode.Position(0, 72),
67+
new vscode.Position(0, firstLine.length)
68+
);
69+
70+
const diagnostic = new vscode.Diagnostic(
71+
range,
72+
`Keep the subject line to at most ${maxSubjectLineLength} characters`,
73+
vscode.DiagnosticSeverity.Warning
74+
);
75+
diagnostic.code = {
76+
target: maxSubjectLineLengthUrl,
77+
value: "Git Commit Message Structure",
78+
};
79+
80+
returnMe.push(diagnostic);
81+
}
82+
83+
return returnMe;
7484
}
7585

7686
// Exports for testing
7787
//
7888
// Ref: https://stackoverflow.com/a/65422568/473672
7989
export const _private = {
80-
getDiagnostics,
81-
maxSubjectLineLengthUrl
90+
getDiagnostics,
91+
maxSubjectLineLengthUrl,
8292
};

src/test/runTest.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import * as path from 'path';
1+
import * as path from "path";
22

3-
import { runTests } from '@vscode/test-electron';
3+
import { runTests } from "@vscode/test-electron";
44

55
async function main() {
6-
try {
7-
// The folder containing the Extension Manifest package.json
8-
// Passed to `--extensionDevelopmentPath`
9-
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
6+
try {
7+
// The folder containing the Extension Manifest package.json
8+
// Passed to `--extensionDevelopmentPath`
9+
const extensionDevelopmentPath = path.resolve(__dirname, "../../");
1010

11-
// The path to test runner
12-
// Passed to --extensionTestsPath
13-
const extensionTestsPath = path.resolve(__dirname, './suite/index');
11+
// The path to test runner
12+
// Passed to --extensionTestsPath
13+
const extensionTestsPath = path.resolve(__dirname, "./suite/index");
1414

15-
// Download VS Code, unzip it and run the integration test
16-
await runTests({ extensionDevelopmentPath, extensionTestsPath });
17-
} catch (err) {
18-
console.error('Failed to run tests');
19-
process.exit(1);
20-
}
15+
// Download VS Code, unzip it and run the integration test
16+
await runTests({ extensionDevelopmentPath, extensionTestsPath });
17+
} catch (err) {
18+
console.error("Failed to run tests");
19+
process.exit(1);
20+
}
2121
}
2222

2323
main();

0 commit comments

Comments
 (0)