@alint-js/core

中文文档
0.7.3发布于 2026/09/29MITTypeScriptES Module近 30 天下载 9.4万

Public rule and plugin SDK and run engine for alint

原始使用文档
文档目录6 节
  1. What it does
  2. How to use
  3. Languages
  4. When to use
  5. When not to use
  6. Memory boundaries

@alint-js/core

The public SDK and run engine for alint.

What it does

This package provides the core SDK and run engine APIs used by plugins, rules, language processors, embedding tools, and agent adapters:

  • definePlugin and defineRule
  • runAlint
  • rule registry and flat config normalization
  • source runtime helpers
  • built-in JavaScript source extraction, with additional language support for others
  • model resolution by size and capability
  • diagnostics and progress payload types
  • framework-neutral agent contracts under @alint-js/core/agent
  • tool-call structured output under @alint-js/core/structured-output
  • config DSL and types for advanced SDK consumers

How to use

Write a rule:

import { defineRule } from '@alint-js/core'

export const rule = defineRule({
  create: ctx => ({
    async onTargetFile(target) {
      const model = await ctx.model({ size: 'small' })
      const file = await ctx.src.readFile(target.file)

      ctx.report({
        filePath: target.file.path,
        loc: target.loc,
        message: `reviewed ${file.lines.length} lines with ${model.id}`,
      })
    },
  }),
})

Source and project rules receive compact descriptors. Source handlers should pass target.file to ctx.src.readFile() so core can detect changes since planning. Read source only when needed:

import { defineRule } from '@alint-js/core'

export const projectRule = defineRule({
  create: ctx => ({
    async onTargetProject(project) {
      for (const entry of project.files) {
        const file = await ctx.src.readFile(entry.path)
        if (file.text.includes('deprecated-api'))
          ctx.report({ filePath: entry.path, message: 'deprecated API used' })
      }
    },
  }),
})

PlannedSourceTarget, ProjectFileEntry, and ProjectTargetEntry intentionally omit source text. Calls to ctx.src.readFile() are explicit and plugin-owned, so a rule controls which source files it loads and how long it retains them. Cache hits do not invoke the handler and therefore do not perform its execution-time read.

Use the agent contract for tool-using rules:

import { requireAgent } from '@alint-js/core/agent'

const agent = requireAgent(ctx)

Ask a model for one validated, typed result with @alint-js/core/structured-output. It forces the model to call a single reporting tool whose arguments match a valibot schema, validates them, and retries with the validation error fed back to the model:

import { generateStructured } from '@alint-js/core/structured-output'
import { array, description, object, pipe } from 'valibot'

const responseSchema = pipe(
  object({ findings: array(findingSchema) }),
  description('Report findings for this file.'),
)

const { findings } = await generateStructured({
  createMessages: retryFeedback => [
    { content: prompt, role: 'system' },
    ...(retryFeedback ? [{ content: retryFeedback, role: 'user' as const }] : []),
    { content: numberedSource, role: 'user' },
  ],
  logger: ctx.logger,
  metering: ctx.metering,
  model: await ctx.model(),
  operation: 'my-rule-judge',
  schema: responseSchema,
})

The reporting tool is named reportFindings by default (toolName overrides it) and its description defaults to the schema's valibot description(...). toolParametersFromSchema, formatSourceWithLineNumbers, and formatOutputLanguageInstruction are exported for callers that build their own tools or prompts. Use ctx.agent instead when the model needs to explore with tools before answering, because a forced tool call is a single shot, not a loop.

Languages

Core parses JavaScript and TypeScript. Everything else is registered by a plugin, so a rule says which languages it can read and core decides what to hand it:

defineRule({
  create: () => ({ onTargetFunction: (target) => { /* ... */ } }),
  languages: 'any',
})
languagesthe rule receivesa named language nothing registered
omittedfile targets only, never functions or classesnot applicable
'any'every language except plaintextnever fails
['go', 'rust']those languages onlyrun fails, alint/missing-language
{ ids: ['go'], skipMissing: true }those languages onlyskipped quietly

'any' excludes plaintext on purpose. Plain text is what a file falls back to when no language claims its extension, so a rule that asked for a language would otherwise be handed unparsed text. When that happens the run reports alint/unregistered-language once per extension — a warning by default, configurable through linterOptions.reportUnregisteredLanguages.

Declaring a list is the stricter choice. It fails the run when the user has not installed a pack that provides one of them, rather than letting the rule match nothing and look like a pass.

A plugin registers a language by describing how to turn a file into targets:

definePlugin({
  languages: {
    zig: {
      extensions: ['.zig'],
      extract: file => [/* SourceTarget[] */],
      name: 'zig',
    },
  },
})

Ids are the identifiers editors use — go, python, typescript, plaintext. Registering fails on a duplicate name or extension, so two plugins can never claim the same language.

Put a FunctionInfo under metadata.function on each function target, and the file's call sites under metadata.calls on the file target. Those two keys are what let a rule read any language without a parser of its own.

@alint-js/languages provides Go, Python and Rust this way.

To parse a file the run was not asked to lint, such as when building a workspace-wide index, use ctx.src.extract(path). It resolves that file's own config and language, and returns nothing for a file the config ignores rather than throwing.

When to use

  • You are writing an alint plugin or rule package.
  • You are adding a language processor or source extractor.
  • You are embedding alint in another tool.
  • You are implementing an AgentAdapter.
  • You need project-wide analysis that can consume compact descriptors and load source lazily.

When not to use

  • Use @alint-js/cli for command-line usage and ordinary alint.config.* files.
  • Use @alint-js/config for setup TOML, config loading, and config-file tooling only.
  • A plugin that needs a persistent repository database should build and inject that database. Do not retain every SourceFile returned by ctx.src.readFile() as a substitute for one.

Memory boundaries

alint bounds concurrent source planning reads and parser work. Planning releases rich extractor values as soon as compact jobs are admitted; queued jobs do not retain source text. It cannot bound source files retained by plugin code after ctx.src.readFile() returns.

A valid cache written by the same alint version is still read from one monolithic JSON document. An extremely large cache may therefore exhaust available memory.

After each cacheable rule job completes, alint writes a cache checkpoint before releasing its scheduler slot. Each checkpoint atomically replaces the monolithic cache file. An interrupted run can therefore reuse every result that had already become durable, but large caches can cause substantial disk writes during runs with many cache misses. Cache hits, skipped jobs, failed jobs, and rules that opt out of caching do not add checkpoint writes. A checkpoint or final cache write error is fatal and causes the run to fail.

版本与发布记录

@alint-js/core 共发布 51 个版本,首次发布于 2026/07/02;过去 12 个月发布了 51 个版本。

版本发布日期距今
0.7.3latest2026/09/291 个月前
0.7.22026/09/281 个月前
0.7.12026/08/292 个月前
0.7.02026/08/292 个月前
0.6.02026/08/192 个月前
0.5.02026/08/162 个月前

关于 @alint-js/core 的常见问题

@alint-js/core 是什么?

Public rule and plugin SDK and run engine for alint

如何安装 @alint-js/core?

在项目目录运行 npm install @alint-js/core,或使用 pnpm add / yarn add / bun add。

@alint-js/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alint-js/core 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@alint-js/core 可以商用吗?

@alint-js/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alint-js/core 的下载量有多少?

根据 npm 官方下载统计,@alint-js/core 在 2026/10/06 – 2026/11/04 这 30 天内被下载 94,044 次,日均约 3,135 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alint-js/core 还在维护吗?

最新版本 0.7.3 发布于 2026/09/29(1 个月前);至今共发布 51 个版本,其中过去 12 个月发布了 51 个;首次发布于 2026/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。