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

3.4 KiB Raw
/**
 * Three or more strings joined with `+` become one template literal.
 *
 *     a + ' - ' + b + '!'   →   `${a} - ${b}!`
 *
 * Every operand must be a `String`, and at least one a literal, or there
 * is nothing to interpolate into. A chain already broken across lines is
 * folded only when the result fits on the line it starts on.
 */

import {
  Node, Expression, BinaryExpression, StringLiteral, TemplateLiteral,
  TaggedTemplateExpression,
} from 'compiler/ast';
import { walkEnter } from 'compiler/visitor';
import { StringBuilder } from 'zena:core';
import { RuleContext, LINE_WIDTH } from '../context.zena';

let BACKTICK = 96;
let DOLLAR = 36;
let LEFT_BRACE = 123;
let BACKSLASH = 92;
let SINGLE_QUOTE = 39;
let DOUBLE_QUOTE = 34;

export function templateLiterals(ctx: RuleContext): void {
  walkEnter(ctx.ast, (n) => {
    if (!isConcatenation(n)) {
      return;
    }
    let parent = ctx.parent(n);
    if (parent != null && isConcatenation(parent)) {
      return;
    }
    rewrite(ctx, n as BinaryExpression);
  });
}

function isConcatenation(node: Node): boolean {
  if (let BinaryExpression {operator} = node) {
    return operator == '+';
  }
  return false;
}

function rewrite(ctx: RuleContext, root: BinaryExpression): void {
  if (!ctx.isString(ctx.typeOf(root))) {
    return;
  }
  let operands = new Array<Expression>();
  flatten(root, operands);
  if (operands.length < 3) {
    return;
  }
  var literals = 0;
  for (let operand in operands) {
    if (!ctx.isString(ctx.typeOf(operand))) {
      return;
    }
    if (operand is StringLiteral || operand is TemplateLiteral) {
      literals += 1;
    }
    if (operand is TaggedTemplateExpression) {
      return;
    }
  }
  if (literals == 0 || ctx.hasComment(root)) {
    return;
  }
  let out = new StringBuilder();
  out.appendByte(BACKTICK);
  for (let operand in operands) {
    let text = ctx.text(operand);
    if (operand is StringLiteral) {
      appendStringBody(out, text);
    } else if (operand is TemplateLiteral) {
      out.appendSlice(text, 1, text.length - 1);
    } else {
      out.append('${');
      out.append(text);
      out.append('}');
    }
  }
  out.appendByte(BACKTICK);
  let result = out.toString();
  if (!ctx.isSingleLine(root) && ctx.columnOf(root) + result.length > LINE_WIDTH) {
    return;
  }
  ctx.replace(root, result);
}

/** The leaves of a left-leaning `+` tree, in order. */
function flatten(node: Expression, out: GrowableArray<Expression>): void {
  if (let BinaryExpression {operator, left, right} = node) {
    if (operator == '+') {
      flatten(left, out);
      flatten(right, out);
      return;
    }
  }
  out.push(node);
}

/**
 * The content of a quoted string literal, re-escaped for a template:
 * the quote no longer needs escaping, backticks and `${` now do.
 */
function appendStringBody(out: StringBuilder, literal: String): void {
  let quote = literal.getByteAt(0);
  var i = 1;
  let end = literal.length - 1;
  while (i < end) {
    let b = literal.getByteAt(i);
    if (b == BACKSLASH && i + 1 < end) {
      let next = literal.getByteAt(i + 1);
      if (next == quote) {
        out.appendByte(quote);
      } else {
        out.appendByte(b);
        out.appendByte(next);
      }
      i += 2;
      continue;
    }
    if (b == BACKTICK) {
      out.appendByte(BACKSLASH);
      out.appendByte(BACKTICK);
    } else if (b == DOLLAR && i + 1 < end && literal.getByteAt(i + 1) == LEFT_BRACE) {
      out.appendByte(BACKSLASH);
      out.appendByte(DOLLAR);
    } else {
      out.appendByte(b);
    }
    i += 1;
  }
}
  • 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