Files
Sebastian Poxhofer b2d3b64c48 refactor(lint): enforce err key for errors in logger metadata (#45759)
* refactor(lint): enforce err key for errors in logger metadata

* refactor(lint): flag error message and stack strings in logger-err-key
2026-09-09 13:55:14 +00:00

41 lines
1.3 KiB
TypeScript

import { logger } from '../../../logger/index.ts';
import { newlineRegex, regEx } from '../../../util/regex.ts';
import type { PackageFileContent } from '../types.ts';
import { Pep723 } from './schema.ts';
// Adapted regex from the Python reference implementation: https://packaging.python.org/en/latest/specifications/inline-script-metadata/#reference-implementation
const regex = regEx(
/^# \/\/\/ (?<type>[a-zA-Z0-9-]+)$\s(?<content>(?:^#(?:| .*)$\s)+)^# \/\/\/$/,
'm',
);
export function extractPep723(
content: string,
packageFile: string,
): PackageFileContent | null {
const match = regex.exec(content);
const matchedContent = match?.groups?.content;
if (!matchedContent) {
return null;
}
// Adapted code from the Python reference implementation: https://packaging.python.org/en/latest/specifications/inline-script-metadata/#reference-implementation
const parsedToml = matchedContent
.split(newlineRegex)
.map((line) => line.substring(line.startsWith('# ') ? 2 : 1))
.join('\n');
const { data: res, error } = Pep723.safeParse(parsedToml);
if (error) {
logger.debug(
{ packageFile, err: error },
`Error parsing PEP 723 inline script metadata`,
);
return null;
}
return res.deps.length ? res : null;
}