UNPKG

@truenine/eslint9-config

Version:

ESLint 9 configuration package for Compose Client projects with TypeScript, Vue, and modern JavaScript support

1 lines 5.65 kB
{"version":3,"file":"beside-comment.cjs","names":[],"sources":["../../../src/rules/code-style/beside-comment.ts"],"sourcesContent":["import type {Rule} from 'eslint' /* eslint-disable ts/no-unsafe-argument */\n\nconst rule: Rule.RuleModule = {\n meta: {\n type: 'layout',\n docs: {\n description: 'Enforce all comments (except JSDoc and tooling) to be beside code to save lines',\n recommended: false\n },\n fixable: 'whitespace',\n messages: {\n besideComment: 'Comment must be on the same line as code (inline/beside) to save vertical space'\n },\n schema: []\n },\n create(context) {\n const {sourceCode} = context\n\n return {\n Program() {\n const comments = sourceCode.getAllComments()\n const {lines} = sourceCode\n\n for (const comment of comments) {\n // eslint-disable-next-line ts/no-unsafe-member-access\n if ((comment as any).type === 'Shebang') continue\n if (comment.type === 'Block' && comment.value.startsWith('*')) continue /* Ignore JSDoc comments (Block comments starting with *) */\n\n const trimmedValue = comment.value.trim()\n if (comment.type === 'Line' && (trimmedValue.startsWith('/') || trimmedValue.startsWith('!'))) continue\n if (trimmedValue.startsWith('#')) continue\n if (comment.type === 'Line' && (/^(?:TODO|FIXME)\\b/i.test(trimmedValue) || trimmedValue.startsWith('@ts-') || trimmedValue.startsWith('ts-') || trimmedValue.startsWith('eslint'))) continue\n\n const {loc, range} = comment\n if (!loc || !range) continue\n\n const startLine = loc.start.line\n const endLine = loc.end.line\n\n for (let i = startLine; i <= endLine; i++) {\n const lineText = lines[i - 1]\n let lineWithCode = lineText\n if (i === startLine && i === endLine) lineWithCode = lineText.slice(0, loc.start.column) + lineText.slice(loc.end.column)\n else if (i === startLine) lineWithCode = lineText.slice(0, loc.start.column)\n else if (i === endLine) lineWithCode = lineText.slice(loc.end.column)\n else lineWithCode = ''\n\n if (lineWithCode.trim() === '') {\n context.report({\n loc: {\n start: {line: i, column: 0},\n end: {line: i, column: lineText.length}\n },\n messageId: 'besideComment',\n fix(fixer) {\n if (i === endLine && lines[i] !== void 0 && lines[i].trim() !== '') { /* Attempt to move the comment to the end of the next line if it contains code */\n const nextLineText = lines[i]\n const commentText = sourceCode.getText(comment as any).trim()\n /* Then append the comment to the end of the next line */ /* Replace the original comment and following newline with nothing */\n /* For now, just a simple replacement of the comment with empty space */ /* This is a bit complex due to potential multiple standalone comments */\n const nextLineEnd = sourceCode.getIndexFromLoc({line: i + 1, column: nextLineText.length})\n return fixer.replaceTextRange([range[0], nextLineEnd], `${nextLineText} ${commentText}`)\n }\n\n if (i <= 1 || (i !== startLine && lines[i - 2].trim() !== '')) return null\n\n const prevLineText = lines[i - 2]\n const commentText = sourceCode.getText(comment as any).trim()\n const prevLineStart = sourceCode.getIndexFromLoc({line: i - 1, column: 0})\n return fixer.replaceTextRange([prevLineStart, range[1]], `${prevLineText} ${commentText}`)\n }\n })\n break\n }\n }\n }\n }\n }\n }\n}\n\nexport default rule\n"],"mappings":";;AAEA,MAAM,OAAwB;CAC5B,MAAM;EACJ,MAAM;EACN,MAAM;GACJ,aAAa;GACb,aAAa;GACd;EACD,SAAS;EACT,UAAU,EACR,eAAe,mFAChB;EACD,QAAQ,EAAE;EACX;CACD,OAAO,SAAS;EACd,MAAM,EAAC,eAAc;AAErB,SAAO,EACL,UAAU;GACR,MAAM,WAAW,WAAW,gBAAgB;GAC5C,MAAM,EAAC,UAAS;AAEhB,QAAK,MAAM,WAAW,UAAU;AAE9B,QAAK,QAAgB,SAAS,UAAW;AACzC,QAAI,QAAQ,SAAS,WAAW,QAAQ,MAAM,WAAW,IAAI,CAAE;IAE/D,MAAM,eAAe,QAAQ,MAAM,MAAM;AACzC,QAAI,QAAQ,SAAS,WAAW,aAAa,WAAW,IAAI,IAAI,aAAa,WAAW,IAAI,EAAG;AAC/F,QAAI,aAAa,WAAW,IAAI,CAAE;AAClC,QAAI,QAAQ,SAAS,WAAW,qBAAqB,KAAK,aAAa,IAAI,aAAa,WAAW,OAAO,IAAI,aAAa,WAAW,MAAM,IAAI,aAAa,WA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