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

How can I write tslint rule for check await to async functions #6273

Closed
cevek opened this issue Dec 28, 2015 · 3 comments
Closed

How can I write tslint rule for check await to async functions #6273

cevek opened this issue Dec 28, 2015 · 3 comments
Labels
API Relates to the public API for TypeScript External Relates to another program, environment, or user action which we cannot control. Question An issue which isn't directly actionable in code

Comments

@cevek
Copy link

cevek commented Dec 28, 2015

foo.ts

export async function foo(){}

bar.ts

import {foo} from './foo';
async function bar(){
   foo(); // I need lint warning that I forgot await keyword
}

I've tried write some code, but I don't understand how works typescript typechecker

export class Rule extends Lint.Rules.AbstractRule {
    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        var options: ts.CompilerOptions = { target: ts.ScriptTarget.ES6 };
        var program = ts.createProgram([sourceFile.fileName], options);
        var typeChecker = program.getTypeChecker();
        return this.applyWithWalker(<Lint.RuleWalker>(new NoManyWalker(sourceFile, this.getOptions(), typeChecker)));
    }
}

class NoManyWalker extends Lint.RuleWalker {
    constructor(sourceFile: ts.SourceFile, options: Lint.IOptions, private typeChecker: ts.TypeChecker){
      super(sourceFile, options);
    }
    public visitCallExpression(node: ts.Node) {
        console.log("visitCallExpression");
        var type = this.typeChecker.getResolvedSignature(<any>node);
        var isAsync = Lint.isNodeFlagSet(type.declaration, ts.NodeFlags.Async);
        console.log(isAsync);
        super.visitCallExpression(node);
    }
}

It works for local async functions, but for imported functions I have a ts error:

var moduleExports = getSymbolOfNode(location).exports;
TypeError: Cannot read property 'exports' of undefined
@DanielRosenwasser
Copy link
Member

I'm vaguely familiar with the issue, but my understanding is that tslint doesn't do cross-module checking, which means that you won't have full semantic information about your program.

@weswigham is familiar with this issue. As a heads up these questions are probably more appropriate on the tslint repo itself.

(Also, you shouldn't need to assert any type to any for our API.)

@DanielRosenwasser
Copy link
Member

See palantir/tslint#680 and palantir/tslint#692.

@DanielRosenwasser DanielRosenwasser added External Relates to another program, environment, or user action which we cannot control. Question An issue which isn't directly actionable in code API Relates to the public API for TypeScript labels Dec 28, 2015
@adidahiya
Copy link
Contributor

see palantir/tslint#892

@DanielRosenwasser is correct about lacking full semantic information about the program to be able to write this rule right now. @jkillian explains more here

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript External Relates to another program, environment, or user action which we cannot control. Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants