Skip to content

Commit

Permalink
refactor: extract verifyWithSharedScopes
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Feb 6, 2024
1 parent e6fd1e3 commit 17f8838
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 59 deletions.
60 changes: 1 addition & 59 deletions src/verifyPatch.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const getSettings = require("./settings").getSettings
const getFileMode = require("./getFileMode")
const extract = require("./extract")
const { splatSet } = require("./utils")
const { remapMessages } = require("./remapMessages")
const { verifyWithSharedScopes } = require("./verifyWithSharedScopes")

const PREPARE_RULE_NAME = "__eslint-plugin-html-prepare"

Expand Down Expand Up @@ -162,61 +162,3 @@ function verifyExternalHtmlPlugin(config, callOriginalVerify) {
}),
]
}

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)
})
},
})
}
}
61 changes: 61 additions & 0 deletions src/verifyWithSharedScopes.js
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)
})
},
})
}
}

0 comments on commit 17f8838

Please sign in to comment.