From d799b69191883d9dd097b8e2b3873947ee13ac28 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Mon, 29 Jul 2024 06:44:21 -0400 Subject: [PATCH 1/2] feat: error attempting to harden 'function' export --- .../eslint-plugin/lib/rules/harden-exports.js | 7 ++- .../eslint-plugin/test/harden-exports.test.js | 43 ++----------------- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/packages/eslint-plugin/lib/rules/harden-exports.js b/packages/eslint-plugin/lib/rules/harden-exports.js index 373195029f..c7d3d4043f 100644 --- a/packages/eslint-plugin/lib/rules/harden-exports.js +++ b/packages/eslint-plugin/lib/rules/harden-exports.js @@ -59,8 +59,11 @@ module.exports = { } } } else if (exportNode.declaration.type === 'FunctionDeclaration') { - // @ts-expect-error xxx typedef - exportNames.push(exportNode.declaration.id.name); + context.report({ + node: exportNode, + // The 'function' keyword hoisting makes the valuable mutable before it can be hardened. + message: `Export '${exportNode.declaration.id.name}' should be a const declaration with an arrow function.`, + }); } } else if (exportNode.specifiers) { for (const spec of exportNode.specifiers) { diff --git a/packages/eslint-plugin/test/harden-exports.test.js b/packages/eslint-plugin/test/harden-exports.test.js index 7aa833ea14..d97fbbaa01 100644 --- a/packages/eslint-plugin/test/harden-exports.test.js +++ b/packages/eslint-plugin/test/harden-exports.test.js @@ -20,37 +20,6 @@ harden(b); }, { code: ` -export function foo() { - console.log("foo"); - } -harden(foo); -export const a = 1; -harden(a); - `, - }, - { - code: ` -export const a = 1; -harden(a); -export function bar() { - console.log("bar"); - } -harden(bar); - `, - }, - { - code: ` -export const a = 1; -harden(a); -export function - multilineFunction() { - console.log("This is a multiline function."); - } -harden(multilineFunction); - `, - }, - { - code: ` export const { getEnvironmentOption, getEnvironmentOptionsList, @@ -109,14 +78,13 @@ export function foo() { errors: [ { message: - "The named export 'foo' should be followed by a call to 'harden'.", + "Export 'foo' should be a const declaration with an arrow function.", }, ], output: ` export function foo() { console.log("foo"); } -harden(foo); `, }, { @@ -129,7 +97,7 @@ export function errors: [ { message: - "The named export 'multilineFunction' should be followed by a call to 'harden'.", + "Export 'multilineFunction' should be a const declaration with an arrow function.", }, ], output: ` @@ -137,7 +105,6 @@ export function multilineFunction() { console.log("This is a multiline function."); } -harden(multilineFunction); `, }, { @@ -167,11 +134,11 @@ export function }, { message: - "The named export 'foo' should be followed by a call to 'harden'.", + "Export 'foo' should be a const declaration with an arrow function.", }, { message: - "The named export 'multilineFunction' should be followed by a call to 'harden'.", + "Export 'multilineFunction' should be a const declaration with an arrow function.", }, ], output: ` @@ -186,12 +153,10 @@ harden(alreadyHardened); export function foo() { console.log("foo"); } -harden(foo); export function multilineFunction() { console.log("This is a multiline function."); } -harden(multilineFunction); `, }, { From c39b2327d82b7891ec81028f6937051585c25643 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Mon, 29 Jul 2024 06:45:32 -0400 Subject: [PATCH 2/2] chore: more concise error message --- packages/eslint-plugin/lib/rules/harden-exports.js | 2 +- packages/eslint-plugin/test/harden-exports.test.js | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/lib/rules/harden-exports.js b/packages/eslint-plugin/lib/rules/harden-exports.js index c7d3d4043f..87a87930b0 100644 --- a/packages/eslint-plugin/lib/rules/harden-exports.js +++ b/packages/eslint-plugin/lib/rules/harden-exports.js @@ -94,7 +94,7 @@ module.exports = { const noun = missingHardenCalls.length === 1 ? 'export' : 'exports'; context.report({ node: exportNode, - message: `The named ${noun} '${missingHardenCalls.join(', ')}' should be followed by a call to 'harden'.`, + message: `Named ${noun} '${missingHardenCalls.join(', ')}' should be followed by a call to 'harden'.`, fix: function (fixer) { const hardenCalls = missingHardenCalls .map(name => `harden(${name});`) diff --git a/packages/eslint-plugin/test/harden-exports.test.js b/packages/eslint-plugin/test/harden-exports.test.js index d97fbbaa01..bd00f69dca 100644 --- a/packages/eslint-plugin/test/harden-exports.test.js +++ b/packages/eslint-plugin/test/harden-exports.test.js @@ -42,8 +42,7 @@ harden(a); `, errors: [ { - message: - "The named export 'b' should be followed by a call to 'harden'.", + message: "Named export 'b' should be followed by a call to 'harden'.", }, ], output: ` @@ -60,8 +59,7 @@ export const a = 1; `, errors: [ { - message: - "The named export 'a' should be followed by a call to 'harden'.", + message: "Named export 'a' should be followed by a call to 'harden'.", }, ], output: ` @@ -125,12 +123,10 @@ export function `, errors: [ { - message: - "The named export 'a' should be followed by a call to 'harden'.", + message: "Named export 'a' should be followed by a call to 'harden'.", }, { - message: - "The named export 'b' should be followed by a call to 'harden'.", + message: "Named export 'b' should be followed by a call to 'harden'.", }, { message: @@ -170,7 +166,7 @@ environmentOptionsListHas, errors: [ { message: - "The named exports 'getEnvironmentOption, getEnvironmentOptionsList, environmentOptionsListHas' should be followed by a call to 'harden'.", + "Named exports 'getEnvironmentOption, getEnvironmentOptionsList, environmentOptionsListHas' should be followed by a call to 'harden'.", }, ], output: `