|
1 | 1 | /*--------------------------------------------------------------------------------------------- |
2 | | -* Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
3 | | -* See LICENSE.md in the project root for license terms and full copyright notice. |
4 | | -*--------------------------------------------------------------------------------------------*/ |
| 2 | + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
| 3 | + * See LICENSE.md in the project root for license terms and full copyright notice. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +/** |
| 7 | + * @import { Rule, Scope, AST } from "eslint" |
| 8 | + */ |
5 | 9 |
|
6 | 10 | //Custom rule that enforces version and description for @deprecated |
7 | 11 |
|
8 | 12 | "use strict"; |
9 | 13 |
|
| 14 | +const { DateTime } = require("luxon"); |
| 15 | + |
| 16 | +const regexParts = { |
| 17 | + expired: "might be removed in next major version", |
| 18 | + notUntil: "will not be removed until", |
| 19 | +}; |
| 20 | + |
| 21 | +const deprecatedCommentRegex = new RegExp( |
| 22 | + [ |
| 23 | + /@deprecated(?=[\W])(?: in (?<version>\d+(?:\.(?:\d|x)+){1,2}))?/.source, |
| 24 | + `(?: - (?<when>(?:${regexParts.expired})|(?:${regexParts.notUntil} (?<date>[0-9]{4}(?:-[0-9]{2}){2}))))?`, |
| 25 | + /(?:(?<separator>\.[^\S\r\n])?(?:[^\S\r\n]*)?(?<description>(?=[\S]).{5,}))?/.source, |
| 26 | + ].join(""), |
| 27 | + "gdu", |
| 28 | +); |
| 29 | + |
| 30 | +const validDescriptionRegex = /^[\w\]`]/; |
| 31 | + |
| 32 | +const messages = { |
| 33 | + noDescription: "@deprecated must be followed by deprecation reason and/or alternative API usage required", |
| 34 | + doubleDeprecation: "Multiple @deprecated notes found in the same comment", |
| 35 | + oldDate: `Expired deprecation date was found and should be replaced with "${regexParts.expired}"`, |
| 36 | + noDate: `@deprecated should be followed by either "${regexParts.notUntil} {YYYY-MM-dd}" or "${regexParts.expired}"`, |
| 37 | + noVersion: `@deprecated should be followed by "in {major}.{minor}`, |
| 38 | + noSeparator: |
| 39 | + "Deprecation reason should be separated from any preceding text by `. ` ONLY if there is a version or other information before the description", |
| 40 | + badDescription: `Deprecation reason should match regex ${validDescriptionRegex.source.replace("\\\\", "\\")}`, |
| 41 | + badVersion: "Version numbers should be complete - no 'x' allowed", |
| 42 | +}; |
| 43 | + |
| 44 | +/** @type {typeof messages} */ |
| 45 | +const messageIds = Object.keys(messages).reduce((obj, key) => { |
| 46 | + obj[key] = key; |
| 47 | + return obj; |
| 48 | +}, {}); |
| 49 | + |
| 50 | +/** @param {string} str */ |
| 51 | +function firstUpper(str) { |
| 52 | + return `${str[0].toUpperCase()}${str.substring(1)}`; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * @param {Rule.RuleFixer} fixer |
| 57 | + * @param {AST.Program["comments"][0]} comment |
| 58 | + * @param {string} newText |
| 59 | + */ |
| 60 | +function wrapComment(fixer, comment, newText) { |
| 61 | + // @ts-expect-error -- comments are not considered nodes but this still works |
| 62 | + return fixer.replaceText(comment, comment.type === "Block" ? `/*${newText}*/` : `//${newText}`); |
| 63 | +} |
| 64 | + |
| 65 | +/** @type {Rule.RuleModule & {messageIds: typeof messageIds}} */ |
10 | 66 | module.exports = { |
11 | 67 | meta: { |
12 | | - messages: { |
13 | | - requireVersionAndSentence: "@deprecated in Major.minor format followed by deprecation reason and/or alternative API usage required" |
14 | | - } |
| 68 | + type: "problem", |
| 69 | + messages, |
| 70 | + fixable: "whitespace", |
| 71 | + schema: [ |
| 72 | + { |
| 73 | + type: "object", |
| 74 | + properties: { |
| 75 | + removeOldDates: { |
| 76 | + type: "boolean", |
| 77 | + }, |
| 78 | + addVersion: { |
| 79 | + type: "string", |
| 80 | + }, |
| 81 | + }, |
| 82 | + additionalProperties: false, |
| 83 | + }, |
| 84 | + ], |
15 | 85 | }, |
16 | 86 | create(context) { |
17 | 87 | return { |
18 | | - Program(node) { |
19 | | - let match; |
20 | | - for (const comment of node.comments) { |
21 | | - if (match = /@deprecated(?<in> in \d+\.(\d|x)+[.,\s](?<sentence>.+))?/.exec(comment.value)) { |
22 | | - if ((match?.groups?.in) && (match?.groups?.sentence && match?.groups?.sentence.replace(/\s/g, '').length > 5)) { |
23 | | - continue; |
| 88 | + Program(program) { |
| 89 | + let regexMatch; |
| 90 | + for (const comment of program.comments ?? []) { |
| 91 | + let found = 0; |
| 92 | + /** @type {Rule.Node} */ |
| 93 | + // @ts-expect-error -- comments are not considered nodes but this still works |
| 94 | + const node = comment; |
| 95 | + /** @type {Scope.Scope["block"]} */ |
| 96 | + while ((regexMatch = deprecatedCommentRegex.exec(comment.value))) { |
| 97 | + const match = regexMatch; |
| 98 | + if (found === 1) { |
| 99 | + context.report({ |
| 100 | + node, |
| 101 | + messageId: messageIds.doubleDeprecation, |
| 102 | + }); |
| 103 | + } |
| 104 | + found++; |
| 105 | + if (!match.groups?.description) { |
| 106 | + context.report({ |
| 107 | + node, |
| 108 | + messageId: messageIds.noDescription, |
| 109 | + }); |
| 110 | + continue; // no point in applying any fixes after this - the description will have to be added manually anyway. |
| 111 | + } |
| 112 | + |
| 113 | + let noVersion = false; |
| 114 | + let addDate = false; |
| 115 | + let oldDate = false; |
| 116 | + let addSeparator = false; |
| 117 | + let removeSeparator = false; |
| 118 | + let badDescription = false; |
| 119 | + if (!match.groups?.version) { |
| 120 | + // the version is missing |
| 121 | + if (context.options[0]?.addVersion || match.groups?.when) { |
| 122 | + // either the options require having a version, or the deprecation date is already there, in both cases the version should also be there. |
| 123 | + noVersion = true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (match.groups?.version?.includes("x")) { |
| 128 | + context.report({ |
| 129 | + node, |
| 130 | + messageId: messageIds.badVersion, |
| 131 | + }); |
24 | 132 | } |
25 | | - else { |
26 | | - context.report({ node: comment, messageId: "requireVersionAndSentence" }); |
| 133 | + |
| 134 | + const now = DateTime.now(); |
| 135 | + const currentDate = now.toFormat("yyyy-MM-dd"); |
| 136 | + const targetDate = now.plus({ year: 1 }).toFormat("yyyy-MM-dd"); |
| 137 | + |
| 138 | + if (context.options[0]?.addVersion && !match.groups?.when) { |
| 139 | + // add date |
| 140 | + addDate = true; |
| 141 | + } |
| 142 | + |
| 143 | + if (context.options[0]?.removeOldDates && match.groups?.date && match.groups.date < currentDate) { |
| 144 | + // remove old date |
| 145 | + oldDate = true; |
27 | 146 | } |
| 147 | + |
| 148 | + if (match.groups?.description && !validDescriptionRegex.test(match.groups.description)) badDescription = true; |
| 149 | + |
| 150 | + if ((match.groups?.version || match.groups?.when) && !match.groups?.separator && match.groups?.description) addSeparator = true; |
| 151 | + else if (!(match.groups?.version || match.groups?.when) && match.groups?.separator && match.groups?.description) removeSeparator = true; |
| 152 | + |
| 153 | + const oldDescription = match.groups?.description?.trimStart() ?? ""; |
| 154 | + const newDescription = firstUpper(oldDescription.replace(/^-\s*/, "")); |
| 155 | + const newComment = |
| 156 | + comment.value.substring(0, match.index) + |
| 157 | + "@deprecated" + |
| 158 | + (noVersion && context.options[0]?.addVersion |
| 159 | + ? ` in ${context.options[0].addVersion}` |
| 160 | + : match.groups?.version |
| 161 | + ? ` in ${match.groups.version}` |
| 162 | + : "") + |
| 163 | + (addDate |
| 164 | + ? ` - ${regexParts.notUntil} ${targetDate}` |
| 165 | + : oldDate |
| 166 | + ? ` - ${regexParts.expired}` |
| 167 | + : match.groups?.when |
| 168 | + ? ` - ${match.groups.when}` |
| 169 | + : "") + |
| 170 | + (addDate || oldDate || match.groups?.when || noVersion || match.groups?.version ? "." : "") + |
| 171 | + ` ${newDescription}` + |
| 172 | + comment.value.substring(match.index + match[0].length); |
| 173 | + |
| 174 | + /** @param {Rule.RuleFixer} fixer */ |
| 175 | + const fix = newComment !== comment.value ? (fixer) => wrapComment(fixer, comment, newComment) : undefined; |
| 176 | + if (noVersion) context.report({ node, messageId: messageIds.noVersion, fix }); |
| 177 | + if (addDate) context.report({ node, messageId: messageIds.noDate, fix }); |
| 178 | + if (oldDate) context.report({ node, messageId: messageIds.oldDate, fix }); |
| 179 | + if (addSeparator || removeSeparator) context.report({ node, messageId: messageIds.noSeparator, fix }); |
| 180 | + if (badDescription) context.report({ node, messageId: messageIds.badDescription, fix }); |
28 | 181 | } |
29 | 182 | } |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | -} |
| 183 | + }, |
| 184 | + }; |
| 185 | + }, |
| 186 | + messageIds, |
| 187 | +}; |
0 commit comments