Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | // Copyright (c) 2015-present, salesforce.com, inc. All rights reserved // Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license const map = require('map-stream'); const gutil = require('gulp-util'); const _ = require('lodash'); const yaml = require('js-yaml'); // Token linting tasks // // Documentation: // https://github.com/salesforce-ux/design-system/wiki/Design-Tokens const prefixes = [ 'BORDER_RADIUS', 'BORDER_WIDTH', 'COLOR', 'COLOR_BACKGROUND', 'COLOR_BORDER', 'COLOR_TEXT', 'DURATION', 'ELEVATION', 'FONT_FAMILY', 'FONT_WEIGHT', 'FONT_SIZE', 'FLEX', 'LINE_HEIGHT', 'MAX_WIDTH', 'MAX_HEIGHT', 'MQ', 'OPACITY', 'HEIGHT', 'SIZE', 'SHADOW', 'SPACING', 'SQUARE', 'TIMING', 'WIDTH', 'Z_INDEX' ]; /** * Define default reporters */ const tokenReporter = lint => gutil.log(JSON.stringify(lint)); const verboseReporter = (lint, file) => lint.errors.forEach(error => gutil.log('tokenlint', `${error.error}: ${error.token} in ${file.path}`) ); const prefixLint = tokenName => prefixes.some(prefix => tokenName.startsWith(prefix)); const TokenLint = function(tokens, pluginOptions = {}) { let self = this; if (!(self instanceof TokenLint)) { return new TokenLint(tokens, pluginOptions); } self.options = _.defaults(pluginOptions || {}, { prefix: false, // TODO: update prefix uppercase: true, characterRange: true }); self.errors = []; _.keys(tokens).forEach(token => { try { self.tokenNameLint(token); } catch (e) { e.forEach(error => self.errors.push({ token: token, error: error }) ); } }); return { errors: self.errors }; }; TokenLint.prototype.tokenNameLint = function(tokenName) { let errors = []; if (this.options.prefix && !prefixLint(tokenName)) { errors.push( 'Token names should be prefixed appropriately (see https://github.com/salesforce-ux/design-system/wiki/Design-Tokens#token-naming--organization)' ); } if (this.options.uppercase && tokenName.toUpperCase() !== tokenName) { errors.push('Token names should be uppercase'); } if (this.options.characterRange && /[^A-Z0-9_]/.test(tokenName)) { errors.push( 'Token names should only contain uppercase letters, underscores and numbers' ); } if (errors.length) { throw errors; } }; /** * Main plugin function */ const tokenlintPlugin = (pluginOptions = {}) => map(function(file, cb) { const tokens = yaml.safeLoad(file.contents.toString('utf8')); const tokenList = tokens.props ? tokens.props : tokens.aliases ? tokens.aliases : {}; const result = TokenLint(tokenList, pluginOptions); file.tokenlint = { errors: result.errors }; // Pass file cb(null, file); }); tokenlintPlugin.report = reporter => map(function(file, cb) { let error = null; if (file.tokenlint.errors) { if (file.tokenlint.errors.length) { error = new gutil.PluginError( 'tokenlint', `Found ${file.tokenlint.errors.length} linting error(s)` ); } if (reporter === 'json') { tokenReporter(file.tokenlint); } else if (reporter === 'verbose') { verboseReporter(file.tokenlint, file); } else if (_.isFunction(reporter)) { reporter(file.tokenlint, file); } } // Pass file cb(error, file); }); module.exports = tokenlintPlugin; |