diff --git a/apps/oxlint/src-js/plugins/context.ts b/apps/oxlint/src-js/plugins/context.ts index 87ab5b498f754..df45bb9aa2ffa 100644 --- a/apps/oxlint/src-js/plugins/context.ts +++ b/apps/oxlint/src-js/plugins/context.ts @@ -89,6 +89,9 @@ export interface InternalContext { messages: Record | null; } +// Cached current working directory. +let cachedCwd: string | null = null; + /** * Context class. * @@ -132,6 +135,12 @@ export class Context { return getInternal(this, 'access `context.physicalFilename`').filePath; } + // Getter for current working directory. + get cwd() { + getInternal(this, 'access `context.cwd`'); + return cachedCwd ??= process.cwd(); + } + // Getter for options for file being linted. get options() { return getInternal(this, 'access `context.options`').options; diff --git a/apps/oxlint/test/fixtures/context_properties/plugin.ts b/apps/oxlint/test/fixtures/context_properties/plugin.ts index 59a2ba45e47b5..f843757e4e504 100644 --- a/apps/oxlint/test/fixtures/context_properties/plugin.ts +++ b/apps/oxlint/test/fixtures/context_properties/plugin.ts @@ -35,6 +35,14 @@ const rule: Rule = { node: SPAN, }); + if (context.cwd.length === 0) { + context.report({ message: 'cwd.length === 0', node: SPAN }); + } + + if (context.cwd !== process.cwd()) { + context.report({ message: 'cwd !== process.cwd()', node: SPAN }); + } + if (this !== rule) context.report({ message: 'this !== rule', node: SPAN }); return {