Skip to content

Commit 2c02d31

Browse files
authored
build: add pre-commit package for linting (#823)
* build: add incremental clang-format checks * build: add pre-commit package for linting
1 parent 1b52c28 commit 2c02d31

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

.clang-format

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -1
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: false
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: true
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Inline
15+
AllowShortIfStatementsOnASingleLine: true
16+
AllowShortLoopsOnASingleLine: true
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: None
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: true
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: false
28+
AfterNamespace: false
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
AfterExternBlock: false
33+
BeforeCatch: false
34+
BeforeElse: false
35+
IndentBraces: false
36+
SplitEmptyFunction: true
37+
SplitEmptyRecord: true
38+
SplitEmptyNamespace: true
39+
BreakBeforeBinaryOperators: None
40+
BreakBeforeBraces: Attach
41+
BreakBeforeInheritanceComma: false
42+
BreakBeforeTernaryOperators: true
43+
BreakConstructorInitializersBeforeComma: false
44+
BreakConstructorInitializers: BeforeColon
45+
BreakAfterJavaFieldAnnotations: false
46+
BreakStringLiterals: true
47+
ColumnLimit: 80
48+
CommentPragmas: '^ IWYU pragma:'
49+
CompactNamespaces: false
50+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
51+
ConstructorInitializerIndentWidth: 4
52+
ContinuationIndentWidth: 4
53+
Cpp11BracedListStyle: true
54+
DerivePointerAlignment: false
55+
DisableFormat: false
56+
ExperimentalAutoDetectBinPacking: false
57+
FixNamespaceComments: true
58+
ForEachMacros:
59+
- foreach
60+
- Q_FOREACH
61+
- BOOST_FOREACH
62+
IncludeBlocks: Preserve
63+
IncludeCategories:
64+
- Regex: '^<ext/.*\.h>'
65+
Priority: 2
66+
- Regex: '^<.*\.h>'
67+
Priority: 1
68+
- Regex: '^<.*'
69+
Priority: 2
70+
- Regex: '.*'
71+
Priority: 3
72+
IncludeIsMainRegex: '([-_](test|unittest))?$'
73+
IndentCaseLabels: true
74+
IndentPPDirectives: None
75+
IndentWidth: 2
76+
IndentWrappedFunctionNames: false
77+
JavaScriptQuotes: Leave
78+
JavaScriptWrapImports: true
79+
KeepEmptyLinesAtTheStartOfBlocks: false
80+
MacroBlockBegin: ''
81+
MacroBlockEnd: ''
82+
MaxEmptyLinesToKeep: 1
83+
NamespaceIndentation: None
84+
ObjCBlockIndentWidth: 2
85+
ObjCSpaceAfterProperty: false
86+
ObjCSpaceBeforeProtocolList: false
87+
PenaltyBreakAssignment: 2
88+
PenaltyBreakBeforeFirstCallParameter: 1
89+
PenaltyBreakComment: 300
90+
PenaltyBreakFirstLessLess: 120
91+
PenaltyBreakString: 1000
92+
PenaltyExcessCharacter: 1000000
93+
PenaltyReturnTypeOnItsOwnLine: 200
94+
PointerAlignment: Left
95+
ReflowComments: true
96+
SortIncludes: true
97+
SortUsingDeclarations: true
98+
SpaceAfterCStyleCast: false
99+
SpaceAfterTemplateKeyword: true
100+
SpaceBeforeAssignmentOperators: true
101+
SpaceBeforeParens: ControlStatements
102+
SpaceInEmptyParentheses: false
103+
SpacesBeforeTrailingComments: 2
104+
SpacesInAngles: false
105+
SpacesInContainerLiterals: true
106+
SpacesInCStyleCastParentheses: false
107+
SpacesInParentheses: false
108+
SpacesInSquareBrackets: false
109+
Standard: Auto
110+
TabWidth: 8
111+
UseTab: Never

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ script:
5454
# Travis CI sets NVM_NODEJS_ORG_MIRROR, but it makes node-gyp fail to download headers for nightly builds.
5555
- unset NVM_NODEJS_ORG_MIRROR
5656

57+
- npm run lint
5758
- npm test
5859
after_success:
5960
- cpp-coveralls --gcov-options '\-lp' --build-root test/build --exclude test

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@
264264
"description": "Node.js API (N-API)",
265265
"devDependencies": {
266266
"benchmark": "^2.1.4",
267-
"fs-extra": "^9.0.1",
268267
"bindings": "^1.5.0",
268+
"clang-format": "^1.4.0",
269+
"fs-extra": "^9.0.1",
270+
"pre-commit": "^1.2.2",
269271
"safe-buffer": "^5.1.1"
270272
},
271273
"directories": {},
@@ -300,7 +302,10 @@
300302
"dev": "node test",
301303
"predev:incremental": "node-gyp configure build -C test --debug",
302304
"dev:incremental": "node test",
303-
"doc": "doxygen doc/Doxyfile"
305+
"doc": "doxygen doc/Doxyfile",
306+
"lint": "node scripts/clang-format.js",
307+
"lint:fix": "git-clang-format"
304308
},
309+
"pre-commit": "lint",
305310
"version": "3.0.2"
306311
}

scripts/clang-format.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env node
2+
3+
const spawn = require('child_process').spawnSync;
4+
const path = require('path');
5+
6+
const filesToCheck = ['*.h', '*.cc'];
7+
const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'master';
8+
9+
function main(args) {
10+
let clangFormatPath = path.dirname(require.resolve('clang-format'));
11+
const options = ['--binary=node_modules/.bin/clang-format', '--style=file'];
12+
13+
const gitClangFormatPath = path.join(clangFormatPath,
14+
'bin/git-clang-format');
15+
const result = spawn('python', [
16+
gitClangFormatPath,
17+
...options,
18+
'--diff',
19+
CLANG_FORMAT_START,
20+
'HEAD',
21+
...filesToCheck
22+
], { encoding: 'utf-8' });
23+
24+
if (result.error) {
25+
console.error('Error running git-clang-format:', result.error);
26+
return 2;
27+
}
28+
29+
const clangFormatOutput = result.stdout.trim();
30+
if (clangFormatOutput !== ('no modified files to format') &&
31+
clangFormatOutput !== ('clang-format did not modify any files')) {
32+
console.error(clangFormatOutput);
33+
const fixCmd = '"npm run lint:fix"';
34+
console.error(`
35+
ERROR: please run ${fixCmd} to format changes in your commit`);
36+
return 1;
37+
}
38+
}
39+
40+
if (require.main === module) {
41+
process.exitCode = main(process.argv.slice(2));
42+
}

0 commit comments

Comments
 (0)