Skip to content

Commit

Permalink
perf: avoid recalculating min and max lengths in align-attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 committed Jan 3, 2024
1 parent 4ac0a65 commit 8272565
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
21 changes: 6 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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'];

Expand Down
11 changes: 7 additions & 4 deletions lib/rules/align-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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 &&
Expand Down
11 changes: 6 additions & 5 deletions lib/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit 8272565

Please sign in to comment.