Suggestions for implementing format linting?

I wrote the following bash script around the old bsrefmt to lint my source files and ensure that I hadn’t somehow introduced unformatted changes:

#!/bin/bash

files=`ls src/**/*.re src/*.re`
errors=false
for file in $files
do
  current=`cat $file`
  linted=`bsrefmt $file`
  diff=`diff <(echo $current) <(echo $linted)`
  if [ ${#diff} -gt 0 ]
  then
    echo "ERROR: $file doesn't pass lint"
    errors=true
  fi
done

if $errors
then
  exit 1
else
  echo "All files pass linting!"
fi

I’m not sure how to do this with Rescript, since the formatter seems to only write the result to a new file. I’d rather not create a ton of new files in order to diff them against the existing ones. Are there any commands I’m missing that I could use to implement this same linting?

bsc -fomat writes to stdout (in bs-platform 8.3.3) so you can use something like

linted=$(./node_modules/.bin/bsc -format $file)

Fyi, I think this should be a feature in rescript formatter itself to check if a rescript file (or whole project) is well formatted or not. i’ve opened an issue recently.

Ah yeah, perfect! Thanks- I tossed a :+1: on your issue as well- I’d rather use a built-in solution that my own hand-rolled script.

1 Like

Is this still the theory of lint checks for rescript? -format isn’t an option in

$ npx bsc -v
ReScript 9.1.4

is there a reason it got deleted?

EDIT: I found rescript format for 9.1.x

The syntax I use now is

#!/bin/bash

files=`ls src/**/*.res src/*.res`
errors=false
for file in $files
do
  current=`cat $file`
  linted=`echo "${current}" | ./node_modules/.bin/rescript format -stdin .res`
  diff=`diff <(echo $current) <(echo $linted)`

  if [ ${#diff} -gt 0 ]
  then
    echo "ERROR: $file doesn't pass lint"
    errors=true
  fi
done

if $errors
then
  exit 1
else
  echo "All files pass linting!"
fi

I’d still love a native rescript lint command that just does a dry-run of the format, failing if anything requires formatting.

2 Likes