sasjs / lint

Linting and formatting for SAS® code

Home Page:https://sasjs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support fail on warning (e.g. in pre-commit hook)

ccastillo232 opened this issue · comments

Currently I have a pre-commit set up but when there are warnings it will commit the changes with no indication of any issues. Only if I introduce a linting issue with severity of ERROR will the commit fail.

I want strict linting in my project and would like to add a pre-commit hook that fails on warnings from sasjs lint. (I have ghooks installed - perhaps it's a ghooks setting that I am not seeing?)

As a workaround I would like to be able to configure the severity of linting issues to force some issues to be errors.

Does this request belong in the cli project? In CLI src/commands/lint/lintCommands.ts::processLint() seems to determine what the return code is. Although I guess you would want to put some kind of failOnWarning setting in the .sasjslint file and have it flow through to the cli.

I explored some workarounds for this.
I was not able to setup a pre-commit hook (with ghooks), but I created a github workflow that calls the following shell script. The shell script is basically a wrapper around the sasjs lint command that will grep the output for the 'Warning' keyword.
It has not been put to use yet and it may turn out to be brittle in its current state, but my hope is it can be maintained/enhanced with minimal effort.

#!/bin/bash

# Shell script to handle results of sasjs lint
# Note that this script will check against all files in your local
# repository, regardless if they've been indexed (marked for a git commit).
# This will make it overly strict, but also could result in pushing non-compliant
# files (for example, if the indexed file is non-compliant but the local version
# is compliant.)
# As a result, this relies on running as part of a CI/CD process.
# Created by Chuck Castillo

# Capture sasjs lint output
sasjs_lint_output=$(sasjs lint)
# Capture sasjs lint return code
lint_rc=$?
# display the output
echo "${sasjs_lint_output}"

if [[ ${lint_rc} -eq 0 ]]
then
	# No errors, check for warnings	
	echo "${sasjs_lint_output}" | grep -qw Warning
	grep_rc="${PIPESTATUS[1]}"
	
	if [[ ${grep_rc} -eq 1 ]]
	then
		exit 0
	else	
		exit 3
	fi
else
	exit 3
fi

Hi Chuck,

You have put the issue in the right place, and I agree it would be a great feature (ability to configure the severity of each rule)

Thanks for contributing your script. When we get some capacity / resource, I'd definitely like to prioritise this (and a PR would also get our attention).

Btw, we stopped using ghooks as it didn't work well when a GIT project contained multiple NPM projects. Instead we add the following as a script in the package.json:

    "prepare": "git rev-parse --git-dir && git config core.hooksPath ./.git-hooks || true"

This updates the git hooks path when running npm install for the current project. You can then put hook scripts inside the .git-hooks folder of the repo, you can see an example in this repo (and many other SASjs repos). No dependency required!

Update - we are currently working on this, and plan to implement a new object in the .sasjslint file that allows configuration of severity levels for each rule, eg:

{
  "noTrailingSpaces": true,
  "hasDoxygenHeader": true,
  "maxLineLength": 300,
  "severityLevel": {
    "hasDoxygenHeader": "warn",
    "maxLineLength": "error",
    "noTrailingSpaces": "error"
  }
}
  • "warn" - show warning in the log (doesn’t affect exit code)
  • "error" - show error in the log (exit code is 1 when triggered)

If a rule is not specified, then the default severity level will be applied.

Hi @ccastillo232 - this issue was just addressed in this PR: #181

It will be available in the next VS Code Extension release (it needs to be bumped to include this version of the lint)

CLI is working:

image

and extension:

image