Skip to content

Commit

Permalink
build: use wasm clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
avivkeller committed Jul 20, 2024
1 parent 45ff44b commit 01adc80
Show file tree
Hide file tree
Showing 6 changed files with 731 additions and 317 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ ADDON_DOC_LINT_FLAGS=-whitespace/ending_newline,-build/header_guard

.PHONY: format-cpp-build
format-cpp-build:
cd tools/clang-format && $(call available-node,$(run-npm-ci))
cd tools/clang-format && chmod +x clang-format.mjs && $(call available-node,$(run-npm-ci))

.PHONY: format-cpp-clean
.NOTPARALLEL: format-cpp-clean
Expand All @@ -1486,8 +1486,8 @@ CLANG_FORMAT_START ?= HEAD
format-cpp: ## Format C++ diff from $CLANG_FORMAT_START to current changes
ifneq ("","$(wildcard tools/clang-format/node_modules/)")
$(info Formatting C++ diff from $(CLANG_FORMAT_START)..)
@$(PYTHON) tools/clang-format/node_modules/.bin/git-clang-format \
--binary=tools/clang-format/node_modules/.bin/clang-format \
@$(PYTHON) tools/clang-format/git-clang-format \
--binary=tools/clang-format/clang-format.mjs \
--style=file \
$(CLANG_FORMAT_START) -- \
$(FORMAT_CPP_FILES)
Expand Down
32 changes: 32 additions & 0 deletions tools/clang-format/clang-format.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

import init, { format } from '@wasm-fmt/clang-format';
import fs from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';

await init();

// Check if -i flag is present
const args = process.argv.slice(2);
const inplace = args.includes('-i');
const fileArguments = args.filter((arg) => !arg.startsWith('-'));

// Read .clang-format configuration
const config = await fs.readFile('.clang-format', 'utf8');

for (const file of fileArguments) {
try {
const content = await fs.readFile(file, 'utf8');
const name = path.basename(file);
const formatted = format(content, name, config);

if (inplace) {
if (formatted !== content) await fs.writeFile(file, formatted);
} else {
console.log(formatted);
}
} catch (error) {
console.error(`Error processing file ${file}:`, error);
}
}
Loading

0 comments on commit 01adc80

Please sign in to comment.