-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract verifyWithSharedScopes
- Loading branch information
1 parent
e6fd1e3
commit 17f8838
Showing
2 changed files
with
62 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const { splatSet } = require("./utils") | ||
|
||
module.exports = { verifyWithSharedScopes } | ||
|
||
function verifyWithSharedScopes(codeParts, verifyCodePart, parserOptions) { | ||
// First pass: collect needed globals and declared globals for each script tags. | ||
const firstPassValues = [] | ||
|
||
for (const codePart of codeParts) { | ||
verifyCodePart(codePart, { | ||
prepare(context) { | ||
const globalScope = context.getScope() | ||
// See https://github.com/eslint/eslint/blob/4b267a5c8a42477bb2384f33b20083ff17ad578c/lib/rules/no-redeclare.js#L67-L78 | ||
let scopeForDeclaredGlobals | ||
if ( | ||
parserOptions.ecmaFeatures && | ||
parserOptions.ecmaFeatures.globalReturn | ||
) { | ||
scopeForDeclaredGlobals = globalScope.childScopes[0] | ||
} else { | ||
scopeForDeclaredGlobals = globalScope | ||
} | ||
|
||
firstPassValues.push({ | ||
codePart, | ||
exportedGlobals: globalScope.through.map( | ||
(node) => node.identifier.name | ||
), | ||
declaredGlobals: scopeForDeclaredGlobals.variables.map( | ||
(variable) => variable.name | ||
), | ||
}) | ||
}, | ||
ignoreRules: true, | ||
}) | ||
} | ||
|
||
// Second pass: declare variables for each script scope, then run eslint. | ||
for (let i = 0; i < firstPassValues.length; i += 1) { | ||
verifyCodePart(firstPassValues[i].codePart, { | ||
prepare(context) { | ||
const exportedGlobals = splatSet( | ||
firstPassValues | ||
.slice(i + 1) | ||
.map((nextValues) => nextValues.exportedGlobals) | ||
) | ||
for (const name of exportedGlobals) context.markVariableAsUsed(name) | ||
|
||
const declaredGlobals = splatSet( | ||
firstPassValues | ||
.slice(0, i) | ||
.map((previousValues) => previousValues.declaredGlobals) | ||
) | ||
const scope = context.getScope() | ||
scope.through = scope.through.filter((variable) => { | ||
return !declaredGlobals.has(variable.identifier.name) | ||
}) | ||
}, | ||
}) | ||
} | ||
} |