|
| 1 | +import type { |
| 2 | + DirectiveType, |
| 3 | + FileProblem, |
| 4 | + RulesConfig, |
| 5 | + SourceLocation, |
| 6 | +} from '@eslint/core'; |
| 7 | +import { |
| 8 | + ConfigCommentParser, |
| 9 | + Directive, |
| 10 | + TextSourceCodeBase, |
| 11 | +} from '@eslint/plugin-kit'; |
| 12 | +import type { Comment, Location, Program } from 'php-parser'; |
| 13 | + |
| 14 | +type NormalizedComment = { |
| 15 | + value: string; |
| 16 | + loc: Location | null; |
| 17 | +}; |
| 18 | + |
| 19 | +type InlineConfigElement = { |
| 20 | + config: { rules: RulesConfig }; |
| 21 | + loc: SourceLocation; |
| 22 | +}; |
| 23 | + |
| 24 | +const INLINE_CONFIG = |
| 25 | + /^\s*(?:eslint(?:-enable|-disable(?:(?:-next)?-line)?)?)(?:\s|$)/u; |
| 26 | + |
| 27 | +const ESLINT_DIRECTIVES = [ |
| 28 | + 'eslint-disable', |
| 29 | + 'eslint-enable', |
| 30 | + 'eslint-disable-next-line', |
| 31 | + 'eslint-disable-line', |
| 32 | +]; |
| 33 | + |
| 34 | +const commentParser = new ConfigCommentParser(); |
| 35 | + |
| 36 | +/** |
| 37 | + * This class is heavily inspired by `@eslint/json` & `@eslint/css`: |
| 38 | + * |
| 39 | + * @see https://github.com/eslint/json/blob/42cca7789/src/languages/json-source-code.js |
| 40 | + * @see https://github.com/eslint/css/blob/fa391df2c/src/languages/css-source-code.js |
| 41 | + */ |
| 42 | +export abstract class TextSourceCodeWithComments extends TextSourceCodeBase { |
| 43 | + #inlineConfigComments: NormalizedComment[] | null = null; |
| 44 | + |
| 45 | + public override ast: Program; |
| 46 | + public override text: string; |
| 47 | + public comments: NormalizedComment[]; |
| 48 | + |
| 49 | + constructor({ ast, text }: { ast: Program; text: string }) { |
| 50 | + super({ ast, text }); |
| 51 | + |
| 52 | + this.ast = ast; |
| 53 | + this.text = text; |
| 54 | + this.comments = this.normalizeComments(ast.comments ?? []); |
| 55 | + } |
| 56 | + |
| 57 | + private normalizeComments(comments: Comment[]): NormalizedComment[] { |
| 58 | + return comments.reduce<NormalizedComment[]>((acc, comment) => { |
| 59 | + if (!comment.loc) { |
| 60 | + return acc; |
| 61 | + } |
| 62 | + |
| 63 | + if (comment.kind === 'commentblock') { |
| 64 | + acc.push({ |
| 65 | + value: comment.value |
| 66 | + .replace(/^\/\*\s*(.*?)\s*\*\/$/gm, '$1') |
| 67 | + .trim(), |
| 68 | + loc: comment.loc, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + if (comment.kind === 'commentline') { |
| 73 | + // `php-parser` sets the end line wrong. |
| 74 | + comment.loc.end.line = comment.loc.start.line; |
| 75 | + |
| 76 | + acc.push({ |
| 77 | + value: comment.value.replace(/^\/\/\s*/gm, '').trim(), |
| 78 | + loc: comment.loc, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + return acc; |
| 83 | + }, []); |
| 84 | + } |
| 85 | + |
| 86 | + getInlineConfigNodes() { |
| 87 | + if (!this.#inlineConfigComments) { |
| 88 | + this.#inlineConfigComments = this.comments.filter((comment) => |
| 89 | + INLINE_CONFIG.test(comment.value), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + return this.#inlineConfigComments; |
| 94 | + } |
| 95 | + |
| 96 | + getDisableDirectives() { |
| 97 | + const problems: FileProblem[] = []; |
| 98 | + const directives: Directive[] = []; |
| 99 | + |
| 100 | + this.getInlineConfigNodes().forEach((comment) => { |
| 101 | + const parsedComment = commentParser.parseDirective(comment.value); |
| 102 | + |
| 103 | + if (!parsedComment || !comment.loc) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const { label, value, justification } = parsedComment; |
| 108 | + |
| 109 | + // `eslint-disable-line` directives are not allowed to span multiple lines as it would be confusing to which lines they apply |
| 110 | + if ( |
| 111 | + label === 'eslint-disable-line' && |
| 112 | + comment.loc.start.line !== comment.loc.end.line |
| 113 | + ) { |
| 114 | + const message = `${label} comment should not span multiple lines.`; |
| 115 | + |
| 116 | + problems.push({ |
| 117 | + ruleId: null, |
| 118 | + message, |
| 119 | + loc: comment.loc, |
| 120 | + }); |
| 121 | + |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (ESLINT_DIRECTIVES.includes(label)) { |
| 126 | + const directiveType = label.replace(/^eslint-/, ''); |
| 127 | + |
| 128 | + directives.push( |
| 129 | + new Directive({ |
| 130 | + type: directiveType as DirectiveType, |
| 131 | + node: comment, |
| 132 | + value, |
| 133 | + justification, |
| 134 | + }), |
| 135 | + ); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + return { problems, directives }; |
| 140 | + } |
| 141 | + |
| 142 | + applyInlineConfig() { |
| 143 | + const problems: FileProblem[] = []; |
| 144 | + const configs: InlineConfigElement[] = []; |
| 145 | + |
| 146 | + this.getInlineConfigNodes().forEach((comment) => { |
| 147 | + const parsedComment = commentParser.parseDirective(comment.value); |
| 148 | + |
| 149 | + if (!parsedComment || !comment.loc) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + const { label, value } = parsedComment; |
| 154 | + |
| 155 | + if (label !== 'eslint') { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + const parseResult = commentParser.parseJSONLikeConfig(value); |
| 160 | + |
| 161 | + if (parseResult.ok) { |
| 162 | + configs.push({ |
| 163 | + config: { |
| 164 | + rules: parseResult.config, |
| 165 | + }, |
| 166 | + loc: comment.loc, |
| 167 | + }); |
| 168 | + |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + problems.push({ |
| 173 | + ruleId: null, |
| 174 | + message: parseResult.error.message, |
| 175 | + loc: comment.loc, |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + return { |
| 180 | + configs, |
| 181 | + problems, |
| 182 | + }; |
| 183 | + } |
| 184 | +} |
0 commit comments