|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2018 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var startsWith = require( '@stdlib/string/starts-with' ); |
| 24 | +var isCapitalized = require( '@stdlib/assert/is-capitalized' ); |
| 25 | +var endsWith = require( '@stdlib/string/ends-with' ); |
| 26 | + |
| 27 | + |
| 28 | +// VARIABLES // |
| 29 | + |
| 30 | +var RE_NUM_SPECIAL = /^[\d`]/; |
| 31 | + |
| 32 | + |
| 33 | +// MAIN // |
| 34 | + |
| 35 | +/** |
| 36 | +* Rule for enforcing styling of tag descriptions. |
| 37 | +* |
| 38 | +* @param {Context} context - lint context |
| 39 | +* @returns {Object} validators |
| 40 | +*/ |
| 41 | +function main( context ) { |
| 42 | + var whitelist; |
| 43 | + var opts; |
| 44 | + |
| 45 | + opts = context.options[ 0 ]; |
| 46 | + whitelist = opts.whitelist || []; |
| 47 | + |
| 48 | + /** |
| 49 | + * Checks whether style rules for tag descriptions are obeyed. |
| 50 | + * |
| 51 | + * @private |
| 52 | + * @param {Object} section - parameters or returns section |
| 53 | + */ |
| 54 | + function tagDescriptionStyle( section ) { |
| 55 | + var desc; |
| 56 | + var tags; |
| 57 | + var i; |
| 58 | + |
| 59 | + tags = section.tags; |
| 60 | + if ( tags ) { |
| 61 | + for ( i = 0; i < tags.length; i++ ) { |
| 62 | + desc = tags[ i ].description; |
| 63 | + if ( !endsWith( desc, '.' ) ) { |
| 64 | + context.report( 'Tag description must end with a period', section ); |
| 65 | + } |
| 66 | + if ( !hasValidStart( desc ) ) { |
| 67 | + context.report( 'Tag description must be capitalized', section ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return { |
| 73 | + 'parameters': tagDescriptionStyle, |
| 74 | + 'returns': tagDescriptionStyle |
| 75 | + }; |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks whether a tag description starts with a capitalized letter or number (save for exceptions such as proper nouns). |
| 79 | + * |
| 80 | + * @private |
| 81 | + * @param {string} desc - tag description |
| 82 | + * @returns {boolean} boolean indicating whether tag description is valid |
| 83 | + */ |
| 84 | + function hasValidStart( desc ) { |
| 85 | + var found; |
| 86 | + var word; |
| 87 | + var i; |
| 88 | + |
| 89 | + if ( !isCapitalized( desc ) && !desc.match( RE_NUM_SPECIAL ) ) { |
| 90 | + // Check whether description starts with a word from the whitelist: |
| 91 | + found = false; |
| 92 | + for ( i = 0; i < whitelist.length; i++ ) { |
| 93 | + word = whitelist[ i ]; |
| 94 | + if ( startsWith( desc, word+' ' ) ) { |
| 95 | + found = true; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + // If not, raise an error: |
| 100 | + if ( !found ) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + return true; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +// EXPORTS // |
| 110 | + |
| 111 | +module.exports = main; |
0 commit comments