Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: BenoitZugmeyer/eslint-plugin-html
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.2
Choose a base ref
...
head repository: BenoitZugmeyer/eslint-plugin-html
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.0.3
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Feb 2, 2019

  1. fix: scope sharing should not be influenced by globalReturn: true

    The 'node' environment sets parserOptions.ecmaFeatures.globalReturn to
    'true'.  Because of this, the JS code is considered as wrapped in a
    function, so declared variables are not in the global scope.  To keep
    the configuration simple, scope sharing is only activated by
    parserOptions.sourceType = 'module', so setting globalReturn to true
    should still consider variables declared in the script body to be
    exported to the following scopes.
    Partially fixes #100
    BenoitZugmeyer committed Feb 2, 2019
    Copy the full SHA
    551b01d View commit details
  2. Copy the full SHA
    609869f View commit details
  3. 5.0.3

    BenoitZugmeyer committed Feb 2, 2019
    Copy the full SHA
    402442a View commit details
Showing with 50 additions and 13 deletions.
  1. +3 −0 CHANGELOG.md
  2. +1 −1 npm-shrinkwrap.json
  3. +1 −1 package.json
  4. +23 −2 src/__tests__/plugin.js
  5. +22 −9 src/index.js
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2019-02-02 v5.0.3
* Fix support for `parserOptions.ecmaFeatures.globalReturn: true` while sharing scope between multiple script tags

2019-02-02 v5.0.2
* Fix support for the --report-unused-disabled-directives option #111

2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-html",
"version": "5.0.2",
"version": "5.0.3",
"description": "A ESLint plugin to lint and fix inline scripts contained in HTML files.",
"license": "ISC",
"repository": {
25 changes: 23 additions & 2 deletions src/__tests__/plugin.js
Original file line number Diff line number Diff line change
@@ -596,7 +596,7 @@ it("should report correct eol-last message position", () => {
})

describe("scope sharing", () => {
it("should share the global scope between script tags", () => {
it("should export global variables between script scopes", () => {
const messages = execute("scope-sharing.html", {
rules: {
"no-console": "off",
@@ -627,7 +627,7 @@ describe("scope sharing", () => {
)
})

it("should share the global scope between script tags", () => {
it("should mark variable as used when the variable is used in another tag", () => {
const messages = execute("scope-sharing.html", {
rules: {
"no-console": "off",
@@ -658,6 +658,27 @@ describe("scope sharing", () => {
)
})

it("should not be influenced by the ECMA feature 'globalReturn'", () => {
const messages = execute("scope-sharing.html", {
rules: {
"no-console": "off",
"no-undef": "error",
"no-unused-vars": "error",
},
globals: {
console: false,
},
env: { es6: true },
parserOptions: {
ecmaFeatures: {
globalReturn: true,
},
},
})

expect(messages.length).toBe(8)
})

it("should not share the global scope if sourceType is 'module'", () => {
const messages = execute("scope-sharing.html", {
rules: {
31 changes: 22 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -197,12 +197,13 @@ function patch(Linter) {
)
}

if (config.parserOptions && config.parserOptions.sourceType === "module") {
const parserOptions = config.parserOptions || {}
if (parserOptions.sourceType === "module") {
for (const codePart of extractResult.code) {
verifyCodePart(codePart)
}
} else {
verifyWithSharedScopes(extractResult.code, verifyCodePart)
verifyWithSharedScopes(extractResult.code, verifyCodePart, parserOptions)
}

messages.sort((ma, mb) => ma.line - mb.line || ma.column - mb.column)
@@ -211,21 +212,33 @@ function patch(Linter) {
}
}

function verifyWithSharedScopes(codeParts, verifyCodePart) {
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: context
.getScope()
.through.map(node => node.identifier.name),
declaredGlobals: context
.getScope()
.variables.map(variable => variable.name),
exportedGlobals: globalScope.through.map(
node => node.identifier.name
),
declaredGlobals: scopeForDeclaredGlobals.variables.map(
variable => variable.name
),
})
},
ignoreRules: true,