Skip to content

Commit 4fad213

Browse files
committed
modernize code, fix linter issues
1 parent e8ebece commit 4fad213

File tree

9 files changed

+294
-259
lines changed

9 files changed

+294
-259
lines changed

.eslintrc

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,66 @@
11
{
22
"rules": {
3-
"arrow-spacing": 2,
4-
"brace-style": [2, "stroustrup", { "allowSingleLine": true }],
5-
// We'd possibly like to remove the 'properties': 'never' one day.
6-
"camelcase": [2, { "properties": "never" }],
7-
"comma-dangle": [2, "always-multiline"],
8-
"comma-spacing": [2, { "before": false, "after": true }],
9-
"constructor-super": 2,
10-
"curly": 0,
11-
"eol-last": 2,
12-
"eqeqeq": [2, "allow-null"],
13-
"guard-for-in": 0,
14-
"indent": [1, 4, {"CallExpression": {"arguments": "first"}, "flatTernaryExpressions": true, "SwitchCase": 1}],
15-
"keyword-spacing": 2,
16-
"linebreak-style": [2, "unix"],
17-
"max-len": [2, 120, 4, { "ignoreUrls": true, "ignorePattern": "\\brequire\\([\"']|eslint-disable", "ignoreComments": true, }],
18-
"no-alert": 2,
19-
"no-array-constructor": 2,
20-
"no-console": 0,
21-
"no-const-assign": 2,
22-
"no-constant-condition": 0,
23-
"no-debugger": 2,
24-
"no-dupe-class-members": 2,
25-
"no-dupe-keys": 2,
26-
"no-extra-bind": 2,
27-
"no-new": 2,
28-
"no-new-func": 2,
29-
"no-new-object": 2,
30-
"no-spaced-func": 2,
31-
"no-this-before-super": 2,
32-
"no-throw-literal": 0,
33-
"no-trailing-spaces": 2,
34-
"no-undef": 0,
35-
"no-unexpected-multiline": 2,
36-
"no-unreachable": 2,
37-
"no-unused-vars": [2, {"args": "none", "varsIgnorePattern": "^_*$"}],
38-
"no-useless-call": 2,
39-
"no-with": 2,
40-
"one-var": [2, "never"],
41-
"prefer-const": 2,
42-
"prefer-spread": 0, // re-enable once we use es6
43-
"semi": [2, "always"],
44-
"space-before-blocks": 2,
45-
"space-before-function-paren": [2, "never"],
46-
"space-infix-ops": 2,
47-
"space-unary-ops": 2,
48-
// ---------------------------------------
49-
// Stuff we explicitly disable.
50-
// We turned this off because it complains when you have a
51-
// multi-line string, which I think is going too far.
52-
"prefer-template": 0,
53-
// We've decided explicitly not to care about this.
54-
"arrow-parens": 0,
55-
// ---------------------------------------
56-
// TODO(csilvers): enable these if/when community agrees on it.
57-
"prefer-arrow-callback": 0,
58-
"object-curly-spacing": [0, "always"],
59-
// Might be nice to turn this on one day, but since we don't
60-
// use jsdoc anywhere it seems silly to require it yet.
61-
"valid-jsdoc": 0,
62-
"require-jsdoc": 0
3+
"arrow-spacing": "error",
4+
"brace-style": ["error", "stroustrup"],
5+
"camelcase": "error",
6+
"comma-dangle": ["error", "always-multiline"],
7+
"comma-spacing": "error",
8+
"constructor-super": "error",
9+
"curly": ["error", "multi-or-nest", "consistent"],
10+
"eol-last": "error",
11+
"eqeqeq": ["error", "always"],
12+
"guard-for-in": "off",
13+
"indent": ["error", 4, {
14+
"CallExpression": {"arguments": "first"},
15+
"FunctionExpression": {"parameters": "first"},
16+
"flatTernaryExpressions": true,
17+
"SwitchCase": 1
18+
}],
19+
"keyword-spacing": "error",
20+
"linebreak-style": ["error", "unix"],
21+
"max-len": ["error", 128, 2, {
22+
"ignoreUrls": true,
23+
"ignorePattern": "\\brequire\\([\"']|eslint-disable",
24+
"ignoreComments": true
25+
}],
26+
"no-alert": "error",
27+
"no-array-constructor": "error",
28+
"no-console": "off",
29+
"no-const-assign": "error",
30+
"no-constant-condition": "off",
31+
"no-debugger": "error",
32+
"no-dupe-class-members": "error",
33+
"no-dupe-keys": "error",
34+
"no-duplicate-imports": "error",
35+
"no-extra-bind": "error",
36+
"no-new": "error",
37+
"no-new-func": "error",
38+
"no-new-object": "error",
39+
"no-spaced-func": "error",
40+
"no-this-before-super": "error",
41+
"no-throw-literal": "error",
42+
"no-trailing-spaces": "error",
43+
"no-undef": "off",
44+
"no-unexpected-multiline": "error",
45+
"no-unreachable": "error",
46+
"no-unused-vars": ["error", {"args": "none", "varsIgnorePattern": "^_*$"}],
47+
"no-useless-call": "error",
48+
"no-with": "error",
49+
"one-var": ["error", "never"],
50+
"prefer-const": "error",
51+
"prefer-spread": "error",
52+
"semi": ["error", "always"],
53+
"space-before-blocks": "error",
54+
"space-before-function-paren": ["error", "always"],
55+
"space-infix-ops": "error",
56+
"space-unary-ops": "error",
57+
"prefer-template": "error",
58+
"no-template-curly-in-string": "error",
59+
"template-curly-spacing": ["error", "never"],
60+
"arrow-parens": ["error", "always"],
61+
"arrow-body-style": "error",
62+
"prefer-arrow-callback": "error",
63+
"object-curly-spacing": ["error", "always"]
6364
},
6465
"env": {
6566
"es6": true,

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ build/pseudocode.js: pseudocode.js $(wildcard src/*.js)
3434
@$(MAKE) --no-print-directory lint
3535
$(BROWSERIFY) $< --exclude mathjax --exclude katex --standalone pseudocode -o $@
3636

37-
lint: pseudocode.js $(wildcard src/*.js)
38-
$(ESLINT) $^
39-
4037
build/pseudocode.css: static/pseudocode.css
4138
cp static/pseudocode.css build/pseudocode.css
4239

@@ -45,6 +42,14 @@ build/%-samples.html: static/%.html.part static/body.html.part static/footer.htm
4542

4643

4744

45+
lint: pseudocode.js $(wildcard src/*.js)
46+
$(ESLINT) $^
47+
48+
fix-lint: pseudocode.js $(wildcard src/*.js)
49+
$(ESLINT) --fix $^
50+
51+
52+
4853
release: build docs build/pseudocode-js.tar.gz build/pseudocode-js.zip
4954
@echo "> Release package generated\n"
5055

docs/pseudocode.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pseudocode.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ var Lexer = require('./src/Lexer');
77
var Parser = require('./src/Parser');
88
var Renderer = require('./src/Renderer');
99

10-
function makeRenderer(data, options) {
10+
function makeRenderer (data, options) {
1111
var lexer = new Lexer(data);
1212
var parser = new Parser(lexer);
1313
return new Renderer(parser, options);
1414
}
1515

16-
function mathjaxTypeset(elem) {
16+
function mathjaxTypeset (elem) {
1717
try {
1818
// MathJax 3.x
1919
MathJax.typeset([elem]);
@@ -26,51 +26,53 @@ function mathjaxTypeset(elem) {
2626

2727
module.exports = {
2828
ParseError: ParseError,
29-
render: function(input, baseDomEle, options) {
29+
render: function (input, baseDomEle, options) {
3030
if (input === null || input === undefined)
31-
throw 'input cannot be empty';
31+
throw new ReferenceError('Input cannot be empty');
3232

3333
var renderer = makeRenderer(input, options);
3434
var elem = renderer.toDOM();
35-
if (baseDomEle) baseDomEle.appendChild(elem);
35+
if (baseDomEle)
36+
baseDomEle.appendChild(elem);
3637

37-
if (renderer.backend.name === 'mathjax') {
38+
if (renderer.backend.name === 'mathjax')
3839
mathjaxTypeset(elem);
39-
}
40+
4041
return elem;
4142
},
42-
renderToString: function(input, options) {
43+
renderToString: function (input, options) {
4344
if (input === null || input === undefined)
44-
throw 'input cannot be empty';
45+
throw new ReferenceError('Input cannot be empty');
4546

4647
var renderer = makeRenderer(input, options);
47-
if (renderer.backend.name === 'mathjax') {
48+
if (renderer.backend.name === 'mathjax')
4849
console.warn('Using MathJax backend -- math may not be rendered.');
49-
}
5050

5151
return renderer.toMarkup();
5252
},
53-
renderElement: function(elem, options) {
53+
renderElement: function (elem, options) {
5454
if (!(elem instanceof Element))
55-
throw 'a DOM element is required';
55+
throw new ReferenceError('A DOM element is required');
5656

5757
elem.style.display = 'none';
5858

5959
var renderer = makeRenderer(elem.textContent, options);
60+
6061
var newElem = renderer.toDOM();
6162
elem.replaceWith(newElem);
6263

6364
if (renderer.backend) {
64-
if (renderer.backend.name === 'mathjax') {
65+
if (renderer.backend.name === 'mathjax')
6566
mathjaxTypeset(newElem);
66-
}
6767
}
6868
},
6969

70-
renderClass: function(className, options) {
70+
renderClass: function (className, options) {
7171
[].forEach.call(
7272
document.getElementsByClassName(className),
73-
function(el) { this.renderElement(el, options); }
73+
function (el) {
74+
this.renderElement(el, options);
75+
}
7476
);
7577
},
7678
};

src/Lexer.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,46 @@
55
var utils = require('./utils');
66
var ParseError = require('./ParseError');
77

8-
var Lexer = function(input) {
8+
var Lexer = function (input) {
99
this._input = input;
1010
this._remain = input;
1111
this._pos = 0;
1212
this._nextAtom = this._currentAtom = null;
1313
this._next(); // get the next atom
1414
};
1515

16-
Lexer.prototype.accept = function(type, text) {
16+
Lexer.prototype.accept = function (type, text) {
1717
if (this._nextAtom.type === type && this._matchText(text)) {
1818
this._next();
1919
return this._currentAtom.text;
2020
}
2121
return null;
2222
};
2323

24-
Lexer.prototype.expect = function(type, text) {
24+
Lexer.prototype.expect = function (type, text) {
2525
var nextAtom = this._nextAtom;
2626
// The next atom is NOT of the right type
27-
if (nextAtom.type !== type)
28-
throw new ParseError('Expect an atom of ' + type + ' but received ' +
29-
nextAtom.type, this._pos, this._input);
27+
if (nextAtom.type !== type) {
28+
throw new ParseError(
29+
`Expected an atom of ${type} but received ${nextAtom.type}`,
30+
this._pos,
31+
this._input
32+
);
33+
}
3034
// Check whether the text is exactly the same
31-
if (!this._matchText(text))
32-
throw new ParseError('Expect `' + text + '` but received `' +
33-
nextAtom.text + '`', this._pos, this._input);
35+
if (!this._matchText(text)) {
36+
throw new ParseError(
37+
`Expected \`${text}\` but received \`${nextAtom.text}\``,
38+
this._pos,
39+
this._input
40+
);
41+
}
3442

3543
this._next();
3644
return this._currentAtom.text;
3745
};
3846

39-
Lexer.prototype.get = function() {
47+
Lexer.prototype.get = function () {
4048
return this._currentAtom;
4149
};
4250

@@ -45,10 +53,10 @@ Lexer.prototype.get = function() {
4553
expression. This object simulates a RegEx object
4654
*/
4755
var mathPattern = {
48-
exec: function(str) {
56+
exec: function (str) {
4957
var delimiters = [
50-
{start: '$', end: '$'},
51-
{start: '\\(', end: '\\)'},
58+
{ start: '$', end: '$' },
59+
{ start: '\\(', end: '\\)' },
5260
];
5361
var totalLen = str.length;
5462

@@ -61,9 +69,10 @@ var mathPattern = {
6169
var remain = str.slice(endPos);
6270
while (endPos < totalLen) {
6371
var pos = remain.indexOf(endDel);
64-
if (pos < 0)
72+
if (pos < 0) {
6573
throw new ParseError('Math environment is not closed',
6674
this._pos, this._input);
75+
}
6776

6877
// false positive, it's escaped, not a match
6978
if (pos > 0 && remain[pos - 1] === '\\') {
@@ -95,13 +104,13 @@ var atomRegex = {
95104
var commentRegex = /^%.*/;
96105
var whitespaceRegex = /^\s+/;
97106

98-
Lexer.prototype._skip = function(len) {
107+
Lexer.prototype._skip = function (len) {
99108
this._pos += len;
100109
this._remain = this._remain.slice(len);
101110
};
102111

103112
/* Get the next atom */
104-
Lexer.prototype._next = function() {
113+
Lexer.prototype._next = function () {
105114
var anyWhitespace = false;
106115
while (1) {
107116
// Skip whitespace (one or more)
@@ -159,17 +168,17 @@ Lexer.prototype._next = function() {
159168
};
160169

161170
/* Check whether the text of the next atom matches */
162-
Lexer.prototype._matchText = function(text) {
171+
Lexer.prototype._matchText = function (text) {
163172
// don't need to match
164173
if (text === null || text === undefined) return true;
165174

166-
// string comparisons are case-insensitive
167-
if (utils.isString(text)) // is a string, exactly the same?
175+
// using case-insensitive comparisons,
176+
// check if text is the same as next atom,
177+
// or if text is an array that contains the next atom
178+
if (utils.isString(text))
168179
return text.toLowerCase() === this._nextAtom.text.toLowerCase();
169-
else {// is a list, match any of them?
170-
text = text.map(function(str) { return str.toLowerCase(); });
171-
return text.indexOf(this._nextAtom.text.toLowerCase()) >= 0;
172-
}
180+
else
181+
return text.some((str) => str.toLowerCase() === this._nextAtom.text.toLowerCase());
173182
};
174183

175184
module.exports = Lexer;

src/ParseError.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
function ParseError(message, pos, input) {
2-
var error = 'Error: ' + message;
1+
function ParseError (message, pos, input) {
2+
var error = `Error: ${message}`;
33
// If we have the input and a position, make the error a bit fancier
44
if (pos !== undefined && input !== undefined) {
5-
error += " at position " + pos + ": `";
5+
error += ` at position ${pos}: \``;
66

77
// Insert a combining underscore at the correct position
8-
input = input.slice(0, pos) + "\u21B1" + input.slice(pos);
8+
input = `${input.slice(0, pos)}\u21B1${input.slice(pos)}`;
99

1010
// Extract some context from the input and add it to the error
1111
var begin = Math.max(0, pos - 15);
1212
var end = pos + 15;
13-
error += input.slice(begin, end) + "`";
13+
error += `${input.slice(begin, end)}\``;
1414
}
1515

1616
this.message = error;

0 commit comments

Comments
 (0)