Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to enforce the position of line comments |
| 3 | * @author Alberto RodrÃguez |
| 4 | */ |
| 5 | "use strict"; |
| 6 | |
| 7 | const astUtils = require("./utils/ast-utils"); |
| 8 | |
| 9 | //------------------------------------------------------------------------------ |
| 10 | // Rule Definition |
| 11 | //------------------------------------------------------------------------------ |
| 12 | |
| 13 | module.exports = { |
| 14 | meta: { |
| 15 | type: "layout", |
| 16 | |
| 17 | docs: { |
| 18 | description: "enforce position of line comments", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 19 | recommended: false, |
| 20 | url: "https://eslint.org/docs/rules/line-comment-position" |
| 21 | }, |
| 22 | |
| 23 | schema: [ |
| 24 | { |
| 25 | oneOf: [ |
| 26 | { |
| 27 | enum: ["above", "beside"] |
| 28 | }, |
| 29 | { |
| 30 | type: "object", |
| 31 | properties: { |
| 32 | position: { |
| 33 | enum: ["above", "beside"] |
| 34 | }, |
| 35 | ignorePattern: { |
| 36 | type: "string" |
| 37 | }, |
| 38 | applyDefaultPatterns: { |
| 39 | type: "boolean" |
| 40 | }, |
| 41 | applyDefaultIgnorePatterns: { |
| 42 | type: "boolean" |
| 43 | } |
| 44 | }, |
| 45 | additionalProperties: false |
| 46 | } |
| 47 | ] |
| 48 | } |
| 49 | ], |
| 50 | messages: { |
| 51 | above: "Expected comment to be above code.", |
| 52 | beside: "Expected comment to be beside code." |
| 53 | } |
| 54 | }, |
| 55 | |
| 56 | create(context) { |
| 57 | const options = context.options[0]; |
| 58 | |
| 59 | let above, |
| 60 | ignorePattern, |
| 61 | applyDefaultIgnorePatterns = true; |
| 62 | |
| 63 | if (!options || typeof options === "string") { |
| 64 | above = !options || options === "above"; |
| 65 | |
| 66 | } else { |
| 67 | above = !options.position || options.position === "above"; |
| 68 | ignorePattern = options.ignorePattern; |
| 69 | |
| 70 | if (Object.prototype.hasOwnProperty.call(options, "applyDefaultIgnorePatterns")) { |
| 71 | applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns; |
| 72 | } else { |
| 73 | applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN; |
| 78 | const fallThroughRegExp = /^\s*falls?\s?through/u; |
| 79 | const customIgnoreRegExp = new RegExp(ignorePattern, "u"); |
| 80 | const sourceCode = context.getSourceCode(); |
| 81 | |
| 82 | //-------------------------------------------------------------------------- |
| 83 | // Public |
| 84 | //-------------------------------------------------------------------------- |
| 85 | |
| 86 | return { |
| 87 | Program() { |
| 88 | const comments = sourceCode.getAllComments(); |
| 89 | |
| 90 | comments.filter(token => token.type === "Line").forEach(node => { |
| 91 | if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (ignorePattern && customIgnoreRegExp.test(node.value)) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | const previous = sourceCode.getTokenBefore(node, { includeComments: true }); |
| 100 | const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line; |
| 101 | |
| 102 | if (above) { |
| 103 | if (isOnSameLine) { |
| 104 | context.report({ |
| 105 | node, |
| 106 | messageId: "above" |
| 107 | }); |
| 108 | } |
| 109 | } else { |
| 110 | if (!isOnSameLine) { |
| 111 | context.report({ |
| 112 | node, |
| 113 | messageId: "beside" |
| 114 | }); |
| 115 | } |
| 116 | } |
| 117 | }); |
| 118 | } |
| 119 | }; |
| 120 | } |
| 121 | }; |