Skip to content

Commit 1b15d2c

Browse files
committed
Add typing and encapsulate in IIFE
1 parent d3d5a56 commit 1b15d2c

1 file changed

Lines changed: 60 additions & 33 deletions

File tree

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,66 @@
11
// JSHINT has some GPL Compatability issues, so we are faking it out and using espree for validation
22
// Based on https://github.com/jquery/esprima/blob/gh-pages/demo/validate.js which is MIT licensed
33

4-
var fakeJSHINT = function() {
5-
var syntax;
6-
var that = this;
7-
this.data = [];
8-
this.convertError = function( error ){
9-
return {
10-
line: error.lineNumber,
11-
character: error.column,
12-
reason: error.description,
13-
code: 'E'
14-
};
15-
};
16-
this.parse = function( code ){
17-
try {
18-
syntax = window.espree.parse(code, {
19-
ecmaVersion: 'latest',
20-
loc: true,
21-
sourceType: 'module'
22-
});
23-
that.data = [];
24-
} catch (e) {
25-
that.data.push( that.convertError( e ) );
26-
}
27-
};
28-
};
4+
( () => {
5+
/**
6+
* @typedef {Object} JSHINTError
7+
* @property {number} line - Line number.
8+
* @property {number} character - Column number.
9+
* @property {string} reason - Error message.
10+
* @property {string} code - Error code.
11+
*/
2912

30-
window.JSHINT = function( text ){
31-
fakeJSHINT.parse( text );
32-
};
33-
window.JSHINT.data = function(){
34-
return {
35-
errors: fakeJSHINT.data
36-
};
37-
};
13+
/**
14+
* Fake JSHINT.
15+
*/
16+
const fakeJSHINT = {
17+
/**
18+
* Collected error(s) during parsing.
19+
*
20+
* @type {JSHINTError[]}
21+
*/
22+
data: [],
3823

24+
/**
25+
* Converts a SyntaxError to a JSHINT error.
26+
*
27+
* @param {SyntaxError} error - SyntaxError to convert.
28+
* @returns {JSHINTError}
29+
*/
30+
convertError( error ) {
31+
return {
32+
line: error.lineNumber,
33+
character: error.column,
34+
reason: error.message,
35+
code: 'E',
36+
};
37+
},
3938

39+
/**
40+
* Parses JS code to find errors.
41+
*
42+
* @param {string} code - JS code to parse.
43+
*/
44+
parse( code ) {
45+
try {
46+
window.espree.parse( code, {
47+
ecmaVersion: 'latest',
48+
loc: true,
49+
sourceType: 'module',
50+
} );
51+
this.data = [];
52+
} catch ( error ) {
53+
this.data.push( this.convertError( error ) );
54+
}
55+
},
56+
};
57+
58+
window.JSHINT = ( text ) => {
59+
fakeJSHINT.parse( text );
60+
};
61+
window.JSHINT.data = () => {
62+
return {
63+
errors: fakeJSHINT.data,
64+
};
65+
};
66+
} )();

0 commit comments

Comments
 (0)