Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: refactoring for prefer-global/*
- Loading branch information
1 parent
4dd9baf
commit 8f25248
Showing
7 changed files
with
139 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const { ReferenceTracker } = require("eslint-utils") | ||
|
||
/** | ||
* Verifier for `prefer-global/*` rules. | ||
*/ | ||
class Verifier { | ||
/** | ||
* Initialize this instance. | ||
* @param {RuleContext} context The rule context to report. | ||
* @param {{modules:object,globals:object}} trackMap The track map. | ||
*/ | ||
constructor(context, trackMap) { | ||
this.context = context | ||
this.trackMap = trackMap | ||
this.verify = | ||
context.options[0] === "never" | ||
? this.verifyToPreferModules | ||
: this.verifyToPreferGlobals | ||
} | ||
|
||
/** | ||
* Verify the code to suggest the use of globals. | ||
* @returns {void} | ||
*/ | ||
verifyToPreferGlobals() { | ||
const { context, trackMap } = this | ||
const tracker = new ReferenceTracker(context.getScope(), { | ||
mode: "legacy", | ||
}) | ||
|
||
for (const { node } of [ | ||
...tracker.iterateCjsReferences(trackMap.modules), | ||
...tracker.iterateEsmReferences(trackMap.modules), | ||
]) { | ||
context.report({ node, messageId: "preferGlobal" }) | ||
} | ||
} | ||
|
||
/** | ||
* Verify the code to suggest the use of modules. | ||
* @returns {void} | ||
*/ | ||
verifyToPreferModules() { | ||
const { context, trackMap } = this | ||
const tracker = new ReferenceTracker(context.getScope()) | ||
|
||
for (const { node } of tracker.iterateGlobalReferences( | ||
trackMap.globals | ||
)) { | ||
context.report({ node, messageId: "preferModule" }) | ||
} | ||
} | ||
} | ||
|
||
module.exports = function checkForPreferGlobal(context, trackMap) { | ||
new Verifier(context, trackMap).verify() | ||
} |
This file was deleted.
Oops, something went wrong.