lint 0.0.2-preview.1

Lint rules for Zena source, each with a fix

Readme Files Dependencies Versions

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 — .zena file discovery (collectZenaFiles)

License#

MIT

2.9 KiB Raw
/**
 * A `var` field that nothing assigns loses its `var`.
 *
 *     class Named {                 class Named {
 *       var name: String;     →       name: String;
 *       new(this.name);               new(this.name);
 *     }                             }
 *
 * A field is initialized by its initializer, a `this.` parameter or the
 * initializer list, never by an assignment — an assignment to an
 * immutable field is an error even in the constructor body — so any
 * assignment to a member of the field's name, anywhere in the file and on
 * any object, keeps the field `var`. A same-named field on another class
 * keeps it too, which errs on the side of leaving code alone.
 *
 * A public field of an exported class can be assigned from a file this
 * check never sees, so only `#private` fields and the fields of classes
 * that are not exported are considered. A field with a separate setter
 * name (`var(#count) count`), a static, shared, abstract or decorated
 * field, and a symbol-keyed field are left alone, and so is the whole
 * standard library, some of whose fields the compiler writes itself.
 */

import {
  ClassDeclaration, FieldDefinition, AssignmentExpression, MemberExpression,
  Identifier,
} from 'compiler/ast';
import { walkEnter } from 'compiler/visitor';
import { Set } from 'zena:collections';
import { RuleContext } from '../context.zena';
import { skipWhitespace } from '../source.zena';

export function immutableFields(ctx: RuleContext): void {
  // The compiler writes some standard library fields itself
  // (`String.#hashCode`, from the string-hash helper it generates), and no
  // rule can see that.
  if (ctx.file.isStdlib) {
    return;
  }
  // Every member name some assignment in the file writes.
  let written = new Set<String>();
  walkEnter(ctx.ast, (n) => {
    if (let AssignmentExpression {left} = n) {
      walkEnter(left, (l) => {
        if (let MemberExpression {property} = l) {
          written.add(property.name);
        }
      });
    }
  });
  walkEnter(ctx.ast, (n) => {
    if (n is ClassDeclaration) {
      rewrite(ctx, n, written);
    }
  });
}

function rewrite(ctx: RuleContext, decl: ClassDeclaration, written: Set<String>): void {
  if (decl.isExtension) {
    return;
  }
  for (let member in decl.body) {
    if (!(member is FieldDefinition)) {
      continue;
    }
    let field = member;
    if (field.mutability != 'var' || field.setterName != null || field.isStatic ||
        field.isShared || field.isAbstract || field.isDeclare || field.decorators.length != 0) {
      continue;
    }
    if (!(field.name is Identifier)) {
      continue;
    }
    let name = (field.name as Identifier).name;
    if (decl.exported && !name.startsWith('#')) {
      continue;
    }
    if (written.has(name)) {
      continue;
    }
    let start = ctx.start(field);
    if (ctx.slice(start, start + 3) != 'var') {
      continue;
    }
    ctx.removeRange(start, skipWhitespace(ctx.source, start + 3));
  }
}
  • compiler compiler.zena-lang.pkg.zena-lang.dev 0.0.2-preview.1
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19
  • compiler compiler.zena-lang.pkg.zena-lang.dev 0.0.2-preview.1