CKStyle插件的开发规范 #19

Closed
wangjeaf opened this Issue Sep 17, 2014 · 0 comments

Comments

Projects
None yet
1 participant
@wangjeaf
Owner

wangjeaf commented Sep 17, 2014

CKStyle插件规范

插件放置的目录

放置在ckstyle/plugins目录下的所有文件(helper.js和index.js除外),每一个文件都对应一种检查规则。

插件的开发规范

开发时可自行添加和修改,但是必须满足以下条件:

1、文件必须以FED开头

2、文件中必须包含与文件名相同的类名,比如FEDNoExpression.js中包含FEDNoExpression类,并注册到global对象上

3、类必须继承自RuleChecker/RuleSetChecker/StyleSheetChecker

4、类中必须包含check方法,并且传入rule/ruleSet/styleSheet作为参数,并且返回True(通过)/False(不通过)或错误信息数组

5、如果check返回bool值,则类中必须包含errorLevel和errorMsg属性,便于检测异常时给出错误提示

6、errorMsg中可以包含 ${selector}/${name}/${value}等属性设置,在错误提示时将进行相应替换

7、每一个规则,需要在test目录中添加对应的单元测试用例,测试用例请参见"CKStyle Unit Test" #18

8、每一个插件,需要按照json格式,编写此类对应的doc属性,以便为官网的此规则生成选项。

9、插件类可以指定一个private属性,如果是private,则不会在官网中生成选项,而且private的类自动必选。

Nodejs插件示例 Demo

var base = require('../base')
var ERROR_LEVEL = base.ERROR_LEVEL
var Class = base.Class
var RuleChecker = base.RuleChecker
var helper = require('./helper');

module.exports = global.FEDNoZeroBeforeDot = new Class(RuleChecker, function() {

    this.__init__ = function(self) {
        self.id = 'no-zero-before-dot'
        self.errorLevel = ERROR_LEVEL.WARNING
        self.errorMsg = 'zero should be removed when meet 0.xxx in "${selector}"'
    }

    this.check = function(self, rule, config) {
        var value = rule.value

        if (self._startsWithZeroDot(value))
            return false

        var values = rule.value.split(' ')
        for(var i = 0; i < values.length; i++) {
            var v = values[i]
            if (self._startsWithZeroDot(v.trim()))
                return false
        }

        return true 
    }

    this.fix = function(self, rule, config) {
        var fixedValue = rule.fixedValue
        fixedValue.split(' ').forEach(function(v) {
            if (self._startsWithZeroDot(v))
                rule.fixedValue = rule.fixedValue.replace(v, v.slice(1))
        })
    }

    this._startsWithZeroDot = function(self, value) {
        return value.indexOf('0.') == 0
    }

    this.__doc__ = {
        "summary":"删除0.x前面的0",
        "desc":" 0.xxx 前面的 0 是可以删除的,以实现更好的压缩。例如<br>\
            <code>0.3px ==> .3px</code><br><br>\
            <code>rgba(0,0,0,0.3)<code><br>\
            <code>==></code><br>\
            <code>rgba(0,0,0,.3)</code>"
    }
})

@wangjeaf wangjeaf added the doc label Sep 17, 2014

@wangjeaf wangjeaf closed this Sep 17, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment