Skip to content

Built-in rules

Checks the resolved variant against package.json, TypeScript configs, additional JSON files, and text files.

monoswan.config.ts
import { defineConfig, enforceVariants } from "monoswan";
export default defineConfig({
rules: [enforceVariants()],
});

Checks package.json property order and provides an autofix. The default follows common package.json ordering and alphabetizes dependency groups. This is defined in DEFAULT_PACKAGE_JSON_SORT_OPTIONS.

Sorting options support alphabetical, fixed, custom, and none orders. Use children for nested values and overrides for named properties.

  • alphabetical compares keys or string-array entries lexicographically.
  • fixed requires a keys array. Listed keys appear first in that order; unlisted keys follow using fallbackCompare, which defaults to alphabetical order.
  • custom requires a compare(keyA, keyB) function.
  • none disables sorting at that location.
  • children applies one sorting rule recursively to child values.
  • overrides assigns a different rule to a named property and takes precedence over children.

The default orders common package fields, sorts dependency groups and scripts alphabetically, and sorts selected string arrays such as files, keywords, and bundled dependencies. Its autofix rewrites package.json with tab indentation and a trailing newline. Import DEFAULT_PACKAGE_JSON_SORT_OPTIONS to extend or inspect the complete default.

sortPackageJson({
order: "fixed",
keys: ["name", "version", "private"],
fallbackCompare: (left, right) => left.localeCompare(right),
overrides: {
scripts: { order: "alphabetical" },
},
});
monoswan.config.ts
import { defineConfig, sortPackageJson } from "monoswan";
export default defineConfig({
rules: [sortPackageJson()],
});

Requires a package-level monoswan config. Set requireVariant: true to require at least one selected variant. Missing config has an autofix that creates monoswan.json.

monoswan.config.ts
import { defineConfig, requireMonoswanConfig } from "monoswan";
export default defineConfig({
rules: [requireMonoswanConfig({ requireVariant: true })],
});