lint 0.0.2-preview.1
Lint rules for Zena source, each with a fix
lint#
Lint rules for Zena source code, each with an automated fix. The compiler and type checker drive every rule, and when a fix is applied it is type-checked again before it is kept.
Usage#
Command Line#
zena lint [--fix] [--only a,b] [--skip a,b] <paths...>
zena lint --list-rules
A path is a .zena file or a directory, searched for them. Without --fix the command prints each finding as path:line:column: message [rule] and exits 1 when there is one; with --fix it rewrites the files. -v on the command lists every fix and the text of any rejected one.
The language server (zena lsp) publishes the findings as warnings and offers each as a quick fix, plus a source.fixAll.zena code action for the whole file.
Library API#
import { LintOptions, lintFile } from 'lint';
import { Project } from 'lint/checking';
import { allRules } from 'lint/rules';
let project = new Project();
let options = new LintOptions(allRules());
let report = await lintFile(project, 'src/main.zena', options);
Rules#
| Rule | Rewrite |
|---|---|
function-declaration |
top-level let f = (…) => … → function f(…) { … } |
unnecessary-cast |
x as T where x: T → x (from the checker's warning, less contextually typed operands) |
unnecessary-optional-chain |
x?.f, x?[i], f?(…) on a non-nullable target → x.f, x[i], f(…) |
unnecessary-annotation |
let x: T = e with e: T; return types the body implies; callback parameter types |
collection-literal |
new Array<T>() + push runs → growable<T>([…]); new Map<K, V>() + []= → {k => v} |
if-initializer |
var x = d; if (c) { x = a; } → let x = if (c) a else d |
nullish |
if (x != null) x else d → x ?? d; if (x != null) x.f else null → x?.f |
match-to-if |
one- and two-arm match → if, if (x is T), if (let P = x) |
else-if |
else { if (…) { … } } → else if (…) { … } |
for-in |
index loops that read only xs[i] → for (let x in xs) |
boolean-return |
if (c) { return true; } return false; → return c;; if (c) true else false → c |
template-literal |
three or more +-joined strings → a template literal |
console-to-string |
console.log(`${x}`) and console.log(i32ToString(x)) → console.log(x) |
arrow-body |
(x) => { return e; } → (x) => e |
prefer-let |
var never assigned again → let |
immutable-field |
var field nothing assigns → immutable field (#private, or the class is not exported) |
empty-constructor |
new() {} on a class with no other constructor → removed; the default is synthesized |
Libraries#
lint/lint/lint— lint driver (lintFile,lintSource,LintOptions,FileReport)lint/checking— compiler project wrapper and overlay host (Project,CheckedFile)lint/checked— checked module wrapper (CheckedFile)lint/rules— rule registry (allRules,ruleNamed)lint/rule— rule runner (NamedRule,proposeEdits,messageOf)lint/context— rule traversal context (RuleContext)lint/source— byte-range edits (Edit,applyEdits,columnOf)lint/files—.zenafile discovery (collectZenaFiles)
License#
MIT
4.4 KiB
Raw
/**
* `if (c) { return true; } return false;` is `return c;`, and the
* expression `if (c) true else false` is `c`.
*
* With the literals the other way round the condition is negated: a
* comparison flips its operator, `!x` loses its `!`, a name or call gets
* one, anything else is wrapped in `!(…)`.
*/
import {
Node, Expression, Statement, Module, BlockStatement, IfStatement, ReturnStatement,
BooleanLiteral, BinaryExpression, UnaryExpression, CallExpression, Identifier,
MemberExpression, LetPatternCondition, IfExpression,
} from 'compiler/ast';
import { walkEnter } from 'compiler/visitor';
import { RuleContext } from '../context.zena';
export function booleanReturns(ctx: RuleContext): void {
walkEnter(ctx.ast, (n) => {
if (n is IfExpression) {
booleanIf(ctx, n);
return;
}
let statements = match (n) {
case Module {body}: body
case BlockStatement {body}: body
case _: null
};
if (statements == null) {
return;
}
var i = 0;
while (i < statements.length) {
let next = if (i + 1 < statements.length) statements[i + 1] else null;
foldAt(ctx, statements[i], next);
i += 1;
}
});
}
/** `if (c) true else false` is `c`; the other way round, `!c`. */
function booleanIf(ctx: RuleContext, node: IfExpression): void {
let alternate = node.alternate;
if (alternate == null || node.test is LetPatternCondition || !(node.test is Expression)) {
return;
}
let whenTrue = ctx.singleExpression(node.consequent);
let whenFalse = ctx.singleExpression(alternate);
if (whenTrue == null || whenFalse == null) {
return;
}
if (!(whenTrue is BooleanLiteral) || !(whenFalse is BooleanLiteral)) {
return;
}
let a = whenTrue.value;
let b = whenFalse.value;
if (a == b || ctx.hasComment(node)) {
return;
}
let test = node.test as Expression;
ctx.replace(node, if (a) ctx.text(test) else negated(ctx, test));
}
function foldAt(ctx: RuleContext, first: Statement, next: Statement | null): void {
if (!(first is IfStatement)) {
return;
}
let guard = first;
if (guard.test is LetPatternCondition || !(guard.test is Expression)) {
return;
}
let whenTrue = returnedBoolean(guard.consequent);
if (whenTrue == null) {
return;
}
var last: Statement = first;
var whenFalse: BooleanLiteral | null = null;
let alternate = guard.alternate;
if (alternate != null) {
whenFalse = returnedBoolean(alternate);
} else if (next != null) {
whenFalse = returnedBoolean(next);
last = next;
}
if (whenFalse == null || whenTrue.value == whenFalse.value) {
return;
}
let start = ctx.start(first);
let end = ctx.statementEnd(last);
if (ctx.hasCommentIn(start, end)) {
return;
}
let test = guard.test as Expression;
let condition = if (whenTrue.value) ctx.text(test) else negated(ctx, test);
ctx.replaceRange(start, end, `return ${condition};`);
}
/** The literal of a `return true;` or `return false;` branch. */
function returnedBoolean(branch: Node): BooleanLiteral | null {
let statement = match (branch) {
case BlockStatement {body}: if (body.length == 1) body[0] else null
case Statement as s: s
case _: null
};
if (statement == null) {
return null;
}
if (let ReturnStatement {argument} = statement) {
if (argument != null && argument is BooleanLiteral) {
return argument;
}
}
return null;
}
/** `!test`, written as directly as the shape allows. */
function negated(ctx: RuleContext, test: Expression): String {
match (test) {
case BinaryExpression {operator, left, right}: {
let flipped = flip(operator);
if (flipped != null) {
return `${ctx.text(left)} ${flipped} ${ctx.text(right)}`;
}
}
case UnaryExpression {operator, argument}: {
if (operator == '!') {
return ctx.text(argument);
}
}
case Identifier {}: { return `!${ctx.text(test)}`; }
case MemberExpression {}: { return `!${ctx.text(test)}`; }
case CallExpression {}: { return `!${ctx.text(test)}`; }
case _: {}
}
return `!(${ctx.text(test)})`;
}
function flip(operator: String): String | null {
if (operator == '==') { return '!='; }
if (operator == '!=') { return '=='; }
if (operator == '===') { return '!=='; }
if (operator == '!==') { return '==='; }
if (operator == '<') { return '>='; }
if (operator == '>=') { return '<'; }
if (operator == '>') { return '<='; }
if (operator == '<=') { return '>'; }
return null;
}
- compiler compiler.zena-lang.pkg.zena-lang.dev 0.0.2-preview.1
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.2-preview.1 | 2026-11-19 |
|
|