diff options
author | cjihrig <cjihrig@gmail.com> | 2019-10-25 12:48:14 -0700 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2019-10-28 09:51:24 -0400 |
commit | 511f67bcb42b59c9a3a3efab8fed578db100afe1 (patch) | |
tree | 8b64f390dd727dd739fd2fb84d69df3c829a9315 /tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib | |
parent | b35181f877d5a92e8bb52eb071489f2a7d87494b (diff) | |
download | android-node-v8-511f67bcb42b59c9a3a3efab8fed578db100afe1.tar.gz android-node-v8-511f67bcb42b59c9a3a3efab8fed578db100afe1.tar.bz2 android-node-v8-511f67bcb42b59c9a3a3efab8fed578db100afe1.zip |
tools: update ESLint to 6.6.0
Update ESLint to 6.6.0
PR-URL: https://github.com/nodejs/node/pull/30123
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib')
-rw-r--r-- | tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/index.js | 4 | ||||
-rw-r--r-- | tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/processor.js | 172 |
2 files changed, 97 insertions, 79 deletions
diff --git a/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/index.js b/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/index.js index 890425ff8a..e27aa162f9 100644 --- a/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/index.js +++ b/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/index.js @@ -5,10 +5,10 @@ "use strict"; -var processor = require("./processor"); +const processor = require("./processor"); module.exports = { - "processors": { + processors: { ".markdown": processor, ".mdown": processor, ".mkdn": processor, diff --git a/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/processor.js b/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/processor.js index a3c9e06da2..8efcc55b9a 100644 --- a/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/processor.js +++ b/tools/node_modules/eslint/node_modules/eslint-plugin-markdown/lib/processor.js @@ -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); } } @@ -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 || @@ -58,16 +56,42 @@ 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*/; +// Before a code block, blockquote characters (`>`) are also considered +// "whitespace". +const leadingWhitespaceRegex = /^[>\s]*/; + +/** + * Gets the offset for the first column of the node's first line in the + * original source text. + * @param {ASTNode} node A Markdown code block AST node. + * @returns {number} The offset for the first column of the node's first line. + */ +function getBeginningOfLineOffset(node) { + return node.position.start.offset - node.position.start.column + 1; +} + +/** + * Gets the leading text, typically whitespace with possible blockquote chars, + * used to indent a code block. + * @param {string} text The text of the file. + * @param {ASTNode} node A Markdown code block AST node. + * @returns {string} The text from the start of the first line to the opening + * fence of the code block. + */ +function getIndentText(text, node) { + return leadingWhitespaceRegex.exec( + text.slice(getBeginningOfLineOffset(node)) + )[0]; +} /** * When applying fixes, the postprocess step needs to know how to map fix ranges @@ -97,24 +121,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 @@ -124,14 +136,14 @@ 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 = getBeginningOfLineOffset(node); /* * 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 @@ -139,16 +151,13 @@ function getBlockRangeMap(text, node, comments) { * 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 = getIndentText(text, node).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 @@ -156,35 +165,37 @@ function getBlockRangeMap(text, node, 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. @@ -196,6 +207,7 @@ function getBlockRangeMap(text, node, comments) { mdOffset += line.length + 1; jsOffset += line.length - trimLength + 1; } + return rangeMap; } @@ -205,19 +217,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; @@ -227,35 +239,36 @@ function preprocess(text) { return; } - comments.unshift("/*" + comment + "*/"); + comments.unshift(`/*${comment}*/`); index--; previousNode = parent.children[index]; } blocks.push(assign({}, node, { - comments: comments, + baseIndentText: getIndentText(text, node), + 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. @@ -264,34 +277,38 @@ 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; - while (i < block.rangeMap.length && block.rangeMap[i].js < range) { + let i = 1; + + while (i < block.rangeMap.length && block.rangeMap[i].js <= range) { i++; } // Apply the mapping delta for this range. return range + block.rangeMap[i - 1].md; }), - text: message.fix.text + text: message.fix.text.replace("\n", `\n${block.baseIndentText}`) }; } @@ -315,14 +332,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 }; |