Skip to content

Commit 4129461

Browse files
committed
Add REPL lint rule for tag description style
1 parent 3f9ae20 commit 4129461

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "@stdlib/_tools/repl-txt/rules/tag-description-style",
3+
"version": "0.0.0",
4+
"description": "Lint rule to enforce styling of tag descriptions.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"bin": {},
17+
"main": "./lib",
18+
"directories": {
19+
"lib": "./lib"
20+
},
21+
"scripts": {},
22+
"homepage": "https://github.com/stdlib-js/stdlib",
23+
"repository": {
24+
"type": "git",
25+
"url": "git://github.com/stdlib-js/stdlib.git"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/stdlib-js/stdlib/issues"
29+
},
30+
"dependencies": {},
31+
"devDependencies": {},
32+
"engines": {
33+
"node": ">=0.10.0",
34+
"npm": ">2.7.0"
35+
},
36+
"os": [
37+
"aix",
38+
"darwin",
39+
"freebsd",
40+
"linux",
41+
"macos",
42+
"openbsd",
43+
"sunos",
44+
"win32",
45+
"windows"
46+
],
47+
"keywords": [
48+
"stdlib",
49+
"tools",
50+
"tool",
51+
"repl.txt",
52+
"repl",
53+
"lint",
54+
"custom",
55+
"rules",
56+
"rule",
57+
"plugin",
58+
"tag",
59+
"tags",
60+
"description",
61+
"style"
62+
]
63+
}

0 commit comments

Comments
 (0)