question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document quick fix (or whatever it's officially called)

See original GitHub issue

TypeScript Version: 3.3.0-dev.201xxxxx

Search Terms: code fix quick fix suggestion

Code not entirely applicable, but this is what I had which caused me to get confused

const fs = require("fs");

Expected behavior: Commandline tsc should behave the same as editor integrations

Actual behavior: The “quick fix” feature, which appears to be called “code fixes” in the codebase and googling the messages led me to the json file which refers to them as “suggestions”, only appears to exist in the typescript server.

As a newcomer this had me extremely confused. I was running tsc on the commandline and getting no errors, but seeing squiggly lines and messages in my editor (atom, in this case).

I was unable to find any documentation about where the message 'require' call may be converted to an import was coming from, or that this was a hint that typescript was able to fix my code for me.

It would also be helpful if the tsc client was able to list the code fix suggestions, even if giving it a way to apply them would be too complex.

Playground Link: n/a

Related Issues: none that I could find

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

13reactions
ghostcommented, Mar 23, 2020

After reporting typescript-eslint/typescript-eslint#1786 I was lead here. As I do not use vscode, I was completely unaware that TypeScript has built-in autofix capabilities outside of the language server. Would be very useful to have a tsc --fix command-line option similar to eslint, as these can be run in git hooks and other places.

2reactions
Jack-Workscommented, Nov 19, 2019

For anyone who interested

import ts from 'typescript'
import { join } from 'path'
import { existsSync, readFileSync } from 'fs'

const compilerOptions: ts.CompilerOptions = {}
const host = ts.createCompilerHost(compilerOptions)
const program = ts.createProgram({
    host: host,
    rootNames: [join(__dirname, '../../my-project/src/index.ts')],
    options: compilerOptions
})
const allFiles = program.getSourceFiles().map(x => x.fileName)
const languageService = ts.createLanguageService(
    {
        getCompilationSettings: () => compilerOptions,
        getScriptFileNames: () => allFiles,
        getScriptVersion: () => '0',
        getCurrentDirectory() {
            return join(__dirname, '../../my-project/src')
        },
        getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
        fileExists: ts.sys.fileExists,
        readFile: ts.sys.readFile,
        readDirectory: ts.sys.readDirectory,
        getScriptSnapshot: fileName => {
            if (!existsSync(fileName)) {
                return undefined
            }
            return ts.ScriptSnapshot.fromString(readFileSync(fileName).toString())
        }
    },
    ts.createDocumentRegistry(true, '../../my-project/src')
)
for (const file of allFiles) {
    const diags = languageService.getSuggestionDiagnostics(file)
    for (const diag of diags) {
        console.log(
            `TS${diag.code}: ${ts.flattenDiagnosticMessageText(diag.messageText, ', ', 2)} | ${diag.file.fileName}`
        )
    }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quitclaim deeds: Your full guide | Chase.com
A deed is a legal document that officially transfers the title of a property ... Quitclaim deeds can also be called quit claim...
Read more >
Word help & learning - Microsoft Support
Get answers to all of your Microsoft Word questions. Find Word help, how-to articles, training videos, tutorials, and more.
Read more >
Affordable Connectivity Program (ACP) Error Message FAQs
An official, unexpired document that has your first name, last name and date of birth on it to resolve an error message about...
Read more >
DocuSign | #1 in Electronic Signature and Agreement Cloud
From everyday transactions to hundred-page contracts, DocuSign transforms how you prepare, sign, act on and manage agreements. Book a Meeting · What is...
Read more >
Form I-94: Travel Record (Arrival/Departure), Explained
What is the I-94 travel record form? ... The I-94 travel record, formally known as the Form I-94 Arrival/Departure Record, is a paper...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found