Skip to content

Commit

Permalink
modernize code, fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SaswatPadhi committed May 27, 2023
1 parent e8ebece commit 4fad213
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 259 deletions.
121 changes: 61 additions & 60 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
{
"rules": {
"arrow-spacing": 2,
"brace-style": [2, "stroustrup", { "allowSingleLine": true }],
// We'd possibly like to remove the 'properties': 'never' one day.
"camelcase": [2, { "properties": "never" }],
"comma-dangle": [2, "always-multiline"],
"comma-spacing": [2, { "before": false, "after": true }],
"constructor-super": 2,
"curly": 0,
"eol-last": 2,
"eqeqeq": [2, "allow-null"],
"guard-for-in": 0,
"indent": [1, 4, {"CallExpression": {"arguments": "first"}, "flatTernaryExpressions": true, "SwitchCase": 1}],
"keyword-spacing": 2,
"linebreak-style": [2, "unix"],
"max-len": [2, 120, 4, { "ignoreUrls": true, "ignorePattern": "\\brequire\\([\"']|eslint-disable", "ignoreComments": true, }],
"no-alert": 2,
"no-array-constructor": 2,
"no-console": 0,
"no-const-assign": 2,
"no-constant-condition": 0,
"no-debugger": 2,
"no-dupe-class-members": 2,
"no-dupe-keys": 2,
"no-extra-bind": 2,
"no-new": 2,
"no-new-func": 2,
"no-new-object": 2,
"no-spaced-func": 2,
"no-this-before-super": 2,
"no-throw-literal": 0,
"no-trailing-spaces": 2,
"no-undef": 0,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"no-unused-vars": [2, {"args": "none", "varsIgnorePattern": "^_*$"}],
"no-useless-call": 2,
"no-with": 2,
"one-var": [2, "never"],
"prefer-const": 2,
"prefer-spread": 0, // re-enable once we use es6
"semi": [2, "always"],
"space-before-blocks": 2,
"space-before-function-paren": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": 2,
// ---------------------------------------
// Stuff we explicitly disable.
// We turned this off because it complains when you have a
// multi-line string, which I think is going too far.
"prefer-template": 0,
// We've decided explicitly not to care about this.
"arrow-parens": 0,
// ---------------------------------------
// TODO(csilvers): enable these if/when community agrees on it.
"prefer-arrow-callback": 0,
"object-curly-spacing": [0, "always"],
// Might be nice to turn this on one day, but since we don't
// use jsdoc anywhere it seems silly to require it yet.
"valid-jsdoc": 0,
"require-jsdoc": 0
"arrow-spacing": "error",
"brace-style": ["error", "stroustrup"],
"camelcase": "error",
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": "error",
"constructor-super": "error",
"curly": ["error", "multi-or-nest", "consistent"],
"eol-last": "error",
"eqeqeq": ["error", "always"],
"guard-for-in": "off",
"indent": ["error", 4, {
"CallExpression": {"arguments": "first"},
"FunctionExpression": {"parameters": "first"},
"flatTernaryExpressions": true,
"SwitchCase": 1
}],
"keyword-spacing": "error",
"linebreak-style": ["error", "unix"],
"max-len": ["error", 128, 2, {
"ignoreUrls": true,
"ignorePattern": "\\brequire\\([\"']|eslint-disable",
"ignoreComments": true
}],
"no-alert": "error",
"no-array-constructor": "error",
"no-console": "off",
"no-const-assign": "error",
"no-constant-condition": "off",
"no-debugger": "error",
"no-dupe-class-members": "error",
"no-dupe-keys": "error",
"no-duplicate-imports": "error",
"no-extra-bind": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-object": "error",
"no-spaced-func": "error",
"no-this-before-super": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef": "off",
"no-unexpected-multiline": "error",
"no-unreachable": "error",
"no-unused-vars": ["error", {"args": "none", "varsIgnorePattern": "^_*$"}],
"no-useless-call": "error",
"no-with": "error",
"one-var": ["error", "never"],
"prefer-const": "error",
"prefer-spread": "error",
"semi": ["error", "always"],
"space-before-blocks": "error",
"space-before-function-paren": ["error", "always"],
"space-infix-ops": "error",
"space-unary-ops": "error",
"prefer-template": "error",
"no-template-curly-in-string": "error",
"template-curly-spacing": ["error", "never"],
"arrow-parens": ["error", "always"],
"arrow-body-style": "error",
"prefer-arrow-callback": "error",
"object-curly-spacing": ["error", "always"]
},
"env": {
"es6": true,
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ build/pseudocode.js: pseudocode.js $(wildcard src/*.js)
@$(MAKE) --no-print-directory lint
$(BROWSERIFY) $< --exclude mathjax --exclude katex --standalone pseudocode -o $@

lint: pseudocode.js $(wildcard src/*.js)
$(ESLINT) $^

build/pseudocode.css: static/pseudocode.css
cp static/pseudocode.css build/pseudocode.css

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



lint: pseudocode.js $(wildcard src/*.js)
$(ESLINT) $^

fix-lint: pseudocode.js $(wildcard src/*.js)
$(ESLINT) --fix $^



release: build docs build/pseudocode-js.tar.gz build/pseudocode-js.zip
@echo "> Release package generated\n"

Expand Down
2 changes: 1 addition & 1 deletion docs/pseudocode.js

Large diffs are not rendered by default.

36 changes: 19 additions & 17 deletions pseudocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ var Lexer = require('./src/Lexer');
var Parser = require('./src/Parser');
var Renderer = require('./src/Renderer');

function makeRenderer(data, options) {
function makeRenderer (data, options) {
var lexer = new Lexer(data);
var parser = new Parser(lexer);
return new Renderer(parser, options);
}

function mathjaxTypeset(elem) {
function mathjaxTypeset (elem) {
try {
// MathJax 3.x
MathJax.typeset([elem]);
Expand All @@ -26,51 +26,53 @@ function mathjaxTypeset(elem) {

module.exports = {
ParseError: ParseError,
render: function(input, baseDomEle, options) {
render: function (input, baseDomEle, options) {
if (input === null || input === undefined)
throw 'input cannot be empty';
throw new ReferenceError('Input cannot be empty');

var renderer = makeRenderer(input, options);
var elem = renderer.toDOM();
if (baseDomEle) baseDomEle.appendChild(elem);
if (baseDomEle)
baseDomEle.appendChild(elem);

if (renderer.backend.name === 'mathjax') {
if (renderer.backend.name === 'mathjax')
mathjaxTypeset(elem);
}

return elem;
},
renderToString: function(input, options) {
renderToString: function (input, options) {
if (input === null || input === undefined)
throw 'input cannot be empty';
throw new ReferenceError('Input cannot be empty');

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

return renderer.toMarkup();
},
renderElement: function(elem, options) {
renderElement: function (elem, options) {
if (!(elem instanceof Element))
throw 'a DOM element is required';
throw new ReferenceError('A DOM element is required');

elem.style.display = 'none';

var renderer = makeRenderer(elem.textContent, options);

var newElem = renderer.toDOM();
elem.replaceWith(newElem);

if (renderer.backend) {
if (renderer.backend.name === 'mathjax') {
if (renderer.backend.name === 'mathjax')
mathjaxTypeset(newElem);
}
}
},

renderClass: function(className, options) {
renderClass: function (className, options) {
[].forEach.call(
document.getElementsByClassName(className),
function(el) { this.renderElement(el, options); }
function (el) {
this.renderElement(el, options);
}
);
},
};
55 changes: 32 additions & 23 deletions src/Lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,46 @@
var utils = require('./utils');
var ParseError = require('./ParseError');

var Lexer = function(input) {
var Lexer = function (input) {
this._input = input;
this._remain = input;
this._pos = 0;
this._nextAtom = this._currentAtom = null;
this._next(); // get the next atom
};

Lexer.prototype.accept = function(type, text) {
Lexer.prototype.accept = function (type, text) {
if (this._nextAtom.type === type && this._matchText(text)) {
this._next();
return this._currentAtom.text;
}
return null;
};

Lexer.prototype.expect = function(type, text) {
Lexer.prototype.expect = function (type, text) {
var nextAtom = this._nextAtom;
// The next atom is NOT of the right type
if (nextAtom.type !== type)
throw new ParseError('Expect an atom of ' + type + ' but received ' +
nextAtom.type, this._pos, this._input);
if (nextAtom.type !== type) {
throw new ParseError(
`Expected an atom of ${type} but received ${nextAtom.type}`,
this._pos,
this._input
);
}
// Check whether the text is exactly the same
if (!this._matchText(text))
throw new ParseError('Expect `' + text + '` but received `' +
nextAtom.text + '`', this._pos, this._input);
if (!this._matchText(text)) {
throw new ParseError(
`Expected \`${text}\` but received \`${nextAtom.text}\``,
this._pos,
this._input
);
}

this._next();
return this._currentAtom.text;
};

Lexer.prototype.get = function() {
Lexer.prototype.get = function () {
return this._currentAtom;
};

Expand All @@ -45,10 +53,10 @@ Lexer.prototype.get = function() {
expression. This object simulates a RegEx object
*/
var mathPattern = {
exec: function(str) {
exec: function (str) {
var delimiters = [
{start: '$', end: '$'},
{start: '\\(', end: '\\)'},
{ start: '$', end: '$' },
{ start: '\\(', end: '\\)' },
];
var totalLen = str.length;

Expand All @@ -61,9 +69,10 @@ var mathPattern = {
var remain = str.slice(endPos);
while (endPos < totalLen) {
var pos = remain.indexOf(endDel);
if (pos < 0)
if (pos < 0) {
throw new ParseError('Math environment is not closed',
this._pos, this._input);
}

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

Lexer.prototype._skip = function(len) {
Lexer.prototype._skip = function (len) {
this._pos += len;
this._remain = this._remain.slice(len);
};

/* Get the next atom */
Lexer.prototype._next = function() {
Lexer.prototype._next = function () {
var anyWhitespace = false;
while (1) {
// Skip whitespace (one or more)
Expand Down Expand Up @@ -159,17 +168,17 @@ Lexer.prototype._next = function() {
};

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

// string comparisons are case-insensitive
if (utils.isString(text)) // is a string, exactly the same?
// using case-insensitive comparisons,
// check if text is the same as next atom,
// or if text is an array that contains the next atom
if (utils.isString(text))
return text.toLowerCase() === this._nextAtom.text.toLowerCase();
else {// is a list, match any of them?
text = text.map(function(str) { return str.toLowerCase(); });
return text.indexOf(this._nextAtom.text.toLowerCase()) >= 0;
}
else
return text.some((str) => str.toLowerCase() === this._nextAtom.text.toLowerCase());
};

module.exports = Lexer;
10 changes: 5 additions & 5 deletions src/ParseError.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function ParseError(message, pos, input) {
var error = 'Error: ' + message;
function ParseError (message, pos, input) {
var error = `Error: ${message}`;
// If we have the input and a position, make the error a bit fancier
if (pos !== undefined && input !== undefined) {
error += " at position " + pos + ": `";
error += ` at position ${pos}: \``;

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

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

this.message = error;
Expand Down
Loading

0 comments on commit 4fad213

Please sign in to comment.