Skip to content

Commit

Permalink
Merge pull request #266 from lo1tuma/gh-265
Browse files Browse the repository at this point in the history
Fix prefer-arrow-callback to not fail when using import.meta
  • Loading branch information
lo1tuma authored Aug 25, 2020
2 parents 08f399a + efafb46 commit 1ba854e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function isFunctionName(variable) {
return variable && variable.defs[0].type === 'FunctionName';
}

/**
* Checks whether or not a given MetaProperty node equals to a given value.
* @param {ASTNode} node - A MetaProperty node to check.
* @param {string} metaName - The name of `MetaProperty.meta`.
* @param {string} propertyName - The name of `MetaProperty.property`.
* @returns {boolean} `true` if the node is the specific value.
*/
function checkMetaProperty(node, metaName, propertyName) {
return node.meta.name === metaName && node.property.name === propertyName;
}

/**
* Gets the variable object of `arguments` which is defined implicitly.
* @param {eslint-scope.Scope} scope - A scope to get.
Expand Down Expand Up @@ -228,10 +239,12 @@ module.exports = {
}
},

MetaProperty() {
MetaProperty(node) {
const info = stack[stack.length - 1];

info.meta = true;
if (info && checkMetaProperty(node, 'new', 'target')) {
info.meta = true;
}
},

// To skip nested scopes.
Expand Down
7 changes: 7 additions & 0 deletions test/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ ruleTester.run('prefer-arrow-callback', rules['prefer-arrow-callback'], {
'foo(function bar() { new.target; });',
'foo(function bar() { new.target; }.bind(this));',
'foo(function bar() { this; }.bind(this, somethingElse));',
{
code: 'import.meta.url',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
},
// mocha-specific valid test cases
'before(function bar() {});',
'after(function bar() {});',
Expand Down

0 comments on commit 1ba854e

Please sign in to comment.