Skip to content

Commit 89993ec

Browse files
committed
add oxfmt precommit hook
1 parent a1c680c commit 89993ec

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ issue](https://github.com/sds/overcommit/issues/238) for more details.
539539
* [Mdl](lib/overcommit/hook/pre_commit/mdl.rb)
540540
* [`*`MergeConflicts](lib/overcommit/hook/pre_commit/merge_conflicts.rb)
541541
* [NginxTest](lib/overcommit/hook/pre_commit/nginx_test.rb)
542+
* [OxFmt](lib/overcommit/hook/pre_commit/ox_fmt.rb)
542543
* [OxLint](lib/overcommit/hook/pre_commit/ox_lint.rb)
543544
* [PhpCs](lib/overcommit/hook/pre_commit/php_cs.rb)
544545
* [PhpCsFixer](lib/overcommit/hook/pre_commit/php_cs_fixer.rb)

config/default.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ PreCommit:
565565
flags: ['-t']
566566
include: '**/nginx.conf'
567567

568+
OxFmt:
569+
enabled: false
570+
description: 'Analyze with oxfmt'
571+
required_executable: 'oxfmt'
572+
flags: ['--check']
573+
install_command: 'npm install -g oxfmt'
574+
568575
OxLint:
569576
enabled: false
570577
description: 'Analyze with oxlint'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
module Overcommit::Hook::PreCommit
4+
# Runs `oxfmt` against any modified files.
5+
#
6+
# Protip: if you have an npm script set up to run oxfmt, you can configure
7+
# this hook to run oxfmt via your npm script by using the `command` option in
8+
# your .overcommit.yml file. This can be useful if you have some oxfmt
9+
# configuration built into your npm script that you don't want to repeat
10+
# somewhere else. Example:
11+
#
12+
# oxfmt:
13+
# required_executable: 'npm'
14+
# enabled: true
15+
# command: ['npm', 'run', 'fmt', '--', '--check']
16+
#
17+
# Note: This hook only supports check mode.
18+
#
19+
# @see https://oxc.rs
20+
class OxFmt < Base
21+
def run
22+
oxfmt_regex = /^(?<file>.+) \(\d+ms\)/
23+
result = execute(command, args: applicable_files)
24+
output = result.stdout.chomp
25+
messages = output.split("\n").grep(oxfmt_regex)
26+
27+
return [:fail, result.stderr] if messages.empty? && !result.success?
28+
return :pass if result.success? && output.empty?
29+
30+
# example message:
31+
# test.js (5ms)
32+
extract_messages(messages, oxfmt_regex)
33+
end
34+
end
35+
end
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Overcommit::Hook::PreCommit::OxFmt do
6+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
7+
let(:context) { double('context') }
8+
subject { described_class.new(config, context) }
9+
10+
before do
11+
subject.stub(:applicable_files).and_return(%w[file1.js file2.js])
12+
end
13+
14+
context 'when oxfmt is unable to run' do
15+
let(:result) { double('result') }
16+
17+
before do
18+
result.stub(:stderr).and_return('SyntaxError: Use of const in strict mode.')
19+
result.stub(:stdout).and_return('')
20+
21+
result.stub(:success?).and_return(false)
22+
subject.stub(:execute).and_return(result)
23+
end
24+
25+
it { should fail_hook }
26+
end
27+
28+
context 'when oxfmt exits successfully' do
29+
let(:result) { double('result') }
30+
31+
before do
32+
result.stub(:success?).and_return(true)
33+
subject.stub(:execute).and_return(result)
34+
end
35+
36+
context 'with no output' do
37+
before do
38+
result.stub(:stdout).and_return('')
39+
end
40+
41+
it { should pass }
42+
end
43+
44+
context 'and it reports an error' do
45+
before do
46+
result.stub(:stdout).and_return([
47+
'Checking formatting...',
48+
'',
49+
'README.md (66ms)',
50+
'',
51+
'Format issues found in above 1 files. Run without `--check` to fix.',
52+
'Finished in 66ms on 1 files using 8 threads.'
53+
].join("\n"))
54+
end
55+
56+
it { should fail_hook }
57+
end
58+
59+
context 'and it doesnt count false positives error messages' do
60+
before do
61+
result.stub(:stdout).and_return([
62+
'$ yarn oxfmt --check /app/project/Error.ts',
63+
'$ /app/project/node_modules/.bin/oxfmt --check /app/project/Error.ts',
64+
'',
65+
].join("\n"))
66+
end
67+
68+
it { should pass }
69+
end
70+
end
71+
72+
context 'when oxfmt exits unsucessfully' do
73+
let(:result) { double('result') }
74+
75+
before do
76+
result.stub(:success?).and_return(false)
77+
subject.stub(:execute).and_return(result)
78+
end
79+
80+
context 'and it reports an error' do
81+
before do
82+
result.stub(:stdout).and_return([
83+
'Checking formatting...',
84+
'',
85+
'README.md (66ms)',
86+
'',
87+
'Format issues found in above 1 files. Run without `--check` to fix.',
88+
'Finished in 66ms on 1 files using 8 threads.'
89+
].join("\n"))
90+
end
91+
92+
it { should fail_hook }
93+
end
94+
end
95+
end

0 commit comments

Comments
 (0)