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

The source string is discarded when using the program argument. #3797

Closed
AlexanderOMara opened this issue Mar 29, 2018 · 4 comments
Closed

Comments

@AlexanderOMara
Copy link

AlexanderOMara commented Mar 29, 2018

Bug Report

  • TSLint version: 5.9.1
  • TypeScript version: 2.7.2
  • Running TSLint via: Node.js API

I don't know if this is the intended behavior as it's not really documented, but using the Node.js API with the program argument like this:

var program = tslint.Linter.createProgram(__dirname + '/tsconfig.json', __dirname);
var linter = new tslint.Linter(options, program);
linter.lint(fileName, fileContents, configuration);

Discards the source string argument:

    public lint(fileName: string, source: string, configuration: IConfigurationFile = DEFAULT_CONFIG): void {
        const sourceFile = this.getSourceFile(fileName, source);
    // ...

(source)

    private getSourceFile(fileName: string, source: string) {
        if (this.program !== undefined) {
            const sourceFile = this.program.getSourceFile(fileName);
            if (sourceFile === undefined) {
                const INVALID_SOURCE_ERROR = dedent`
                    Invalid source file: ${fileName}. Ensure that the files supplied to lint have a .ts, .tsx, .d.ts, .js or .jsx extension.
                `;
                throw new FatalError(INVALID_SOURCE_ERROR);
            }
            return sourceFile;
        } else {
            return utils.getSourceFile(fileName, source);
        }
    }

(source)

This means the code is read from disk and cannot be supplied dynamically, and linters like Atom's linter-tslint are not as useful as they could be because they cannot lint the file while the user types.

As an experiment, I tried editing the compiled JavaScript to make the SourceFileObject use the properties utils.getSourceFile creates, and that made the API work (although the CLI didn't quite work, it seems changing statements property was the problem).

    Linter.prototype.getSourceFile = function (fileName, source) {
        if (this.program !== undefined) {
            var sourceFile = this.program.getSourceFile(fileName);
            if (sourceFile === undefined) {
                var INVALID_SOURCE_ERROR = utils_1.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n                    Invalid source file: ", ". Ensure that the files supplied to lint have a .ts, .tsx, .d.ts, .js or .jsx extension.\n                "], ["\n                    Invalid source file: ", ". Ensure that the files supplied to lint have a .ts, .tsx, .d.ts, .js or .jsx extension.\n                "])), fileName);
                throw new error_1.FatalError(INVALID_SOURCE_ERROR);
            }

            // ADDED CODE:
            const other = utils.getSourceFile(fileName, source);
            Object.keys(other).forEach(function(p) {
                sourceFile[p] = other[p];
            });

            return sourceFile;
        }
        else {
            return utils.getSourceFile(fileName, source);
        }
    };

Any thoughts on this issue, or suggestions on a good way to solve it?

@phyllisstein
Copy link

This seems like a solid investigation of a pretty annoying glitch. Any insight from the maintainers on how it could be resolved?

@JoshuaKGoldberg
Copy link
Contributor

solid investigation

Solid investigation and solid writeup, thanks for this!

IMO Linter and Runner have grown a bit out of hand and we could probably do with swapping into revamped versions of them. The current Linter would have to be kept around for legacy API compatibility, but I don't see a clean way around fixing the existing code in-place.

seems changing statements property was the problem

Sorry, which statements property are you referring to?

Tangentially related: #3677.

@JoshuaKGoldberg
Copy link
Contributor

💀 It's time! 💀

TSLint is being deprecated and no longer accepting pull requests for major new changes or features. See #4534. 😱

If you'd like to see this change implemented, you have two choices:

  • Recommended: Check if this is available in ESLint + typescript-eslint
  • Not Recommended: Fork TSLint locally 🤷‍♂️

👋 It was a pleasure open sourcing with you!

If you believe this message was posted here in error, please comment so we can re-open the issue!

@JoshuaKGoldberg
Copy link
Contributor

🤖 Beep boop! 👉 TSLint is deprecated 👈 (#4534) and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

@palantir palantir locked and limited conversation to collaborators Mar 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants