Skip to content

Extending

tannerbaum edited this page Feb 26, 2020 · 2 revisions

You can add custom rule implementations in your config. customRules is an array of objects like this:

{
	// your custom rule's name
	name: string,
	
	// is the rule looking at an individual script or the whole scripts object?
	isObjectRule: boolean,
	
	// the error message if the validation fails for this rule, uses template tags {{name}} (and {{names}} for object rules)
	message: string,
	
	// the validation function for the rule
	// for object rules it returns the guilty scripts on validation failure
	// meaning: if isObjectRule === true: (scripts: Object) => true | Array<string>
	// for script rules it returns true or false
	// meaning: if isObjectRule === false: (key: string, script: string, scripts: Object) => boolean
	validate: function
	
	// if the problem is autofixable, provide a fix function here
	// for object rules it returns a fixed scripts object
	// for script rules it returns an array with [key: string, value: string]
	fix: function
}

As an example here's a custom config (.scriptlintrc.js) to override the camelCase rule and replace it with a kebab-case one:

module.exports = {
  rules: {
    "correct-casing": false,
    "correct-kebab-casing": true
  },
  customRules: [
    {
      name: "correct-kebab-casing",
      isObjectRule: false,
      message: "`{{name}}`: Script name must be kebab case",
      validate: name => /^[\d:a-z\-]+$/.test(name)
    }
  ]
};


scriptlint status
npm version badge
dependency badge
Issue badge
CI badge

Clone this wiki locally