From 1fa8a07a791b2497c8003e63e45f359eb65b1508 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 30 Sep 2024 13:59:51 +0700 Subject: [PATCH] [Refactor] create `sourceType` helper --- src/core/sourceType.js | 7 +++++++ src/rules/no-default-export.js | 3 ++- src/rules/no-named-export.js | 3 ++- src/rules/unambiguous.js | 3 ++- 4 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/core/sourceType.js diff --git a/src/core/sourceType.js b/src/core/sourceType.js new file mode 100644 index 000000000..4243be4cf --- /dev/null +++ b/src/core/sourceType.js @@ -0,0 +1,7 @@ +/** + * @param {import('eslint').Rule.RuleContext} context + * @returns 'module' | 'script' | undefined + */ +export default function sourceType(context) { + return context.parserOptions.sourceType; +} diff --git a/src/rules/no-default-export.js b/src/rules/no-default-export.js index fcb4f1b2f..d18f0c48f 100644 --- a/src/rules/no-default-export.js +++ b/src/rules/no-default-export.js @@ -1,6 +1,7 @@ import { getSourceCode } from 'eslint-module-utils/contextCompat'; import docsUrl from '../docsUrl'; +import sourceType from '../core/sourceType'; module.exports = { meta: { @@ -15,7 +16,7 @@ module.exports = { create(context) { // ignore non-modules - if (context.parserOptions.sourceType !== 'module') { + if (sourceType(context) !== 'module') { return {}; } diff --git a/src/rules/no-named-export.js b/src/rules/no-named-export.js index efaf9dc4c..fc9b2c48d 100644 --- a/src/rules/no-named-export.js +++ b/src/rules/no-named-export.js @@ -1,3 +1,4 @@ +import sourceType from '../core/sourceType'; import docsUrl from '../docsUrl'; module.exports = { @@ -13,7 +14,7 @@ module.exports = { create(context) { // ignore non-modules - if (context.parserOptions.sourceType !== 'module') { + if (sourceType(context) !== 'module') { return {}; } diff --git a/src/rules/unambiguous.js b/src/rules/unambiguous.js index 91152ea2a..2491fad3e 100644 --- a/src/rules/unambiguous.js +++ b/src/rules/unambiguous.js @@ -5,6 +5,7 @@ import { isModule } from 'eslint-module-utils/unambiguous'; import docsUrl from '../docsUrl'; +import sourceType from '../core/sourceType'; module.exports = { meta: { @@ -19,7 +20,7 @@ module.exports = { create(context) { // ignore non-modules - if (context.parserOptions.sourceType !== 'module') { + if (sourceType(context) !== 'module') { return {}; }