@sasjs/lint
<!-- SPDX-License-Identifier: MIT -->
is the linting and formatting engine for SAS code. It is a TypeScript library with no SAS dependency - it reads
files as text - and it is the single engine behind
, the SASjs VS Code extension's Problems panel, and the SASjs Server Studio editor.
This skill covers the rules, the
file, the formatter, and the ways to invoke it. It does not cover the SAS language itself (see
) or the rest of the CLI (see
).
When to use
- Writing or changing a file
- A run reports a warning and you need to know what it means or how to silence it
- Adding lint to a CI pipeline or a git pre-commit hook
- Running the formatter (, format-on-save, )
- Explaining why a rule does or does not fire in an editor
Where it runs
| Surface | How it is invoked | Notes |
|---|
| SASjs CLI | , , , | is the default subcommand. writes a at the project root holding the full default config |
| VS Code extension (, published on Open VSX) | Problems panel (ctrl+shift+M), Format Document, format-on-save | Config precedence: project > setting sasjs-for-vscode.lintConfig
> > defaults |
| SASjs Server Studio | and POST /SASjsApi/code/format
, body | Rules come from the nearest at or above the file's folder on the SASjs Drive, walking up to the drive root. A drive-root is seeded on first start and shows in the file tree as |
| Library | , , , , and the folder/project variants | See the API section below |
All four call the same rules. The CLI, extension and server each pin a version of
, so a rule added to the library only appears in a surface once that surface bumps its dependency - check the pinned version before telling a user a rule should fire in their editor.
Config resolution
When no configuration is passed in, the library resolves it in this order:
- at the project root (the library walks up to find the project)
- in the user's home directory
- - the built-in defaults
A setting that is absent takes its default, and so does a project with no
at all.
is exported from
@sasjs/lint/utils/getLintConfig
, and it is what
writes out.
The .sasjslint file
json
{
"noTrailingSpaces": true,
"maxLineLength": 100,
"noSingleAsteriskComments": true,
"severityLevel": {
"hasDoxygenHeader": "warn",
"noEncodedPasswords": "error"
}
}
Rules and defaults
Two groups, and the way you switch a rule off depends on which group it is in.
On by default - switch off with (except where noted):
| Setting | Default | Severity | What it checks |
|---|
| true | WARNING | the file begins with a Doxygen header |
| true | WARNING | carries the macro name |
| true | WARNING | macros are defined with parentheses, and no space before the |
| true | WARNING | no defined inside another macro |
| true | WARNING | no space in a parameter name (), no unrecognised option () |
| true | WARNING | every macro a file calls is declared in the file or its header |
| true | WARNING | every macro in is actually called |
| true | WARNING | no line ends in whitespace |
| true | WARNING | no tab characters. Alias: (deprecated) |
| true | WARNING | no zero-width or otherwise non-standard characters |
| 2 | WARNING | leading spaces divide cleanly by this number |
| true | WARNING | filenames are lowercase (autocall on requires it) |
| true | WARNING | filenames contain no spaces |
| true | ERROR | no or encoded password |
| 80 | WARNING | line length. Off with |
Off by default - switch on with :
| Setting | Default | Severity | What it checks |
|---|
| false | WARNING | comment statements of the form |
| false | WARNING | a libref the file assigns and never uses |
| false | WARNING | macros carry the options in |
| | WARNING | line endings conform to or |
Supporting settings (not rules):
| Setting | Default | Purpose |
|---|
| | paths to skip. Files in are skipped too |
| | 4-hex-digit strings such as |
| see below | header template applied to a file with no header |
| | per-rule override of / |
| 80 | line limit inside the header |
| 80 | line limit inside / / records |
| | options required by |
| | librefs leaves alone |
Severity and exit codes
Every rule has three states: off, WARNING, or ERROR.
returns a non-zero exit code
only when an ERROR is reported - warnings leave the exit code at 0, so a hook or pipeline keyed on the exit code passes a file that only warns.
is the only rule that ships as ERROR.
promotes or demotes a rule by name, and accepts only
and
; any other value is ignored without complaint:
json
{
"severityLevel": {
"maxLineLength": "error",
"hasDoxygenHeader": "warn"
}
}
Per-file overrides
A file adjusts the rules that apply to itself with a
block in its Doxygen header:
sas
/**
@file
@brief Calls the settlement API
@sasjslint {"maxHeaderLineLength": 200, "noUndeclaredMacros": false}
<h4> SAS Macros </h4>
@li mf_trim.sas
**/
- Scalars replace, arrays are additive (, , gain entries rather than replacing), objects merge key by key ().
- The override applies to the formatter as well, so honours it - a file that switches off keeps the header entries it would otherwise lose.
- Only the header is read, and only a line that starts with the tag followed immediately by . A malformed or unterminated block is ignored rather than raised.
- The header wins over the configuration the caller passes.
- is the one setting a header cannot use: it is evaluated before the file is read.
- Place the block outside the section, which the formatter rewrites.
The formatter
The formatter applies the fixable rules on save. Implemented today:
- add the macro name to
- add a Doxygen header template when the file has none
- add macros a file uses to , de-duplicated and sorted
- remove macros from that section that the file does not use
- remove trailing spaces
Not implemented (do not promise these): tabs to spaces, gremlin removal, line-ending correction, converting single-asterisk comments to block comments.
uses
as the newline placeholder - a literal
does not work:
json
{
"defaultHeader": "/**{lineEnding} @file{lineEnding} @brief Our Company Brief{lineEnding}**/"
}
Library API
ts
import { lintText, lintFile, formatText, formatFile, LintConfig, Severity } from '@sasjs/lint'
const diagnostics = await lintText(code, config) // config optional
const fixed = await formatText(code, config) // config optional
lintText(text, configuration?)
, lintFile(path, configuration?)
, ,
formatText(text, configuration?)
, , ,
- builds a config from a plain object; returns a new config with an override merged in
- carries , , , and
- is (0), (1), (2)
Passing an explicit
matters when the caller knows the rules - a server or editor linting a buffer with no path on disk cannot rely on the working-directory walk.
Pitfalls
- Off means something different per rule. The on-by-default rules switch off with ; switches off with ; switches off with ; the off-by-default rules ignore (it is the same as leaving them out) and need . Getting this wrong is the most common reason a rule "does not work".
- and are inert when is off, because they are scoped to it. The effective limit is the higher of the two values, so setting one lower than is silently ignored.
- plus the formatter removes dependencies. Those entries are consumed by the compiler, so a macro referenced only indirectly - through a dynamic call, or supplied by an d program - loses its declaration and the job can fail to assemble. Switch the rule off for the project or the file when that applies.
- does not see . Macros that ship with SAS are always treated as declared (the list is generated from ), but macros made available through are invisible to the linter and are reported. Declare them in the header or switch the rule off.
- is deliberately generous. A libref counts as used if it appears anywhere outside a statement - including inside a double-quoted string, as in . It is off by default because a file is not a complete job, and it has no formatter fix.
- An empty is not the same as no only for settings whose default was previously applied conditionally. Since 4.0.0 an absent or falls back to its documented default (80, and ), so a project that relied on omitting them to disable those rules must now set or
"hasMacroNameInMend": false
.
- entries are strings, not numbers: , exactly four hex digits.
- The CLI output is a box-drawn table. It is meant for a terminal; do not paste it into a plain-text or ASCII-only document without expecting the border glyphs to be a problem.
- with no subcommand behaves as , linting every file in the project.
Verification
- Confirm a rule fires: put the offending construct in a file and run
terminal(command="npx sasjs lint")
. A hit prints a row with the message and a position; no row means the rule is off or the construct is excluded (check the config first, then the rule's exclusions).
- Confirm a config change took effect: run before and after editing , and compare the rows. A rule that is off by default needs , not .
- Confirm the formatter's effect: run and inspect the diff - the section and lines are the ones it rewrites.
- Confirm a per-file override: add the block, run , and check that the rule it switches off no longer reports for that file while still reporting for its neighbours.