Skip to content

Commit

Permalink
add clang-format hook (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
gengjiawen authored Dec 10, 2020
1 parent 4927643 commit c1bdff0
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 130 deletions.
111 changes: 111 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
- Regex: '^<.*\.h>'
Priority: 1
- Regex: '^<.*'
Priority: 2
- Regex: '.*'
Priority: 3
IncludeIsMainRegex: '([-_](test|unittest))?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
UseTab: Never
15 changes: 15 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: clang-format

on: [push, pull_request]

jobs:
check-clang-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- name: check clang-format
run: |
git config clangFormat.binary node_modules/.bin/clang-format
git config clangFormat.style file
npx check-clang-format
7 changes: 5 additions & 2 deletions 6_object_wrap/napi/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ napi_value MyObject::Init(napi_env env, napi_value exports) {
napi_ref* constructor = new napi_ref;
status = napi_create_reference(env, cons, 1, constructor);
assert(status == napi_ok);
status = napi_set_instance_data(env, constructor,
status = napi_set_instance_data(
env,
constructor,
[](napi_env env, void* data, void* hint) {
napi_ref* constructor = static_cast<napi_ref*>(data);
napi_status status = napi_delete_reference(env, *constructor);
assert(status == napi_ok);
delete constructor;
}, nullptr);
},
nullptr);
assert(status == napi_ok);

status = napi_set_named_property(env, exports, "MyObject", cons);
Expand Down
1 change: 0 additions & 1 deletion 6_object_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "myobject.h"

Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {

Napi::Function func =
DefineClass(env,
"MyObject",
Expand Down
7 changes: 5 additions & 2 deletions 7_factory_wrap/napi/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ napi_status MyObject::Init(napi_env env) {
napi_ref* constructor = new napi_ref;
status = napi_create_reference(env, cons, 1, constructor);
assert(status == napi_ok);
status = napi_set_instance_data(env, constructor,
status = napi_set_instance_data(
env,
constructor,
[](napi_env env, void* data, void* hint) {
napi_ref* constructor = static_cast<napi_ref*>(data);
napi_status status = napi_delete_reference(env, *constructor);
assert(status == napi_ok);
delete constructor;
}, nullptr);
},
nullptr);
assert(status == napi_ok);

return napi_ok;
Expand Down
2 changes: 0 additions & 2 deletions 7_factory_wrap/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using namespace Napi;

Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {

Napi::Function func = DefineClass(
env, "MyObject", {InstanceMethod("plusOne", &MyObject::PlusOne)});

Expand All @@ -19,7 +18,6 @@ Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {

MyObject::MyObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyObject>(info) {

this->counter_ = info[0].As<Napi::Number>().DoubleValue();
};

Expand Down
7 changes: 5 additions & 2 deletions 8_passing_wrapped/napi/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ napi_status MyObject::Init(napi_env env) {
napi_ref* constructor = new napi_ref;
status = napi_create_reference(env, cons, 1, constructor);
assert(status == napi_ok);
status = napi_set_instance_data(env, constructor,
status = napi_set_instance_data(
env,
constructor,
[](napi_env env, void* data, void* hint) {
napi_ref* constructor = static_cast<napi_ref*>(data);
napi_status status = napi_delete_reference(env, *constructor);
assert(status == napi_ok);
delete constructor;
}, nullptr);
},
nullptr);
assert(status == napi_ok);

return napi_ok;
Expand Down
2 changes: 0 additions & 2 deletions 8_passing_wrapped/node-addon-api/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

MyObject::MyObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyObject>(info) {

this->val_ = info[0].As<Napi::Number>().DoubleValue();
};

void MyObject::Init(Napi::Env env, Napi::Object exports) {

Napi::Function func = DefineClass(env, "MyObject", {});

Napi::FunctionReference* constructor = new Napi::FunctionReference();
Expand Down
4 changes: 2 additions & 2 deletions function-reference-demo/node-addon-api/src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "native-addon.h"

Napi::Object Init(Napi::Env env, Napi::Object exports) {
NativeAddon::Init(env, exports);
return exports;
NativeAddon::Init(env, exports);
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
24 changes: 13 additions & 11 deletions function-reference-demo/node-addon-api/src/native-addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
Napi::FunctionReference NativeAddon::constructor;

Napi::Object NativeAddon::Init(Napi::Env env, Napi::Object exports) {

Napi::Function func = DefineClass(env, "NativeAddon", {
InstanceMethod("tryCallByStoredReference", &NativeAddon::TryCallByStoredReference),
InstanceMethod("tryCallByStoredFunction", &NativeAddon::TryCallByStoredFunction)
});
Napi::Function func =
DefineClass(env,
"NativeAddon",
{InstanceMethod("tryCallByStoredReference",
&NativeAddon::TryCallByStoredReference),
InstanceMethod("tryCallByStoredFunction",
&NativeAddon::TryCallByStoredFunction)});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand All @@ -17,18 +19,18 @@ Napi::Object NativeAddon::Init(Napi::Env env, Napi::Object exports) {
return exports;
}

NativeAddon::NativeAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NativeAddon>(info) {
NativeAddon::NativeAddon(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<NativeAddon>(info) {
jsFnRef = Napi::Persistent(info[0].As<Napi::Function>());
jsFn = info[1].As<Napi::Function>();
}

void NativeAddon::TryCallByStoredReference(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();
jsFnRef.Call({});
// Napi::Env env = info.Env();
jsFnRef.Call({});
}

void NativeAddon::TryCallByStoredFunction(const Napi::CallbackInfo& info) {
// Napi::Env env = info.Env();
jsFn.Call({});
// Napi::Env env = info.Env();
jsFn.Call({});
}
20 changes: 10 additions & 10 deletions function-reference-demo/node-addon-api/src/native-addon.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include <napi.h>

class NativeAddon : public Napi::ObjectWrap<NativeAddon> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
NativeAddon(const Napi::CallbackInfo& info);

private:
static Napi::FunctionReference constructor;
Napi::FunctionReference jsFnRef;
Napi::Function jsFn;
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
NativeAddon(const Napi::CallbackInfo& info);

void TryCallByStoredReference(const Napi::CallbackInfo& info);
void TryCallByStoredFunction(const Napi::CallbackInfo& info);
private:
static Napi::FunctionReference constructor;
Napi::FunctionReference jsFnRef;
Napi::Function jsFn;

void TryCallByStoredReference(const Napi::CallbackInfo& info);
void TryCallByStoredFunction(const Napi::CallbackInfo& info);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Napi::FunctionReference NativeEmitter::constructor;

Napi::Object NativeEmitter::Init(Napi::Env env, Napi::Object exports) {

Napi::Function func =
DefineClass(env,
"NativeEmitter",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#include "SimpleAsyncWorker.h"

Value runSimpleAsyncWorker(const CallbackInfo& info) {
int runTime = info[0].As<Number>();
Function callback = info[1].As<Function>();
SimpleAsyncWorker* asyncWorker = new SimpleAsyncWorker(callback, runTime);
asyncWorker->Queue();
std::string msg = "SimpleAsyncWorker for " + std::to_string(runTime) + " seconds queued.";
return String::New(info.Env(),msg.c_str());
int runTime = info[0].As<Number>();
Function callback = info[1].As<Function>();
SimpleAsyncWorker* asyncWorker = new SimpleAsyncWorker(callback, runTime);
asyncWorker->Queue();
std::string msg =
"SimpleAsyncWorker for " + std::to_string(runTime) + " seconds queued.";
return String::New(info.Env(), msg.c_str());
};

Object Init(Env env, Object exports) {
exports["runSimpleAsyncWorker"] = Function::New(env, runSimpleAsyncWorker, std::string("runSimpleAsyncWorker"));
return exports;
exports["runSimpleAsyncWorker"] = Function::New(
env, runSimpleAsyncWorker, std::string("runSimpleAsyncWorker"));
return exports;
}

NODE_API_MODULE(addon, Init)
11 changes: 6 additions & 5 deletions napi-asyncworker-example/node-addon-api/src/SimpleAsyncWorker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#include <thread>

SimpleAsyncWorker::SimpleAsyncWorker(Function& callback, int runTime)
: AsyncWorker(callback), runTime(runTime) {};
: AsyncWorker(callback), runTime(runTime){};

void SimpleAsyncWorker::Execute() {
std::this_thread::sleep_for(std::chrono::seconds(runTime));
if (runTime == 4) SetError ("Oops! Failed after 'working' 4 seconds.");
std::this_thread::sleep_for(std::chrono::seconds(runTime));
if (runTime == 4) SetError("Oops! Failed after 'working' 4 seconds.");
};

void SimpleAsyncWorker::OnOK() {
std::string msg = "SimpleAsyncWorker returning after 'working' " + std::to_string(runTime) + " seconds.";
Callback().Call({Env().Null(), String::New(Env(), msg)});
std::string msg = "SimpleAsyncWorker returning after 'working' " +
std::to_string(runTime) + " seconds.";
Callback().Call({Env().Null(), String::New(Env(), msg)});
};
15 changes: 7 additions & 8 deletions napi-asyncworker-example/node-addon-api/src/SimpleAsyncWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
using namespace Napi;

class SimpleAsyncWorker : public AsyncWorker {

public:
SimpleAsyncWorker(Function& callback, int runTime);
virtual ~SimpleAsyncWorker() {};
public:
SimpleAsyncWorker(Function& callback, int runTime);
virtual ~SimpleAsyncWorker(){};

void Execute();
void OnOK();
void Execute();
void OnOK();

private:
int runTime;
private:
int runTime;
};
Loading

0 comments on commit c1bdff0

Please sign in to comment.