Skip to content

Commit ffada2f

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents 47b57b0 + cbddb57 commit ffada2f

171 files changed

Lines changed: 1040 additions & 442 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/packages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ To ease initial development, the project includes various [snippets][stdlib-snip
7070
Once you are ready to begin creating a new package, we recommend the following order:
7171

7272
1. **Write the `README`**. README first development helps you identify use cases, clarify intended behavior and edge cases, and refine package scope and intent.
73-
2. **Create the `package.json`**. The `package.json` file will include the package description and indicate whether the package has a command-line interface (CLI). Add keywords you think will help users identify the project.
73+
2. **Create the `package.json`**. The `package.json` file will include the package description, indicate whether the package has a command-line interface (CLI), and include keywords you think will help users identify the project.
7474
3. **Write examples**. The `README` should include a main example which can be immediately transferred to a main example file. Other examples may explore additional methods, edge cases, and uses.
7575
4. **Write the implementation**. While some will argue that tests should come before the implementation, our experience is that writing an implementation against the example is nearly as effective and requires less upfront investment when ideas are still being formed. We too often write tests and then write an implementation against those tests, only to realize that the approach is flawed, necessitating two refactorings, rather than one. Accordingly, our general recommendation is README/example driven development, but you are free to pursue the approach which bests suits your tastes and workflow.
7676
5. **Write benchmarks**. Benchmarks typically require less work than unit tests, but they often help flag potential performance cliffs and a need to rethink a particular approach and implementation. In our experience, writing benchmarks before tests helps minimize the number, and extent, of refactorings.

etc/eslint/rules/stdlib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var rules = {};
3434
* return out;
3535
* }
3636
*/
37-
rules[ 'stdlib/empty-line-before-comment' ] = 'off'; // FIXME
37+
rules[ 'stdlib/empty-line-before-comment' ] = 'error';
3838

3939

4040
// EXPORTS //

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# empty-line-before-comment
22

3-
> [ESLint rule][eslint-rules] to enforce empty lines before single-line comments.
3+
> [ESLint rule][eslint-rules] to enforce empty lines before comments.
44
55
<section class="intro">
66

@@ -18,11 +18,11 @@ var rule = require( '@stdlib/_tools/eslint/rules/empty-line-before-comment' );
1818

1919
#### rule
2020

21-
[ESLint rule][eslint-rules] to enforce empty lines before single-line comments.
21+
[ESLint rule][eslint-rules] to enforce empty lines before comments.
2222

23-
```javascript
24-
/* eslint-disable */
23+
<!-- eslint-disable -->
2524

25+
```javascript
2626
// Bad...
2727
function square( x ) {
2828
var x;

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/**
4-
* ESLint rule to enforce empty lines before single-line comments.
4+
* ESLint rule to enforce empty lines before comments.
55
*
66
* @module @stdlib/_tools/eslint/rules/empty-line-before-comment
77
*

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/lib/main.js

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
'use strict';
22

3+
// MODULES //
4+
5+
var isArray = require( '@stdlib/assert/is-array' );
6+
7+
8+
// VARIABLES //
9+
10+
var RETURN_ANNOTATION_REGEXP = /\s*(\* ){0,1}(\/\/|\/\*) (e\.g\.,){0,1}(returns|=>)/;
11+
var CASE_STATEMENT = /case [^:]+:/;
12+
13+
314
// MAIN //
415

516
/**
6-
* Rule for validating that single-line comments are preceded by empty lines.
17+
* Rule for validating that comments are preceded by empty lines.
718
*
819
* @param {Object} context - ESLint context
920
* @returns {Object} validators
@@ -16,21 +27,19 @@ function main( context ) {
1627
*
1728
* @private
1829
* @param {ASTNode} node - node to report
19-
* @returns {void}
2030
*/
2131
function report( node ) {
2232
context.report({
2333
'node': node,
24-
'message': 'Missing empty line before single-line comment'
34+
'message': 'Missing empty line before comment'
2535
});
2636
} // end FUNCTION report()
2737

2838
/**
29-
* Checks whether single-line comments are preceded by empty lines.
39+
* Checks whether comments are preceded by empty lines.
3040
*
3141
* @private
3242
* @param {ASTNode} node - node to examine
33-
* @returns {void}
3443
*/
3544
function validate( node ) {
3645
var startsLine;
@@ -43,24 +52,48 @@ function main( context ) {
4352
var i;
4453

4554
comments = source.getCommentsInside( node );
46-
firstLine = source.getFirstToken( node ).loc.start.line;
47-
for ( i = 0; i < comments.length; i++ ) {
48-
current = comments[ i ];
49-
line = current.loc.start.line;
50-
prevLine = source.lines[ line-2 ];
51-
token = source.getTokenBefore( current );
52-
if ( token.loc.start.line === line ) {
53-
startsLine = false;
54-
} else {
55-
startsLine = true;
56-
}
57-
if (
58-
startsLine &&
59-
prevLine !== '' &&
60-
firstLine !== line - 1 &&
61-
token.value !== '{'
62-
) {
63-
report( node );
55+
if ( isArray( comments ) && comments.length > 0 ) {
56+
firstLine = source.getFirstToken( node ).loc.start.line;
57+
for ( i = 0; i < comments.length; i++ ) {
58+
current = comments[ i ];
59+
line = current.loc.start.line;
60+
prevLine = source.lines[ line-2 ];
61+
token = source.getTokenBefore( current );
62+
if ( token.loc.start.line === line ) {
63+
startsLine = false;
64+
} else {
65+
startsLine = true;
66+
}
67+
if (
68+
// Flag single-line comments which are not preceded by an empty line:
69+
prevLine !== '' &&
70+
71+
// Only flag comments that start a line:
72+
startsLine &&
73+
74+
// Don't raise an error when comment is at the beginning of a code block:
75+
firstLine !== line - 1 &&
76+
77+
// Don't raise an error when the comment is preceded by the opening curly brace of e.g. an object literal or if-clause:
78+
token.value !== '{' &&
79+
80+
// Don't raise an error when the comment is preceded by the opening square bracket of an array literal:
81+
token.value !== '[' &&
82+
83+
// Don't raise an error when the comment is after the end of a preceding code block:
84+
token.value !== '}' &&
85+
86+
// Don't raise an error when the comment is at the start of the condition of an if-clause:
87+
token.value !== '(' &&
88+
89+
// Don't raise an error when the comment is after a case statement:
90+
!CASE_STATEMENT.test( prevLine ) &&
91+
92+
// Don't raise an error for return annotations in example code:
93+
!RETURN_ANNOTATION_REGEXP.test( current )
94+
) {
95+
report( node );
96+
}
6497
}
6598
}
6699
} // end FUNCTION validate()
@@ -78,7 +111,7 @@ function main( context ) {
78111
module.exports = {
79112
'meta': {
80113
'docs': {
81-
'description': 'enforce empty lines before single-line comments'
114+
'description': 'enforce empty lines before comments'
82115
},
83116
'schema': []
84117
},

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@stdlib/_tools/eslint/rules/empty-line-before-comment",
33
"version": "0.0.0",
4-
"description": "ESLint rule to enforce empty lines before single-line comments.",
4+
"description": "ESLint rule to enforce empty lines before comments.",
55
"author": {
66
"name": "The Stdlib Authors",
77
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/test/fixtures/invalid.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test = {
1313
].join( '\n' ),
1414
'errors': [
1515
{
16-
'message': 'Missing empty line before inline comment',
16+
'message': 'Missing empty line before comment',
1717
'type': 'FunctionDeclaration'
1818
}
1919
]
@@ -35,7 +35,67 @@ test = {
3535
].join( '\n' ),
3636
'errors': [
3737
{
38-
'message': 'Missing empty line before inline comment',
38+
'message': 'Missing empty line before comment',
39+
'type': 'FunctionDeclaration'
40+
}
41+
]
42+
};
43+
invalid.push( test );
44+
45+
test = {
46+
'code': [
47+
'function square( x ) {',
48+
' var x;',
49+
'',
50+
' // Square the number:',
51+
' // y = x^2 = x*2',
52+
' return x*x;',
53+
'}'
54+
].join( '\n' ),
55+
'errors': [
56+
{
57+
'message': 'Missing empty line before comment',
58+
'type': 'FunctionDeclaration'
59+
}
60+
]
61+
};
62+
invalid.push( test );
63+
64+
test = {
65+
'code': [
66+
'function makePerson() {',
67+
' var person = {',
68+
' // Job title:',
69+
' \'title\': \'engineer\',',
70+
' // Last name:',
71+
' \'name\': \'Susan\'',
72+
' };',
73+
' return person;',
74+
'}'
75+
].join( '\n' ),
76+
'errors': [
77+
{
78+
'message': 'Missing empty line before comment',
79+
'type': 'FunctionDeclaration'
80+
}
81+
]
82+
};
83+
invalid.push( test );
84+
85+
test = {
86+
'code': [
87+
'function makePerson() {',
88+
' var person = {',
89+
' \'title\': \'engineer\',',
90+
' \'name\': \'Susan\'',
91+
' };',
92+
' // Return the object:',
93+
' return person;',
94+
'}'
95+
].join( '\n' ),
96+
'errors': [
97+
{
98+
'message': 'Missing empty line before comment',
3999
'type': 'FunctionDeclaration'
40100
}
41101
]

0 commit comments

Comments
 (0)