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
10.0 KiB
Raw
/**
* A `match` with one or two arms is an `if`.
*
* match (x) { case null: a case _: b } → if (x == null) a else b
* match (p) { case Point {x, y}: a case _: b } → if (let Point {x, y} = p) a else b
* match (s) { case Circle: a case _: b } → if (s is Circle) a else b
* match (b) { case true: a case false: b } → if (b) a else b
* match (e) { case Add {l, r}: use(l, r) } → if (let Add {l, r} = e) { use(l, r); }
*
* The second arm must bind nothing — a wildcard, a literal, a bare type
* name — since `else` has nothing to bind. Guards are left alone. A
* nullable discriminant keeps its `match` when the first arm is a
* destructuring pattern: `if (let …)` against `null` traps (#663).
*/
import {
Node, Expression, Statement, MatchExpression, MatchCase, ExpressionStatement,
BlockStatement, Identifier, MemberExpression, NumberLiteral, StringLiteral,
BooleanLiteral, NullLiteral, BindingPattern, IdentifierPattern, ClassPattern,
RecordPattern, TuplePattern, ArrayPattern, StringLiteral as StringLit,
TemplateLiteral, FunctionExpression, IfExpression,
} from 'compiler/ast';
import { walkEnter } from 'compiler/visitor';
import { FunctionType } from 'compiler/types';
import { RuleContext, LINE_WIDTH } from '../context.zena';
import { dedentContinuation, indentationAt, lineStart } from '../source.zena';
export function matchToIf(ctx: RuleContext): void {
walkEnter(ctx.ast, (n) => {
if (n is MatchExpression) {
rewrite(ctx, n);
}
});
}
function rewrite(ctx: RuleContext, node: MatchExpression): void {
let cases = node.cases;
if (cases.length == 0 || cases.length > 2) {
return;
}
for (let c in cases) {
if (c.guard != null) {
return;
}
}
let parent = ctx.parent(node);
if (parent == null) {
return;
}
let asStatement = parent is ExpressionStatement && !isValueTail(ctx, parent);
if (cases.length == 1 && !asStatement) {
return;
}
if (cases.length == 1 && isEmptyBlock(cases[0].body)) {
return;
}
let discriminant = node.discriminant;
// `case (let k, let v):` binds inside the pattern; `if (let …)` has one
// `let` for the whole pattern and no place for those.
if (hasNestedBinding(cases[0].pattern)) {
return;
}
let condition = conditionFor(ctx, cases[0].pattern, discriminant);
if (condition == null) {
return;
}
if (cases.length == 2 && !bindsNothing(cases[1].pattern)) {
return;
}
if (ctx.hasComment(node)) {
return;
}
// Arm bodies move from under `case` to under `if`: one level up.
let target = if (asStatement) parent else node;
let indent = indentationAt(ctx.source, ctx.start(target));
let caseIndent = indentationAt(ctx.source, ctx.start(cases[0]));
let dedent = if (hasMultiLineLiteral(ctx, node)) 0 else caseIndent.length - indent.length;
let column = ctx.columnOf(target);
// A `case _: {}` in statement position is no `else` at all.
let hasElse = cases.length == 2 && !(asStatement && isEmptyBlock(cases[1].body));
var text = '';
if (!asStatement) {
// In value position an arm is an expression or a value block, and a
// short pair reads best on one line.
let first = armText(ctx, cases[0].body, dedent);
let second = armText(ctx, cases[1].body, dedent);
let oneLine = `if (${condition}) ${first} else ${second}`;
if (cases[0].body is Expression && cases[1].body is Expression &&
ctx.isSingleLine(cases[0].body) && ctx.isSingleLine(cases[1].body) &&
column + oneLine.length <= LINE_WIDTH) {
text = oneLine;
} else {
text = `if (${condition}) ${blockOf(first, cases[0].body, indent)} else ${blockOf(second, cases[1].body, indent)}`;
}
ctx.replace(node, text);
return;
}
// In statement position every arm is a block. A one-line block stays
// on the line when the whole `if` fits, and is opened up otherwise.
let first = statementArm(ctx, cases[0].body, dedent);
var oneLine = `if (${condition}) { ${first} }`;
var second = '';
if (hasElse) {
second = statementArm(ctx, cases[1].body, dedent);
oneLine = `${oneLine} else { ${second} }`;
}
let allOneLine = ctx.isSingleLine(cases[0].body) &&
(!hasElse || ctx.isSingleLine(cases[1].body));
if (allOneLine && column + oneLine.length <= LINE_WIDTH) {
text = oneLine;
} else {
text = `if (${condition}) {\n${indent} ${first}\n${indent}}`;
if (hasElse) {
text = `${text} else {\n${indent} ${second}\n${indent}}`;
}
}
ctx.replaceRange(ctx.start(parent), ctx.statementEnd(parent), text);
}
/**
* An arm's statements as they will sit inside the `if` block: the
* block's own statements, dedented one level, or the expression with a
* `;` after it.
*/
function statementArm(ctx: RuleContext, body: Node, dedent: i32): String {
if (body is BlockStatement) {
let block = body;
if (block.body.length == 0) {
return '';
}
let inner = ctx.slice(ctx.start(block.body[0]), ctx.statementEnd(block.body[block.body.length - 1]));
return dedentContinuation(inner, dedent);
}
return `${dedentContinuation(ctx.text(body), dedent)};`;
}
function isEmptyBlock(body: Node): boolean {
if (let BlockStatement {body as statements} = body) {
return statements.length == 0;
}
return false;
}
/**
* Whether the expression statement is the last statement of a block
* whose value it is — a value-block arm, `if` branch, or function body
* with a non-void result — so that the match is really an expression.
*/
function isValueTail(ctx: RuleContext, statement: Node): boolean {
let block = ctx.parent(statement);
if (block == null || !(block is BlockStatement)) {
return false;
}
let body = block.body;
if (body.length == 0 || body[body.length - 1] !== statement) {
return false;
}
let owner = ctx.parent(block);
if (owner == null) {
return false;
}
return match (owner) {
case MatchCase {}: {
// The arm of a match that is itself a statement is a statement block.
let outer = ctx.parent(owner);
if (outer == null) {
false
} else {
let outerParent = ctx.parent(outer);
!(outerParent != null && outerParent is ExpressionStatement &&
!isValueTail(ctx, outerParent))
}
}
case IfExpression {}: true
case FunctionExpression {}: {
let t = ctx.typeOf(owner);
if (t != null && t is FunctionType) {
!ctx.isVoid(t.returnType)
} else {
false
}
}
case _: false
};
}
/** The `if` condition that selects `pattern`, or null for a pattern `if` cannot express. */
function conditionFor(ctx: RuleContext, pattern: Node, discriminant: Expression): String | null {
let subject = ctx.text(discriminant);
let nullable = ctx.isNullable(ctx.typeOf(discriminant));
return match (pattern) {
case Identifier {name}: {
// A bare name is a type test; `_` alone selects everything.
if (name == '_') null else `${subject} is ${name}`
}
case MemberExpression {}: `${subject} == ${ctx.text(pattern)}`
case NullLiteral {}: `${subject} == null`
case NumberLiteral {}: `${subject} == ${ctx.text(pattern)}`
case StringLiteral {}: `${subject} == ${ctx.text(pattern)}`
case BooleanLiteral {value}: {
if (value) {
subject
} else if (ctx.isSimpleReference(discriminant)) {
`!${subject}`
} else {
`!(${subject})`
}
}
case BindingPattern {pattern as inner}: {
if (nullable || inner is IdentifierPattern) {
null
} else {
`let ${ctx.text(inner)} = ${subject}`
}
}
case ClassPattern {properties}: {
if (nullable) {
null
} else if (properties.length == 0) {
// `case Foo {}:` binds nothing but narrows the discriminant, which
// is what `is` does and `if (let Foo {} = x)` does not.
`${subject} is ${classPatternTypeText(ctx, pattern)}`
} else {
`let ${ctx.text(pattern)} = ${subject}`
}
}
case RecordPattern {}: if (nullable) null else `let ${ctx.text(pattern)} = ${subject}`
case TuplePattern {}: if (nullable) null else `let ${ctx.text(pattern)} = ${subject}`
case _: null
};
}
/** The type a class pattern names, as source text: `Box<f64>` for `case Box<f64> {}:`. */
function classPatternTypeText(ctx: RuleContext, pattern: ClassPattern): String {
let name = ctx.text(pattern.name);
let typeArguments = pattern.typeArguments;
if (typeArguments == null) {
return name;
}
var args = '';
for (let arg in typeArguments) {
args = if (args.length == 0) ctx.text(arg) else `${args}, ${ctx.text(arg)}`;
}
return `${name}<${args}>`;
}
/** Whether a `let`/`var` binding sits inside the pattern rather than at its top. */
function hasNestedBinding(pattern: Node): boolean {
var found = false;
walkEnter(pattern, (n) => {
if (n !== pattern && n is BindingPattern) {
found = true;
}
});
return found;
}
/** Whether the pattern introduces no names, so it can be an `else`. */
function bindsNothing(pattern: Node): boolean {
return match (pattern) {
case Identifier {}: true
case MemberExpression {}: true
case NullLiteral {}: true
case NumberLiteral {}: true
case StringLiteral {}: true
case BooleanLiteral {}: true
case ClassPattern {properties}: properties.length == 0
case _: false
};
}
/** An arm's body as it will read under the `if`, in value position. */
function armText(ctx: RuleContext, body: Node, dedent: i32): String {
return dedentContinuation(ctx.text(body), dedent);
}
/** `text` as a block, unless the arm already was one. */
function blockOf(text: String, body: Node, indent: String): String {
if (body is BlockStatement) {
return text;
}
return `{\n${indent} ${text}\n${indent}}`;
}
/** Whether a string or template literal in the match spans lines (dedenting would change it). */
function hasMultiLineLiteral(ctx: RuleContext, node: Node): boolean {
var found = false;
walkEnter(node, (n) => {
if ((n is StringLit || n is TemplateLiteral) && !ctx.isSingleLine(n)) {
found = true;
}
});
return found;
}
- 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 |
|
|