eslint-plugin-jest
Advanced tools
Comparing version 22.0.0 to 22.0.1
@@ -6,3 +6,3 @@ # Disallow Jasmine globals | ||
environment. Most functionality offered by Jasmine has been ported to Jest, and | ||
the Jasmine globals will stop working in the future. Developers should therefor | ||
the Jasmine globals will stop working in the future. Developers should therefore | ||
migrate to Jest's documented API instead of relying on the undocumented Jasmine | ||
@@ -9,0 +9,0 @@ API. |
{ | ||
"name": "eslint-plugin-jest", | ||
"version": "22.0.0", | ||
"version": "22.0.1", | ||
"description": "Eslint rules for Jest", | ||
@@ -5,0 +5,0 @@ "repository": "jest-community/eslint-plugin-jest", |
@@ -18,2 +18,5 @@ 'use strict'; | ||
`require('foo')('bar')`, | ||
'function callback(fail) { fail() }', | ||
'var spyOn = require("actions"); spyOn("foo")', | ||
'function callback(pending) { pending() }', | ||
], | ||
@@ -20,0 +23,0 @@ invalid: [ |
'use strict'; | ||
const { getDocsUrl, getNodeName } = require('./util'); | ||
const { getDocsUrl, getNodeName, scopeHasLocalReference } = require('./util'); | ||
function collectReferences(scope) { | ||
const locals = new Set(); | ||
const unresolved = new Set(); | ||
let currentScope = scope; | ||
while (currentScope !== null) { | ||
for (const ref of currentScope.variables) { | ||
const isReferenceDefined = ref.defs.some(def => { | ||
return def.type !== 'ImplicitGlobalVariable'; | ||
}); | ||
if (isReferenceDefined) { | ||
locals.add(ref.name); | ||
} | ||
} | ||
for (const ref of currentScope.through) { | ||
unresolved.add(ref.identifier.name); | ||
} | ||
currentScope = currentScope.upper; | ||
} | ||
return { locals, unresolved }; | ||
} | ||
module.exports = { | ||
@@ -70,11 +43,3 @@ meta: { | ||
'CallExpression[callee.name="pending"]'(node) { | ||
const references = collectReferences(context.getScope()); | ||
if ( | ||
// `pending` was found as a local variable or function declaration. | ||
references.locals.has('pending') || | ||
// `pending` was not found as an unresolved reference, | ||
// meaning it is likely not an implicit global reference. | ||
!references.unresolved.has('pending') | ||
) { | ||
if (scopeHasLocalReference(context.getScope(), 'pending')) { | ||
return; | ||
@@ -81,0 +46,0 @@ } |
'use strict'; | ||
const { getDocsUrl, getNodeName } = require('./util'); | ||
const { getDocsUrl, getNodeName, scopeHasLocalReference } = require('./util'); | ||
@@ -11,2 +11,13 @@ module.exports = { | ||
fixable: 'code', | ||
messages: { | ||
illegalGlobal: | ||
'Illegal usage of global `{{ global }}`, prefer `{{ replacement }}`', | ||
illegalMethod: | ||
'Illegal usage of `{{ method }}`, prefer `{{ replacement }}`', | ||
illegalFail: | ||
'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback', | ||
illegalPending: | ||
'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`', | ||
illegalJasmine: 'Illegal usage of jasmine global', | ||
}, | ||
}, | ||
@@ -21,29 +32,38 @@ create(context) { | ||
} | ||
if ( | ||
calleeName === 'spyOn' || | ||
calleeName === 'spyOnProperty' || | ||
calleeName === 'fail' || | ||
calleeName === 'pending' | ||
) { | ||
if (scopeHasLocalReference(context.getScope(), calleeName)) { | ||
// It's a local variable, not a jasmine global. | ||
return; | ||
} | ||
if (calleeName === 'spyOn' || calleeName === 'spyOnProperty') { | ||
context.report({ | ||
node, | ||
message: `Illegal usage of global \`${calleeName}\`, prefer \`jest.spyOn\``, | ||
}); | ||
switch (calleeName) { | ||
case 'spyOn': | ||
case 'spyOnProperty': | ||
context.report({ | ||
node, | ||
messageId: 'illegalGlobal', | ||
data: { global: calleeName, replacement: 'jest.spyOn' }, | ||
}); | ||
break; | ||
case 'fail': | ||
context.report({ | ||
node, | ||
messageId: 'illegalFail', | ||
}); | ||
break; | ||
case 'pending': | ||
context.report({ | ||
node, | ||
messageId: 'illegalPending', | ||
}); | ||
break; | ||
} | ||
return; | ||
} | ||
if (calleeName === 'fail') { | ||
context.report({ | ||
node, | ||
message: | ||
'Illegal usage of `fail`, prefer throwing an error, or the `done.fail` callback', | ||
}); | ||
return; | ||
} | ||
if (calleeName === 'pending') { | ||
context.report({ | ||
node, | ||
message: | ||
'Illegal usage of `pending`, prefer explicitly skipping a test using `test.skip`', | ||
}); | ||
return; | ||
} | ||
if (calleeName.startsWith('jasmine.')) { | ||
@@ -64,3 +84,7 @@ const functionName = calleeName.replace('jasmine.', ''); | ||
node, | ||
message: `Illegal usage of \`${calleeName}\`, prefer \`expect.${functionName}\``, | ||
messageId: 'illegalMethod', | ||
data: { | ||
method: calleeName, | ||
replacement: `expect.${functionName}`, | ||
}, | ||
}); | ||
@@ -73,3 +97,7 @@ return; | ||
node, | ||
message: `Illegal usage of \`${calleeName}\`, prefer \`expect.extend\``, | ||
messageId: 'illegalMethod', | ||
data: { | ||
method: calleeName, | ||
replacement: `expect.extend`, | ||
}, | ||
}); | ||
@@ -82,3 +110,7 @@ return; | ||
node, | ||
message: `Illegal usage of \`${calleeName}\`, prefer \`jest.fn\``, | ||
messageId: 'illegalMethod', | ||
data: { | ||
method: calleeName, | ||
replacement: 'jest.fn', | ||
}, | ||
}); | ||
@@ -90,3 +122,3 @@ return; | ||
node, | ||
message: 'Illegal usage of jasmine global', | ||
messageId: 'illegalJasmine', | ||
}); | ||
@@ -93,0 +125,0 @@ } |
@@ -145,2 +145,40 @@ 'use strict'; | ||
const collectReferences = scope => { | ||
const locals = new Set(); | ||
const unresolved = new Set(); | ||
let currentScope = scope; | ||
while (currentScope !== null) { | ||
for (const ref of currentScope.variables) { | ||
const isReferenceDefined = ref.defs.some(def => { | ||
return def.type !== 'ImplicitGlobalVariable'; | ||
}); | ||
if (isReferenceDefined) { | ||
locals.add(ref.name); | ||
} | ||
} | ||
for (const ref of currentScope.through) { | ||
unresolved.add(ref.identifier.name); | ||
} | ||
currentScope = currentScope.upper; | ||
} | ||
return { locals, unresolved }; | ||
}; | ||
const scopeHasLocalReference = (scope, referenceName) => { | ||
const references = collectReferences(scope); | ||
return ( | ||
// referenceName was found as a local variable or function declaration. | ||
references.locals.has(referenceName) || | ||
// referenceName was not found as an unresolved reference, | ||
// meaning it is likely not an implicit global reference. | ||
!references.unresolved.has(referenceName) | ||
); | ||
}; | ||
module.exports = { | ||
@@ -164,2 +202,3 @@ method, | ||
getDocsUrl, | ||
scopeHasLocalReference, | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
188670
5411