Skip to content

Commit a624db1

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents 5cfee64 + 3959a77 commit a624db1

File tree

15 files changed

+1450
-22
lines changed

15 files changed

+1450
-22
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-require-throws-tags/lib/main.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var parseJSDoc = require( 'doctrine' ).parse;
2424
var objectKeys = require( '@stdlib/utils/keys' );
2525
var contains = require( '@stdlib/assert/contains' );
2626
var isObject = require( '@stdlib/assert/is-object' );
27+
var isArray = require( '@stdlib/assert/is-array' );
2728
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2829
var findJSDoc = require( '@stdlib/_tools/eslint/utils/find-jsdoc' );
2930
var unique = require( './unique.js' );
@@ -103,13 +104,11 @@ function extractFunctionBoundaries( node ) {
103104
function main( context ) {
104105
var throwStatements;
105106
var functionHash;
106-
var hasJSDOC;
107107
var source;
108108

109109
source = context.getSourceCode();
110110
functionHash = {};
111111
throwStatements = [];
112-
hasJSDOC = false;
113112

114113
/**
115114
* Reports the error message.
@@ -132,28 +131,63 @@ function main( context ) {
132131
* @param {ASTNode} node - function node
133132
*/
134133
function registerFunction( node ) {
134+
var annotations;
135135
if (
136136
node.id &&
137137
node.id.name &&
138138
!hasOwnProp( functionHash, node.id.name )
139139
) {
140+
annotations = extractThrowsAnnotations( node );
140141
functionHash[ node.id.name ] = {
141142
'name': node.id.name,
142143
'start': node.start,
143144
'end': node.end,
144145
'loc': node.loc,
145-
'throwAnnotations': extractThrowsAnnotations( node ),
146-
'functionBoundaries': extractFunctionBoundaries( node )
146+
'throwAnnotations': annotations || [],
147+
'functionBoundaries': extractFunctionBoundaries( node ),
148+
'hasJSDoc': isArray( annotations )
147149
};
148150
}
149151
}
150152

153+
/**
154+
* Checks whether a function node is assigned as a value to a `Object.defineProperty` expression.
155+
*
156+
* @private
157+
* @param {ASTNode} node - function node
158+
* @returns {(Array|null)} array of error annotation types attached to the object expression if applicable, `null` otherwise
159+
*/
160+
function checkDefineProperty( node ) {
161+
var callee;
162+
var parent;
163+
164+
if ( node ) {
165+
parent = node.parent;
166+
if ( parent ) {
167+
parent = parent.parent;
168+
}
169+
if ( parent ) {
170+
parent = parent.parent;
171+
}
172+
if ( parent ) {
173+
callee = parent.callee;
174+
if (
175+
callee && callee.property &&
176+
callee.property.name === 'defineProperty'
177+
) {
178+
return extractThrowsAnnotations( parent.parent );
179+
}
180+
}
181+
}
182+
return null;
183+
}
184+
151185
/**
152186
* Extracts `@throws` tags from JSDoc comment corresponding to the given node.
153187
*
154188
* @private
155189
* @param {ASTNode} node - function node
156-
* @returns {Array} array of error annotation types
190+
* @returns {(Array|null)} array of error annotation types
157191
*/
158192
function extractThrowsAnnotations( node ) {
159193
var jsdoc;
@@ -162,17 +196,18 @@ function main( context ) {
162196
var out;
163197
var i;
164198

165-
out = [];
166199
jsdoc = findJSDoc( source, node );
167200
if ( isObject( jsdoc ) ) {
168-
hasJSDOC = true;
169201
ast = parseJSDoc( jsdoc.value, DOPTS );
170202
tags = ast.tags;
203+
out = [];
171204
for ( i = 0; i < tags.length; i++ ) {
172205
if ( tags[ i ].title === 'throws' ) {
173206
out.push( tags[ i ].type.name );
174207
}
175208
}
209+
} else {
210+
out = checkDefineProperty( node );
176211
}
177212
return out;
178213
}
@@ -190,20 +225,19 @@ function main( context ) {
190225
var i;
191226
var j;
192227

193-
if ( !hasJSDOC ) {
194-
return;
195-
}
196228
keys = objectKeys( functionHash );
197229
for ( i = 0; i < keys.length; i++ ) {
198230
fcn = functionHash[ keys[ i ] ];
199-
fcn.throwStatements = [];
200-
for ( j = 0; j < throwStatements.length; j++ ) {
201-
stmt = throwStatements[ j ];
202-
if ( isInFunction( stmt, fcn ) ) {
203-
fcn.throwStatements.push( stmt.type );
231+
if ( fcn.hasJSDoc ) {
232+
fcn.throwStatements = [];
233+
for ( j = 0; j < throwStatements.length; j++ ) {
234+
stmt = throwStatements[ j ];
235+
if ( isInFunction( stmt, fcn ) ) {
236+
fcn.throwStatements.push( stmt.type );
237+
}
204238
}
239+
validateFunction( fcn );
205240
}
206-
validateFunction( fcn );
207241
}
208242
}
209243

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-require-throws-tags/test/fixtures/invalid.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@ test = {
227227
};
228228
invalid.push( test );
229229

230+
test = {
231+
'code': [
232+
'/**',
233+
'* Beep boop.',
234+
'*',
235+
'* @name bar',
236+
'* @memberof Foo.prototype',
237+
'* @type {Function}',
238+
'*/',
239+
'Object.defineProperty( Foo.prototype, \'bar\', {',
240+
' \'configurable\': false,',
241+
' \'enumerable\': false,',
242+
' \'writable\': false,',
243+
' \'value\': function bar() {',
244+
' throw new Error( \'beep\' );',
245+
' }',
246+
'});'
247+
].join( '\n' ),
248+
'errors': [
249+
{
250+
'message': '`bar` is missing `@throws {Error}` annotation(s)',
251+
'type': null
252+
}
253+
]
254+
};
255+
invalid.push( test );
256+
230257

231258
// EXPORTS //
232259

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-require-throws-tags/test/fixtures/valid.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,28 @@ test = {
407407
};
408408
valid.push( test );
409409

410+
test = {
411+
'code': [
412+
'/**',
413+
'* Beep boop.',
414+
'*',
415+
'* @name bar',
416+
'* @memberof Foo.prototype',
417+
'* @type {Function}',
418+
'* @throws {Error} beep',
419+
'*/',
420+
'Object.defineProperty( Foo.prototype, \'bar\', {',
421+
' \'configurable\': false,',
422+
' \'enumerable\': false,',
423+
' \'writable\': false,',
424+
' \'value\': function bar() {',
425+
' throw new Error( \'beep\' );',
426+
' }',
427+
'});'
428+
].join( '\n' )
429+
};
430+
valid.push( test );
431+
410432

411433
// EXPORTS //
412434

lib/node_modules/@stdlib/_tools/eslint/rules/repl-namespace-order/lib/main.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ function main( context ) {
6161
if ( prop.key.value === 'alias' ) {
6262
alias = prop.value.value;
6363
if ( alias ) {
64-
if ( prevAlias ) {
65-
if ( alias.localeCompare( prevAlias, 'en', OPTS_COMPARE ) < 0 ) {
66-
report( prevAlias, alias, node );
67-
} else {
68-
prevAlias = alias;
69-
}
64+
if (
65+
prevAlias &&
66+
alias.localeCompare( prevAlias, 'en', OPTS_COMPARE ) < 0 )
67+
{
68+
report( prevAlias, alias, node );
7069
} else {
7170
prevAlias = alias;
7271
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Mersenne Twister
22+
23+
> A 32-bit [Mersenne Twister][mt] pseudorandom number generator.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mt19937 = require( '@stdlib/random/base/mt19937' );
31+
```
32+
33+
#### mt19937()
34+
35+
Returns a pseudorandom integer on the interval `[1, 4294967295]`.
36+
37+
```javascript
38+
var v = mt19937();
39+
// returns <number>
40+
```
41+
42+
#### mt19937.normalized()
43+
44+
Returns a pseudorandom number on the interval `[0,1)`.
45+
46+
```javascript
47+
var v = mt19937.normalized();
48+
// returns <number>
49+
```
50+
51+
#### mt19937.factory( \[seed] )
52+
53+
Returns a 32-bit [Mersenne Twister][mt] pseudorandom number generator.
54+
55+
```javascript
56+
var rand = mt19937.factory();
57+
```
58+
59+
By default, a random integer is used to seed the returned generator. To seed the generator, provide an `integer` on the interval `[1, 4294967295]`.
60+
61+
```javascript
62+
var rand = mt19937.factory( 1234 );
63+
64+
var v = rand();
65+
// returns 822569775
66+
```
67+
68+
#### mt19937.NAME
69+
70+
The generator name.
71+
72+
```javascript
73+
var str = mt19937.NAME;
74+
// returns 'mt19937'
75+
```
76+
77+
#### mt19937.SEED
78+
79+
The value used to seed `mt19937()`.
80+
81+
```javascript
82+
var rand;
83+
var v;
84+
var i;
85+
86+
// Generate pseudorandom values...
87+
for ( i = 0; i < 100; i++ ) {
88+
v = mt19937();
89+
}
90+
91+
// Generate the same pseudorandom values...
92+
rand = mt19937.factory( mt19937.SEED );
93+
for ( i = 0; i < 100; i++ ) {
94+
v = rand();
95+
}
96+
```
97+
98+
#### mt19937.MIN
99+
100+
Minimum possible value.
101+
102+
```javascript
103+
var min = mt19937.MIN;
104+
// returns 1
105+
```
106+
107+
#### mt19937.MAX
108+
109+
Maximum possible value.
110+
111+
```javascript
112+
var max = mt19937.MAX;
113+
// returns 4294967295
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- The generator has a period of `2^{19937} - 1`.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
```javascript
137+
var mt19937 = require( '@stdlib/random/base/mt19937' );
138+
139+
var seed;
140+
var rand;
141+
var i;
142+
143+
// Generate pseudorandom numbers...
144+
for ( i = 0; i < 100; i++ ) {
145+
console.log( mt19937() );
146+
}
147+
148+
// Create a new pseudorandom number generator...
149+
seed = 1234;
150+
rand = mt19937.factory( seed );
151+
for ( i = 0; i < 100; i++ ) {
152+
console.log( rand() );
153+
}
154+
155+
// Create another pseudorandom number generator using a previous seed...
156+
rand = mt19937.factory( mt19937.SEED );
157+
for ( i = 0; i < 100; i++ ) {
158+
console.log( rand() );
159+
}
160+
```
161+
162+
</section>
163+
164+
<!-- /.examples -->
165+
166+
167+
<section class="references">
168+
169+
</section>
170+
171+
<!-- /.references -->
172+
173+
<section class="links">
174+
175+
[mt]: https://en.wikipedia.org/wiki/Mersenne_Twister
176+
177+
</section>
178+
179+
<!-- /.links -->

0 commit comments

Comments
 (0)