Skip to content

Commit

Permalink
Chore: Upgrade to eslint-config-eslint@5.0.1 (#110)
Browse files Browse the repository at this point in the history
* Chore: Upgrade to eslint-config-eslint@5.0.1

The prior config was still enforcing ES5 coding standards. The earliest
supported version of Node.js is 6.14.0, so this lets us use all the nice
ES6 features.

* npm run lint -- --fix

* assert.equal -> assert.strictEqual

* Use block-scoped variable declarations

* Use array spread operator
  • Loading branch information
btmills committed Jun 5, 2019
1 parent a248c9a commit fdacf0c
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 390 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -5,10 +5,10 @@

"use strict";

var processor = require("./processor");
const processor = require("./processor");

module.exports = {
"processors": {
processors: {
".markdown": processor,
".mdown": processor,
".mkdn": processor,
Expand Down
141 changes: 66 additions & 75 deletions lib/processor.js
Expand Up @@ -5,37 +5,35 @@

"use strict";

var assign = require("object-assign");
var unified = require("unified");
var remarkParse = require("remark-parse");
const assign = require("object-assign");
const unified = require("unified");
const remarkParse = require("remark-parse");

var SUPPORTED_SYNTAXES = ["js", "javascript", "node", "jsx"];
var UNSATISFIABLE_RULES = [
const SUPPORTED_SYNTAXES = ["js", "javascript", "node", "jsx"];
const UNSATISFIABLE_RULES = [
"eol-last", // The Markdown parser strips trailing newlines in code fences
"unicode-bom" // Code blocks will begin in the middle of Markdown files
];
var SUPPORTS_AUTOFIX = true;
const SUPPORTS_AUTOFIX = true;

var markdown = unified().use(remarkParse);
const markdown = unified().use(remarkParse);

var blocks = [];
let blocks = [];

/**
* Performs a depth-first traversal of the Markdown AST.
* @param {ASTNode} node A Markdown AST node.
* @param {object} callbacks A map of node types to callbacks.
* @param {object} [parent] The node's parent AST node.
* @param {Object} callbacks A map of node types to callbacks.
* @param {Object} [parent] The node's parent AST node.
* @returns {void}
*/
function traverse(node, callbacks, parent) {
var i;

if (callbacks[node.type]) {
callbacks[node.type](node, parent);
}

if (typeof node.children !== "undefined") {
for (i = 0; i < node.children.length; i++) {
for (let i = 0; i < node.children.length; i++) {
traverse(node.children[i], callbacks, node);
}
}
Expand All @@ -47,9 +45,9 @@ function traverse(node, callbacks, parent) {
* @returns {string[]} An array of JS block comments.
*/
function getComment(html) {
var commentStart = "<!--";
var commentEnd = "-->";
var regex = /^(eslint\b|global\s)/;
const commentStart = "<!--";
const commentEnd = "-->";
const regex = /^(eslint\b|global\s)/;

if (
html.slice(0, commentStart.length) !== commentStart ||
Expand All @@ -58,16 +56,16 @@ function getComment(html) {
return "";
}

html = html.slice(commentStart.length, -commentEnd.length);
const comment = html.slice(commentStart.length, -commentEnd.length);

if (!regex.test(html.trim())) {
if (!regex.test(comment.trim())) {
return "";
}

return html;
return comment;
}

var leadingWhitespaceRegex = /^\s*/;
const leadingWhitespaceRegex = /^\s*/;

/**
* When applying fixes, the postprocess step needs to know how to map fix ranges
Expand Down Expand Up @@ -97,24 +95,12 @@ var leadingWhitespaceRegex = /^\s*/;
* @param {ASTNode} node A Markdown code block AST node.
* @param {comments} comments List of configuration comment strings that will be
* inserted at the beginning of the code block.
* @returns {object[]} A list of offset-based adjustments, where lookups are
* @returns {Object[]} A list of offset-based adjustments, where lookups are
* done based on the `js` key, which represents the range in the linted JS,
* and the `md` key is the offset delta that, when added to the JS range,
* returns the corresponding location in the original Markdown source.
*/
function getBlockRangeMap(text, node, comments) {
var baseIndent,
code,
commentLength,
i,
jsOffset,
leadingWhitespaceLength,
line,
lines,
mdOffset,
rangeMap,
startOffset,
trimLength;

/*
* The parser sets the fenced code block's start offset to wherever content
Expand All @@ -124,67 +110,67 @@ function getBlockRangeMap(text, node, comments) {
* additional indenting, the opening fence's first backtick may be up to
* three whitespace characters after the start offset.
*/
startOffset = node.position.start.offset;
const startOffset = node.position.start.offset;

/*
* Extract the Markdown source to determine the leading whitespace for each
* line.
*/
code = text.slice(startOffset, node.position.end.offset);
lines = code.split("\n");
const code = text.slice(startOffset, node.position.end.offset);
const lines = code.split("\n");

/*
* The parser trims leading whitespace from each line of code within the
* fenced code block up to the opening fence's first backtick. The first
* backtick's column is the AST node's starting column plus any additional
* indentation.
*/
baseIndent = node.position.start.column - 1
+ leadingWhitespaceRegex.exec(lines[0])[0].length;
const baseIndent = node.position.start.column - 1 +
leadingWhitespaceRegex.exec(lines[0])[0].length;

/*
* Track the length of any inserted configuration comments at the beginning
* of the linted JS and start the JS offset lookup keys at this index.
*/
commentLength = comments.reduce(function(len, comment) {
return len + comment.length + 1;
}, 0);
const commentLength = comments.reduce((len, comment) => len + comment.length + 1, 0);

/*
* In case there are configuration comments, initialize the map so that the
* first lookup index is always 0. If there are no configuration comments,
* the lookup index will also be 0, and the lookup should always go to the
* last range that matches, skipping this initialization entry.
*/
rangeMap = [{
const rangeMap = [{
js: 0,
md: 0
}];

// Start the JS offset after any configuration comments.
jsOffset = commentLength;
let jsOffset = commentLength;

/*
* Start the Markdown offset at the beginning of the block's first line of
* actual code. The first line of the block is always the opening fence, so
* the code begins on the second line.
*/
mdOffset = startOffset + lines[0].length + 1;
let mdOffset = startOffset + lines[0].length + 1;

/*
* For each line, determine how much leading whitespace was trimmed due to
* indentation. Increase the JS lookup offset by the length of the line
* post-trimming and the Markdown offset by the total line length.
*/
for (i = 0; i + 1 < lines.length; i++) {
line = lines[i + 1];
leadingWhitespaceLength = leadingWhitespaceRegex.exec(line)[0].length;
for (let i = 0; i + 1 < lines.length; i++) {
const line = lines[i + 1];
const leadingWhitespaceLength = leadingWhitespaceRegex.exec(line)[0].length;

// The parser trims leading whitespace up to the level of the opening
// fence, so keep any additional indentation beyond that.
trimLength = Math.min(baseIndent, leadingWhitespaceLength);
const trimLength = Math.min(baseIndent, leadingWhitespaceLength);

rangeMap.push({
js: jsOffset,

// Advance `trimLength` character from the beginning of the Markdown
// line to the beginning of the equivalent JS line, then compute the
// delta.
Expand All @@ -205,19 +191,19 @@ function getBlockRangeMap(text, node, comments) {
* @returns {string[]} Source code strings to lint.
*/
function preprocess(text) {
var ast = markdown.parse(text);
const ast = markdown.parse(text);

blocks = [];
traverse(ast, {
"code": function(node, parent) {
var comments = [];
var index, previousNode, comment;
code(node, parent) {
const comments = [];

if (node.lang && SUPPORTED_SYNTAXES.indexOf(node.lang.split(" ")[0].toLowerCase()) >= 0) {
index = parent.children.indexOf(node) - 1;
previousNode = parent.children[index];
let index = parent.children.indexOf(node) - 1;
let previousNode = parent.children[index];

while (previousNode && previousNode.type === "html") {
comment = getComment(previousNode.value);
const comment = getComment(previousNode.value);

if (!comment) {
break;
Expand All @@ -227,35 +213,35 @@ function preprocess(text) {
return;
}

comments.unshift("/*" + comment + "*/");
comments.unshift(`/*${comment}*/`);
index--;
previousNode = parent.children[index];
}

blocks.push(assign({}, node, {
comments: comments,
comments,
rangeMap: getBlockRangeMap(text, node, comments)
}));
}
}
});

return blocks.map(function(block) {
return block.comments.concat(block.value).concat("").join("\n");
});
return blocks.map(block => [
...block.comments,
block.value,
""
].join("\n"));
}

/**
* Creates a map function that adjusts messages in a code block.
* @param {Block} block A code block.
* @returns {function} A function that adjusts messages in a code block.
* @returns {Function} A function that adjusts messages in a code block.
*/
function adjustBlock(block) {
var leadingCommentLines = block.comments.reduce(function(count, comment) {
return count + comment.split("\n").length;
}, 0);
const leadingCommentLines = block.comments.reduce((count, comment) => count + comment.split("\n").length, 0);

var blockStart = block.position.start.line;
const blockStart = block.position.start.line;

/**
* Adjusts ESLint messages to point to the correct location in the Markdown.
Expand All @@ -264,26 +250,30 @@ function adjustBlock(block) {
*/
return function adjustMessage(message) {

var lineInCode = message.line - leadingCommentLines;
var endLine = message.endLine - leadingCommentLines;
const lineInCode = message.line - leadingCommentLines;
const endLine = message.endLine - leadingCommentLines;

if (lineInCode < 1) {
return null;
}

var out = {
const out = {
line: lineInCode + blockStart,
endLine: endLine ? endLine + blockStart : endLine,
column: message.column + block.position.indent[lineInCode - 1] - 1
};

var adjustedFix = {};
const adjustedFix = {};

if (message.fix) {
adjustedFix.fix = {
range: message.fix.range.map(function(range) {
range: message.fix.range.map(range => {

// Advance through the block's range map to find the last
// matching range by finding the first range too far and
// then going back one.
var i = 1;
let i = 1;

while (i < block.rangeMap.length && block.rangeMap[i].js < range) {
i++;
}
Expand Down Expand Up @@ -315,14 +305,15 @@ function excludeUnsatisfiableRules(message) {
* @returns {Message[]} A flattened array of messages with mapped locations.
*/
function postprocess(messages) {
return [].concat.apply([], messages.map(function(group, i) {
var adjust = adjustBlock(blocks[i]);
return [].concat(...messages.map((group, i) => {
const adjust = adjustBlock(blocks[i]);

return group.map(adjust).filter(excludeUnsatisfiableRules);
}));
}

module.exports = {
preprocess: preprocess,
postprocess: postprocess,
preprocess,
postprocess,
supportsAutofix: SUPPORTS_AUTOFIX
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -38,7 +38,8 @@
"devDependencies": {
"chai": "^3.0.0",
"eslint": "^4.19.1",
"eslint-config-eslint": "^3.0.0",
"eslint-config-eslint": "^5.0.1",
"eslint-plugin-node": "^6.0.1",
"eslint-release": "^1.0.0",
"istanbul": "^0.4.5",
"mocha": "^2.2.5"
Expand Down

0 comments on commit fdacf0c

Please sign in to comment.