Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Plugin API

zxqfox edited this page Nov 11, 2014 · 11 revisions

Plugin API

Plugin example

index.js:

module.exports = function (conf) {
  // plugin loading
  conf.registerRule(require('./lib/rules/your-rule.js'));
};

./lib/rules/your-rule.js (here you can take a look at rules in jscs repo):

module.exports = function () {};
module.exports.prototype.getOptionName = function () {
  return 'yourRule';
};
module.exports.prototype.configure = function (options) {
  assert(options === true || typeof options === 'object');
  // rule preparation and configuration
  this._options = options;
};
module.exports.prototype.check = function (file, errors) {
  // checking file
  if (this._options.color === 'red') {
    errors.add('red color', 1, 0);
  }
  file.iterate(function(node, parentNode, parentCollection) {
    if (!isValid(node)) {
      errors.add('invalid node found', node.loc.start);
    }
  });
  if (true) { // add an error
    errors.add('some error', 1, 0);
  }
};

./package.json:

{
  "devDependencies": {
    "jscs": ">=1.8.0 <2.0.0"
  },
  "peerDependencies": {
    "jscs": ">=1.8.0 <2.0.0"
  }
}

Using your plugin

To load plugin you should just add it to plugins (or add your rules to additionalRules before v1.8):

.jscsrc with plugins list

{
  "plugins": ["your-plugin-package-name"],
  "yourRule": true
}

.jscsrc with additionalRules list

{
  "additionalRules": ["node_modules/your-plugin-package-name/lib/rules/*.js"],
  "yourRule": true
}
Clone this wiki locally