Skip to content

Commit

Permalink
Breaking: change behavior for eslint@>=4.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Oct 28, 2017
1 parent 28327dc commit 54d5fa0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
53 changes: 18 additions & 35 deletions lib/disabled-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,15 @@ module.exports = class DisabledArea {
* @param {Token} comment - The comment token to disable.
* @param {object} location - The start location to disable.
* @param {string[]|null} ruleIds - The ruleId names to disable.
* @param {string} kind - The kind of disable-comments to show in reports.
* @param {string} kind - The kind of disable-comments.
* @returns {void}
* @private
*/
_disable(comment, location, ruleIds, kind) {
if (ruleIds) {
for (const ruleId of ruleIds) {
if (this._getArea(ruleId, location) != null) {
this.duplicateDisableDirectives.push({
comment,
ruleId,
kind,
})
this.duplicateDisableDirectives.push({ comment, ruleId })
}

this.areas.push({
Expand All @@ -84,11 +80,7 @@ module.exports = class DisabledArea {
}
else {
if (this._getArea(null, location) != null) {
this.duplicateDisableDirectives.push({
comment,
ruleId: null,
kind,
})
this.duplicateDisableDirectives.push({ comment, ruleId: null })
}

this.areas.push({
Expand All @@ -108,21 +100,21 @@ module.exports = class DisabledArea {
* @param {Token} comment - The comment token to enable.
* @param {object} location - The start location to enable.
* @param {string[]|null} ruleIds - The ruleId names to enable.
* @param {string} kind - The kind of disable-comments.
* @returns {void}
* @private
*/
_enable(comment, location, ruleIds) {
_enable(comment, location, ruleIds, kind) {
if (ruleIds) {
for (const ruleId of ruleIds) {
let used = false

for (let i = this.areas.length - 1; i >= 0; --i) {
const area = this.areas[i]

if (area.end === null && area.ruleId === ruleId) {
if (area.end === null && area.kind === kind && area.ruleId === ruleId) {
area.end = location
used = true
break
}
}

Expand All @@ -132,21 +124,18 @@ module.exports = class DisabledArea {
}
}
else {
let start = null
let used = false

for (let i = this.areas.length - 1; i >= 0; --i) {
const area = this.areas[i]

if (start !== null && area.start !== start) {
break
}
if (area.end === null) {
if (area.end === null && area.kind === kind) {
area.end = location
start = area.start
used = true
}
}

if (start === null) {
if (!used) {
this.unusedEnableDirectives.push({ comment, ruleId: null })
}
}
Expand Down Expand Up @@ -192,32 +181,26 @@ module.exports = class DisabledArea {
const ruleIds = m[2] ? m[2].split(DELIMITER) : null

if (comment.type === "Block" && kind === "eslint-disable") {
this._disable(comment, comment.loc.start, ruleIds, kind)
this._disable(comment, comment.loc.start, ruleIds, "block")
}
else if (comment.type === "Block" && kind === "eslint-enable") {
this._enable(comment, comment.loc.start, ruleIds)
this._enable(comment, comment.loc.start, ruleIds, "block")
}
else if (
comment.type === "Line" &&
kind === "eslint-disable-line"
) {
else if (comment.type === "Line" && kind === "eslint-disable-line") {
const line = comment.loc.start.line
const start = { line, column: 0 }
const end = { line: line + 1, column: -1 }

this._disable(comment, start, ruleIds, kind)
this._enable(comment, end, ruleIds)
this._disable(comment, start, ruleIds, "line")
this._enable(comment, end, ruleIds, "line")
}
else if (
comment.type === "Line" &&
kind === "eslint-disable-next-line"
) {
else if (comment.type === "Line" && kind === "eslint-disable-next-line") {
const line = comment.loc.start.line
const start = { line: line + 1, column: 0 }
const end = { line: line + 2, column: -1 }

this._disable(comment, start, ruleIds, kind)
this._enable(comment, end, ruleIds)
this._disable(comment, start, ruleIds, "line")
this._enable(comment, end, ruleIds, "line")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/rules/disable-enable-pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ console.log('This code does not even have any special comments')
code: `
/*eslint-disable no-undef*/
/*eslint-disable no-unused-vars*/
/*eslint-enable*/
/*eslint-enable no-unused-vars*/
`,
errors: [
{
Expand Down

0 comments on commit 54d5fa0

Please sign in to comment.