Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [promise-function-async] bad fixer with computed and literal methods #3163

Merged
merged 3 commits into from Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/eslint-plugin/src/rules/promise-function-async.ts
@@ -1,7 +1,9 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { last } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd rather avoid adding a dependency on lodash for such trivial things.
Please inline the function impl!

import * as ts from 'typescript';
import * as util from '../util';

Expand Down Expand Up @@ -156,8 +158,39 @@ export default util.createRule<Options, MessageIds>({
(node.parent.type === AST_NODE_TYPES.Property &&
node.parent.method))
) {
return fixer.insertTextBefore(node.parent.key, 'async ');
// this function is a class method or object function property shorthand
const method = node.parent;

/** the token to put `async` before */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick

Suggested change
/** the token to put `async` before */
/// the token to put `async` before

let keyToken = sourceCode.getFirstToken(method)!;

// if there are decorators then skip past them
if (
method.type === AST_NODE_TYPES.MethodDefinition &&
method.decorators
) {
const lastDecorator = last(method.decorators)!;
keyToken = sourceCode.getTokenAfter(lastDecorator)!;
}

// if current token is a keyword like `static` or `public` then skip it
while (keyToken.type === AST_TOKEN_TYPES.Keyword) {
keyToken = sourceCode.getTokenAfter(keyToken)!;
}

// check if there is a space between key and previous token
const insertSpace = !sourceCode.isSpaceBetween!(
sourceCode.getTokenBefore(keyToken)!,
keyToken,
);

let code = 'async ';
if (insertSpace) {
code = ` ${code}`;
}
return fixer.insertTextBefore(keyToken, code);
}

return fixer.insertTextBefore(node, 'async ');
},
});
Expand Down
36 changes: 36 additions & 0 deletions packages/eslint-plugin/tests/rules/promise-function-async.test.ts
@@ -1,3 +1,4 @@
import { noFormat } from '@typescript-eslint/experimental-utils/src/eslint-utils';
import rule from '../../src/rules/promise-function-async';
import { getFixturesRootDir, RuleTester } from '../RuleTester';

Expand Down Expand Up @@ -630,6 +631,41 @@ class Test {
public async test() {
return Promise.resolve(123);
}
}
`,
},
{
code: noFormat`
class Test {
@decorator(async () => {})
static protected[(1)]() {
return Promise.resolve(1);
}
public'bar'() {
return Promise.resolve(2);
}
private['baz']() {
return Promise.resolve(3);
}
}
`,
errors: [
{ line: 4, column: 3, messageId },
{ line: 7, column: 3, messageId },
{ line: 10, column: 3, messageId },
],
output: noFormat`
class Test {
@decorator(async () => {})
static protected async [(1)]() {
return Promise.resolve(1);
}
public async 'bar'() {
return Promise.resolve(2);
}
private async ['baz']() {
return Promise.resolve(3);
}
}
`,
},
Expand Down