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
3.2 KiB
Raw
/**
* `console` methods take any value and format it with `toString`, so a
* string conversion around the argument can go.
*
* console.log(`${x}`); → console.log(x);
* console.log(i32ToString(x)); → console.log(x);
*/
import {
Expression, CallExpression, MemberExpression, Identifier, TemplateLiteral,
} from 'compiler/ast';
import { walkEnter } from 'compiler/visitor';
import { typeToString } from 'compiler/types';
import { RuleContext } from '../context.zena';
export function consoleToString(ctx: RuleContext): void {
walkEnter(ctx.ast, (n) => {
if (n is CallExpression) {
rewrite(ctx, n);
}
});
}
function rewrite(ctx: RuleContext, call: CallExpression): void {
if (call.optional || call.typeArguments != null || call.args.length != 1) {
return;
}
if (!(call.callee is MemberExpression)) {
return;
}
let callee = call.callee as MemberExpression;
if (callee.optional || callee.isSymbolAccess || !isConsoleMethod(callee.property.name) ||
!isConsole(ctx, callee.object)) {
return;
}
let arg = call.args[0];
let inner = unwrap(ctx, arg);
if (inner === arg) {
return;
}
let text = ctx.text(inner);
if (text.length == 0) {
return;
}
ctx.replace(arg, text);
}
function isConsoleMethod(name: String): boolean {
return name == 'log' || name == 'error' || name == 'warn' ||
name == 'info' || name == 'debug';
}
function isConsole(ctx: RuleContext, object: Expression): boolean {
let t = ctx.typeOf(object);
if (t == null) {
return false;
}
let name = typeToString(ctx.unwrap(t));
return name == 'Console' || name == 'HostConsole' || name == 'ComponentConsole';
}
function unwrap(ctx: RuleContext, expr: Expression): Expression {
var current = expr;
while (!ctx.hasComment(current)) {
let next = unwrapOnce(ctx, current);
if (next == null) {
break;
}
current = next;
}
return current;
}
function unwrapOnce(ctx: RuleContext, expr: Expression): Expression | null {
if (let TemplateLiteral {quasis, expressions} = expr) {
if (expressions.length == 1 && quasis.length == 2 &&
quasis[0].raw.length == 0 && quasis[1].raw.length == 0) {
return expressions[0];
}
return null;
}
if (let CallExpression {callee, args, typeArguments, optional} = expr) {
if (optional || typeArguments != null) {
return null;
}
if (args.length == 1 && callee is Identifier) {
let sym = ctx.symbolOf(callee);
if (sym == null) {
return null;
}
let target = sym.resolveTarget();
if (!target.modulePath.startsWith('zena:')) {
return null;
}
let inner = args[0];
let name = target.name;
if (name == 'toString' || name == 'i32ToString' || name == 'f64ToString' ||
name == 'boolToString') {
return inner;
}
if ((name == 'u32ToString' || name == 'i64ToString' || name == 'u64ToString' ||
name == 'f32ToString') && !ctx.isContextuallyTyped(inner)) {
return inner;
}
return null;
}
if (args.length == 0 && callee is MemberExpression) {
let member = callee;
if (!member.optional && member.isSymbolAccess && member.property.name == 'toString') {
return member.object;
}
}
}
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 |
|
|