I am writing an ESLint rule, and I have two scopes, one that "scans" the file and looks for export declarations, and the other scope runs when inside a specific file (index.ts).
I need to "collect" the result of the previous run and save it. Is there a best practice here for sharing data between 2 scopes?
Consider the code:
module.exports = {
'a-rule': {
meta: {
type: 'problem',
},
create(context) {
const filename = context.getFilename();
return {
Program: (node) => {
node.body.forEach(node => {
if (node.type === 'ExportNamedDeclaration') {
const absolutePath = path.resolve(filename);
array.push(absolutePath);
} else if (node.type === 'SomeOtherUseCase') {
console.log(array); // empty
}
}
}
The code will run multiple times in a few different scopes so that that array will get re-instantiated. I found a solution to declare the array at the beginning of the rule file, but I wonder if there isn't a better way.
Thanks