ViewLint Logo

ViewLint

TypeScript API Reference

Use ViewLint Functionality Directly

The ViewLint TypeScript API is useful when you want to run ViewLint from a script, build a custom integration, or lint custom Targets programmatically.

Since ViewLint is developed natively in TypeScript, the type hints will be particularly helpful when developing.

Note that much of this API overlaps with the API from the configuration files, as the CLI and configuration files are powered by this set of APIs under-the-hood.

ViewLint

import { ViewLint } from "viewlint";

new ViewLint(options?)

The constructor accepts an options object:

  • overrideConfigFile: string | undefined: Use a specific config file path.
  • baseConfig: Config | Config[] | undefined: Applied before the discovered config file.
  • overrideConfig: Config | Config[] | undefined: Applied after everything else.
  • plugins: Record<string, Plugin> | undefined: Extra plugins (in addition to config).

Lint targets

import { ViewLint } from "viewlint";
import rules from "@viewlint/rules";
import { defaultView, defineConfig } from "viewlint/config";

const viewlint = new ViewLint({
  baseConfig: defineConfig({
    plugins: { rules },
    extends: ["rules/recommended"],
  }),
});

const results = await viewlint.lintTargets([
  {
    view: defaultView,
    options: {
      context: {
        baseURL: "https://example.com",
      },
    },
  },
]);

The API is very helpful when you want to generate targets dynamically. Note that you cannot use the named configurations directly via their string in your config. However, you are able to import their objects and use them directly. For example:

import { ViewLint } from "viewlint";
import myConfig from "./viewlint.config.ts"

const viewlint = new ViewLint({
  baseConfig: myConfig,
});

const results = await viewlint.lintTargets([
  {
    view: myConfig.views.loggedin,
    options: myConfig.options.local,
  },
]);

Format results

ViewLint ships with two built-in formatters: stylish and json.

const formatter = await viewlint.loadFormatter("stylish");
const output = await formatter.format(results);
process.stdout.write(output);

Config helpers

import { defineConfig, defineViewFromActions } from "viewlint/config";

See: Configuration Files and Configuring Views

Rule helper

import { defineRule } from "viewlint/plugin";

See: Custom Rules Reference

On this page