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.

getDiagnostics throws when the filename does not have an extension

See original GitHub issue

Simple:

const project = new Project();
const file = project.createSourceFile("no_extension", "const a: number = 0 as any as number");

console.log(file.getDiagnostics());

For stack trace check the details of the original issue below.

Original issue Taking a stab at the silly #357, I'm getting an error.

Idea

function toString (type: string | Type): string {
  if (typeof type == 'string') {
    return type
  } else {
    return type.getText()
  }
}

export function isAssignable (project: Project, dst: string | Type, src: string | Type): boolean {
  const dstType = toString(dst)
  const srcType = toString(src)

  Error.stackTraceLimit = Infinity

  const content = `const a: ${dstType} = 0 as any as ${srcType}`
  const file = project.createSourceFile(FILENAME, content)
  console.log(content) // => const a: number = 0 as any as number
  const diagnostics = file.getDiagnostics()

  console.log(diagnostics)
}

Test

  describe(`simple cases`, () => {
    const project = new Project()
    const klass = loadClassFromFile('01.ts')('TestClass01', project)

    describe(`using types`, () => {
      fit(`says that "number" is assignable to "number"`, () => {
        const dst = klass.getPropertyOrThrow('p1').getType()
        const src = klass.getPropertyOrThrow('p1').getType()
        expect(isAssignable(project, dst, src)).toBe(true)
      })

Loaded file

class TestClass01 {

  p1: number = 1
  p2: string = '2'
  p3: number | string = 3
  p4: number | null = 4

Stack trace

1) isAssignable simple cases using types says that "number" is assignable to "number"
  Message:
    TypeError: Cannot read property 'flags' of undefined
  Stack:
        at <Jasmine>
        at Object.getCheckFlags (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:11719:23)
        at getTypeOfSymbol (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:29638:20)
        at checkVariableLikeDeclaration (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:45723:41)
        at checkVariableDeclaration (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:45789:20)
        at checkSourceElement (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47748:28)
        at Object.forEach (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:1762:30)
        at checkVariableStatement (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:45799:16)
        at checkSourceElement (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47717:28)
        at Object.forEach (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:1762:30)
        at checkSourceFileWorker (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47889:20)
        at checkSourceFile (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47868:13)
        at getDiagnosticsWorker (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47933:17)
        at Object.getDiagnostics (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:47919:24)
        at /home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76361:85
        at runWithCancellationToken (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76324:24)
        at getSemanticDiagnosticsForFileNoCache (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76347:20)
        at getAndCacheDiagnostics (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76583:26)
        at getSemanticDiagnosticsForFile (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76344:20)
        at getDiagnosticsHelper (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76286:24)
        at Object.getSemanticDiagnostics (/home/lazar/wane/node_modules/ts-simple-ast/node_modules/typescript/lib/typescript.js:76299:20)
        at Program.getSemanticDiagnostics (/home/lazar/wane/node_modules/ts-simple-ast/dist/compiler/tools/Program.js:76:55)
        at SourceFile.getDiagnostics (/home/lazar/wane/node_modules/ts-simple-ast/dist/compiler/file/SourceFile.js:788:104)
        at Object.isAssignable (/home/lazar/wane/src/compiler/analyzer/utils/type-checker.ts:41:28)
        at UserContext.fit (/home/lazar/wane/src/compiler/analyzer/utils/tests/type-checker.spec.ts:15:16)
        at <Jasmine>
        at runCallback (timers.js:763:18)
        at tryOnImmediate (timers.js:734:5)
        at processImmediate (timers.js:716:5)

Sorry for vague description, I’ll create a repro later if you need it.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Jul 10, 2018

Hi @lazarljubenovic,

There’s definitely something going on here, but I wasn’t able to reproduce when trying this:

const project = new Project();
const file = project.createSourceFile("test.ts", "const a: number = 0 as any as number");

console.log(file.getDiagnostics());

Can you see something I’m missing in that? Maybe it has to do with the way the project is setup.

0reactions
dsherretcommented, Jul 12, 2018

I’m not 100% clear in pure TypeScript compiler API

Me neither! 😄 I feel like I’m probably not doing something conventional here, but I’ll look into it more later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filename does not have extension - Microsoft Community
I use Quick Parts to insert the filename into the footer of my Word 16 documents. This is built into my normal template....
Read more >
VS Code API | Visual Studio Code Extension API
VS Code API. VS Code API is a set of JavaScript APIs that you can invoke in your Visual Studio Code extension. This...
Read more >
How to get the filename without the extension in Java?
String fileName = file.getName(); int pos = fileName.lastIndexOf("."); if (pos > 0 && pos < (fileName.length() - 1)) { // If '.' is...
Read more >
FilenameUtils (Apache Commons IO 2.11.0 API)
Checks whether the extension of the fileName is one of those specified. ... If the double dot has no parent path segment to...
Read more >
std::filesystem::path::extension - cppreference.com
If the pathname is either . or .. , or if filename() does not contain the '.' character, then empty path is returned....
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