Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

sourceFile duplication for TypedRule #1969

Closed
andy-hanson opened this issue Jan 2, 2017 · 4 comments · Fixed by #2235
Closed

sourceFile duplication for TypedRule #1969

andy-hanson opened this issue Jan 2, 2017 · 4 comments · Fixed by #2235

Comments

@andy-hanson
Copy link
Contributor

andy-hanson commented Jan 2, 2017

Bug Report

  • TSLint version: master
  • TypeScript version: 2.1.4 (the one in package.json)
  • Running TSLint via: ./bin/tslint --test test/rules/my-rule

TypeScript code being linted

export class Rule extends Lint.Rules.TypedRule {
    /* tslint:disable:object-literal-sort-keys */
    public static metadata: Lint.IRuleMetadata = {
        ruleName: "my-rule",
        description: "",
        optionsDescription: "Not configurable.",
        options: null,
        optionExamples: ["true"],
        type: "functionality",
        typescriptOnly: false,
    };
    /* tslint:enable:object-literal-sort-keys */

    public applyWithProgram(sourceFile: ts.SourceFile, langSvc: ts.LanguageService): Lint.RuleFailure[] {
        console.log(sourceFile === langSvc.getProgram().getSourceFile(sourceFile.fileName));
        return [];
    }
}

Actual behavior

Prints false.

Expected behavior

Prints true. The source file used for linting should be the same as the one used for the program.
I want to write a rule that uses the type checker to get information about nodes as I recurse. I can't do that if it uses a different AST.

@nchen63
Copy link
Contributor

nchen63 commented Jan 3, 2017

Is the AST different or are the sourceFile objects different references?

@andy-hanson
Copy link
Contributor Author

Looks like they're completely separate. Added this to the top of applyWithProgram in noForInArrayRule.ts:

const langSvcSourceFile = langSvc.getProgram().getSourceFile(sourceFile.fileName);
console.log(sourceFile === langSvcSourceFile);
console.log(sourceFile.statements[0] === langSvcSourceFile.statements[0]);
process.exit(0);

Both lines print false.

@nchen63
Copy link
Contributor

nchen63 commented Jan 7, 2017

But the getText() and individual offsets would all be the same right? The === is a reference comparison so I don't think you can use that for these objects that are created at different times

@ghost
Copy link

ghost commented Jan 7, 2017

Since it's the same file being parsed twice, the two separate source files should have identical contents.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants