Skip to content

Optional Oxlint plugin

The @kensio/smartass package also exports an optional Oxlint plugin that discourages less specific assertion usage. For example:

assertIdentical(foo.length, 2);
// suggested improvement:
assertArrayLength(foo, 2);

The plugin reports the same suggestions as the optional ESLint config: both are generated from a single table of selectors, so the two linters report identically and you only need whichever one you already run.

The @kensio/smartass/oxlint export is an Oxlint JS plugin. Register it under jsPlugins in your .oxlintrc.json and turn the rule on:

{
"jsPlugins": [{ "name": "smartass", "specifier": "@kensio/smartass/oxlint" }],
"rules": {
"smartass/prefer-specific-assertions": "warn"
}
}

The name you give the plugin is the prefix the rule is configured under, so a different name means a different rule name — "name": "sa" would make the rule sa/prefer-specific-assertions.

smartass/prefer-specific-assertions is the plugin’s only rule. It takes no options, so "warn" or "error" is the whole of its configuration. It reports a suggestion wherever a broad assertion is doing a job one of the specific assertions describes better, and never rewrites your code: every report is a message, with no fixer.

Only calls to smartass assertion functions are matched — every selector is anchored to an assert* call by name, so an expect() or a helper of your own with a similar shape is left alone.

Some examples of the replacements the rule will suggest:

  • assertIdentical(value, true)assertTrue(value)
  • assertIdentical(value, false)assertFalse(value)
  • assertIdentical(value, undefined)assertUndefined(value)
  • assertTrue(value != null)assertNonNullable(value)
  • assertIdentical(typeof value, "string")assertTypeString(value)
  • assertIdentical(value instanceof MyClass, true)assertInstanceOf(value, MyClass)
  • assertIdentical("key" in object, true)assertObjectHasProperty(object, "key")
  • assertIdentical(value.length, expected)assertArrayLength() etc.
  • assertIdentical(value.size, expected)assertSetSize() etc.
  • assertTrue(array.length > 0)assertArrayNotEmpty(array)
  • assertTrue(value.includes(expected))assertStringIncludes() etc.
  • assertTrue(value.startsWith(prefix))assertStringStartsWith(value, prefix)
  • assertTrue(value.endsWith(suffix))assertStringEndsWith(value, suffix)
  • assertTrue(stats.isDirectory())assertDirectoryExists() etc.

JS plugins are loaded from a jsPlugins entry, which needs an Oxlint version that supports them. The plugin is exercised against Oxlint’s own RuleTester, from oxlint/plugins-dev, on Oxlint 1.77 and later.

@kensio/smartass/oxlint does not depend on Oxlint itself: the plugin types are declared structurally, so adding the plugin adds no Oxlint version constraint to your install.