diff --git a/lib/index.ts b/lib/index.ts index 145bc72..5aced13 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -15,7 +15,7 @@ import requireDownloadUrl from './rules/require-download-url'; import requireName from './rules/require-name'; import requireVersion from './rules/require-version'; import useHomepageAndUrl from './rules/use-homepage-and-url'; -import type { ESLint } from 'eslint'; +import type { ESLint, Rule } from 'eslint'; const rules = Object.fromEntries( Object.entries({ @@ -34,20 +34,11 @@ const rules = Object.fromEntries( 'require-name': requireName, 'require-version': requireVersion, 'use-homepage-and-url': useHomepageAndUrl - }).map(([ruleName, ruleMeta]) => { - return [ - ruleName, - { - ...ruleMeta, - meta: { - ...ruleMeta.meta, - docs: { - ...ruleMeta.meta.docs, - url: `https://yash-singh1.github.io/eslint-plugin-userscripts/#/rules/${ruleName}` - } - } - } - ]; + }).map(([ruleName, ruleMeta]: [string, Rule.RuleModule]) => { + if (ruleMeta.meta?.docs) { + ruleMeta.meta.docs.url = `https://yash-singh1.github.io/eslint-plugin-userscripts/#/rules/${ruleName}`; + } + return [ruleName, ruleMeta]; }) ) satisfies ESLint.Plugin['rules']; diff --git a/lib/rules/align-attributes.ts b/lib/rules/align-attributes.ts index 6fd0bd3..86ed2c4 100644 --- a/lib/rules/align-attributes.ts +++ b/lib/rules/align-attributes.ts @@ -37,6 +37,8 @@ export default { }[] = []; let start: Position | null = null; let end: Position | null = null; + let maxLength: number = 0; + let minSpace: number = Number.POSITIVE_INFINITY; for (const comment of comments) { if (done || comment.type !== 'Line' || !isNonNullishComment(comment)) { @@ -67,6 +69,9 @@ export default { line: comment.loc.start.line, comment }); + + maxLength = Math.max(maxLength, metadata.at(-1)!.key.length); + minSpace = Math.min(minSpace, metadata.at(-1)!.space); } } @@ -78,11 +83,9 @@ export default { return {}; } - const totalSpacing = - Math.max(...metadata.map(({ key }) => key.length)) + spacing; + const totalSpacing = maxLength + spacing; - const hasSpaceLessThenSpacing = - metadata.map(({ space }) => space).sort()[0] < spacing; + const hasSpaceLessThenSpacing = minSpace < spacing; if ( start && diff --git a/lib/utils/parse.ts b/lib/utils/parse.ts index eb69a31..b6907cf 100644 --- a/lib/utils/parse.ts +++ b/lib/utils/parse.ts @@ -77,6 +77,7 @@ export function parse(sourceCode: SourceCode) { const trimmedLine = line.trim(); const isLineComment = trimmedLine.startsWith('//'); + const commentContent = trimmedLine.slice(2).trim(); if ( // https://github.com/Yash-Singh1/eslint-plugin-userscripts/issues/8 @@ -93,7 +94,7 @@ export function parse(sourceCode: SourceCode) { } else if ( inMetadata && isLineComment && - trimmedLine.slice(2).trim() === '==/UserScript==' + commentContent === '==/UserScript==' ) { result.end = true; done = true; @@ -106,7 +107,7 @@ export function parse(sourceCode: SourceCode) { } else if ( !inMetadata && isLineComment && - trimmedLine.slice(2).trim() === '==UserScript==' + commentContent === '==UserScript==' ) { result.enteredMetadata = index; inMetadata = true; @@ -119,12 +120,12 @@ export function parse(sourceCode: SourceCode) { } else if ( inMetadata && isLineComment && - trimmedLine.slice(2).trim() !== '' // nor an empty line, (see #8 above) + commentContent !== '' // nor an empty line, (see #8 above) ) { if ( - trimmedLine.slice(2).trim().startsWith('@') // is a header + commentContent.startsWith('@') // is a header ) { - const mainContent = trimmedLine.slice(2).trim().slice(1); + const mainContent = commentContent.slice(1); result.lines.push({ value: line, diff --git a/tests/lib/index.ts b/tests/lib/index.ts index 5de89a2..a3dfb67 100644 --- a/tests/lib/index.ts +++ b/tests/lib/index.ts @@ -16,6 +16,6 @@ describe('config', () => { describe('rules', () => { it('should have meta.docs.url', () => { - plugin.rules[Object.keys(plugin.rules)[0]].meta.docs.url.should.be.String; + plugin.rules[Object.keys(plugin.rules)[0]].meta!.docs!.url!.should.be.String; }); });