-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-await-expression-member
rule (#1586)
- Loading branch information
Showing
11 changed files
with
478 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Forbid member access from await expression | ||
|
||
When accessing a member from an await expression, the await expression has to be parenthesized, which is not readable. | ||
|
||
This rule is fixable for simple member access. | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = (await import('./foo.js')).default; | ||
``` | ||
|
||
```js | ||
const secondElement = (await getArray())[1]; | ||
``` | ||
|
||
```js | ||
const property = (await getObject()).property; | ||
``` | ||
|
||
```js | ||
const data = await (await fetch('/foo')).json(); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const {default: foo} = await import('./foo.js'); | ||
``` | ||
|
||
```js | ||
const [, secondElement] = await getArray(); | ||
``` | ||
|
||
```js | ||
const {property} = await getObject(); | ||
``` | ||
|
||
```js | ||
const response = await fetch('/foo'); | ||
const data = await response.json(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
const {getParentheses} = require('../utils/parentheses.js'); | ||
|
||
function * removeParentheses(node, fixer, sourceCode) { | ||
const parentheses = getParentheses(node, sourceCode); | ||
for (const token of parentheses) { | ||
yield fixer.remove(token); | ||
} | ||
} | ||
|
||
module.exports = removeParentheses; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
const { | ||
removeParentheses, | ||
removeMemberExpressionProperty, | ||
} = require('./fix/index.js'); | ||
|
||
const MESSAGE_ID = 'no-await-expression-member'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Do not access a member directly from an await expression.', | ||
}; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return { | ||
'MemberExpression[object.type="AwaitExpression"]'(memberExpression) { | ||
const {property} = memberExpression; | ||
const problem = { | ||
node: property, | ||
messageId: MESSAGE_ID, | ||
}; | ||
|
||
// `const foo = (await bar)[0]` | ||
if ( | ||
memberExpression.computed | ||
&& !memberExpression.optional | ||
&& property.type === 'Literal' | ||
&& (property.value === 0 || property.value === 1) | ||
&& memberExpression.parent.type === 'VariableDeclarator' | ||
&& memberExpression.parent.init === memberExpression | ||
&& memberExpression.parent.id.type === 'Identifier' | ||
) { | ||
problem.fix = function * (fixer) { | ||
const variable = memberExpression.parent.id; | ||
yield fixer.insertTextBefore(variable, property.value === 0 ? '[' : '[, '); | ||
yield fixer.insertTextAfter(variable, ']'); | ||
|
||
yield removeMemberExpressionProperty(fixer, memberExpression, sourceCode); | ||
yield * removeParentheses(memberExpression.object, fixer, sourceCode); | ||
}; | ||
|
||
return problem; | ||
} | ||
|
||
// `const foo = (await bar).foo` | ||
if ( | ||
!memberExpression.computed | ||
&& !memberExpression.optional | ||
&& property.type === 'Identifier' | ||
&& memberExpression.parent.type === 'VariableDeclarator' | ||
&& memberExpression.parent.init === memberExpression | ||
&& memberExpression.parent.id.type === 'Identifier' | ||
&& memberExpression.parent.id.name === property.name | ||
) { | ||
problem.fix = function * (fixer) { | ||
const variable = memberExpression.parent.id; | ||
yield fixer.insertTextBefore(variable, '{'); | ||
yield fixer.insertTextAfter(variable, '}'); | ||
|
||
yield removeMemberExpressionProperty(fixer, memberExpression, sourceCode); | ||
yield * removeParentheses(memberExpression.object, fixer, sourceCode); | ||
}; | ||
|
||
return problem; | ||
} | ||
|
||
return problem; | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Forbid member access from await expression.', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'const foo = await promise', | ||
'const {foo: bar} = await promise', | ||
'const foo = !await promise', | ||
'const foo = typeof await promise', | ||
'const foo = await notPromise.method()', | ||
'const foo = foo[await promise]', | ||
|
||
// These await expression need parenthesized, but rarely used | ||
'new (await promiseReturnsAClass)', | ||
'(await promiseReturnsAFunction)()', | ||
], | ||
invalid: [ | ||
'(await promise)[0]', | ||
'(await promise).property', | ||
'const foo = (await promise).bar()', | ||
'const foo = (await promise).bar?.()', | ||
'const foo = (await promise)?.bar()', | ||
|
||
'const firstElement = (await getArray())[0]', | ||
'const secondElement = (await getArray())[1]', | ||
'const thirdElement = (await getArray())[2]', | ||
'const optionalFirstElement = (await getArray())?.[0]', | ||
'const {propertyOfFirstElement} = (await getArray())[0]', | ||
'const [firstElementOfFirstElement] = (await getArray())[0]', | ||
'let foo, firstElement = (await getArray())[0]', | ||
'var firstElement = (await getArray())[0], bar', | ||
|
||
'const property = (await getObject()).property', | ||
'const renamed = (await getObject()).property', | ||
'const property = (await getObject())[property]', | ||
'const property = (await getObject())?.property', | ||
'const {propertyOfProperty} = (await getObject()).property', | ||
'const {propertyOfProperty} = (await getObject()).propertyOfProperty', | ||
'const [firstElementOfProperty] = (await getObject()).property', | ||
'const [firstElementOfProperty] = (await getObject()).firstElementOfProperty', | ||
|
||
'firstElement = (await getArray())[0]', | ||
'property = (await getArray()).property', | ||
], | ||
}); | ||
|
||
test.typescript({ | ||
valid: [ | ||
'function foo () {return (await promise) as string;}', | ||
], | ||
invalid: [], | ||
}); |
Oops, something went wrong.