Files
renovate/lib/util/json-writer/editor-config.ts
Jamie Tanna 021612414c fix(lint): use parseInt over Number (#45472)
* fix(lint): use `parseInt` over `Number`

As a code review comment that comes up every so often, we can add a
linting rule to enforce this check.

This also fixes any existing usages.

As this could be breaking, we can release it as a `fix`.

Co-authored-by: Claude Sonnet 5 <jamie.tanna+claude-code@mend.io>

* fixup! fix(lint): use `parseInt` over `Number`

* fixup! fix(lint): use `parseInt` over `Number`

---------

Co-authored-by: Claude Sonnet 5 <jamie.tanna+claude-code@mend.io>
2026-08-26 13:02:26 +00:00

52 lines
1.5 KiB
TypeScript

import { isNumber } from '@sindresorhus/is';
import type { Props } from 'editorconfig';
import { parse } from 'editorconfig';
import upath from 'upath';
import { GlobalConfig } from '../../config/global.ts';
import { logger } from '../../logger/index.ts';
import type { CodeFormat } from './code-format.ts';
import type { IndentationType } from './indentation-type.ts';
export class EditorConfig {
public static async getCodeFormat(fileName: string): Promise<CodeFormat> {
const localDir = GlobalConfig.get('localDir');
try {
const knownProps = await parse(upath.join(localDir, fileName));
return {
indentationSize: EditorConfig.getIndentationSize(knownProps),
indentationType: EditorConfig.getIndentationType(knownProps),
maxLineLength: knownProps.max_line_length as number | 'off' | undefined,
};
} catch (err) {
logger.warn({ err }, 'Failed to parse editor config');
return {};
}
}
private static getIndentationType(
knownProps: Props,
): IndentationType | undefined {
const { indent_style: indentStyle } = knownProps;
if (indentStyle === 'tab') {
return 'tab';
}
if (indentStyle === 'space') {
return 'space';
}
return undefined;
}
private static getIndentationSize(knownProps: Props): number | undefined {
const { indent_size: indentSize } = knownProps;
if (isNumber(indentSize) && Number.isInteger(indentSize)) {
return indentSize;
}
return undefined;
}
}