Skip to content

Commit

Permalink
Set isToggleImportedFromOpticks in the settings object to allow mocki…
Browse files Browse the repository at this point in the history
…ng in the tests
  • Loading branch information
Gerrit88 committed Apr 2, 2024
1 parent 7e593e2 commit f7e2c79
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
9 changes: 3 additions & 6 deletions packages/eslint-plugin/lib/rules/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,18 @@ module.exports = {
},

create(context) {
// QUESTION it seems the test runner reads from `context.settings`
// while .eslintrc.js reads from `settings`
// The test runner reads from `context.settings`, while .eslintrc.js reads from `settings`
const settings = context.settings || settings
const {opticks} = settings

let isToggleImportedFromOpticks = false

return {
ImportDeclaration: (node) => {
const importPath = node.source.value

if (importPath === 'opticks') {
node.specifiers.forEach((specifier) => {
if (specifier.imported.name === 'toggle') {
isToggleImportedFromOpticks = true
settings.isToggleImportedFromOpticks = true
}
})
}
Expand All @@ -63,7 +60,7 @@ module.exports = {
callee: {name}
} = node

if (name === 'toggle' && isToggleImportedFromOpticks) {
if (name === 'toggle' && settings.isToggleImportedFromOpticks) {
// Check for invalid number of variants
if (node.arguments.length === 2) {
return context.report({
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-plugin/tests/lib/rules/toggle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @fileoverview Opticks
* @author Gerrit
* @warning These test do not work with the isToggleImportedFromOpticks logic in the rules.
* To run the test, set isToggleImportedFromOpticks to "true" in the rule.
* @author Gerrit Burger
*/
'use strict'

Expand All @@ -19,7 +17,9 @@ const rule = require('../../../lib/rules/toggle'),

RuleTester.setDefaultConfig({
settings: {
opticks: {experiments: {foo: 'a', bar: null, baz: 'b'}}
opticks: {experiments: {foo: 'a', bar: null, baz: 'b'}},
// Mock the import of the toggle function from the opticks package
isToggleImportedFromOpticks: true
}
})
const ruleTester = new RuleTester()
Expand Down

0 comments on commit f7e2c79

Please sign in to comment.