Skip to content

API: crash while printing ImportClause with removeComments: true #22239

@ajafff

Description

@ajafff

TypeScript Version: 2.7.2, 2.8.0-dev.20180228

Search Terms:

Code

import * as ts from 'typescript';
const sourceFile = ts.createSourceFile('import-foo.ts', "import {foo} from 'foo';", ts.ScriptTarget.ESNext);
ts.createPrinter().printFile(sourceFile); // works as expected
ts.createPrinter({removeComments: true}).printFile(sourceFile); // throws

Expected behavior:

No exception

Actual behavior:

This happens because ImportClause.name is undefined. Also note that ImportClause.namedBindings could also be undefined and there is no check for that.

TypeError: Cannot read property 'kind' of undefined
    at pipelineEmitUnspecified (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71850:29)
    at pipelineEmitWithHint (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71831:50)
    at pipelineEmitWithSourceMap (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71822:17)
    at emitNodeWithComments (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:69263:17)
    at pipelineEmitWithComments (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71811:17)
    at pipelineEmitWithNotification (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71805:17)
    at emit (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71792:13)
    at emitImportClause (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:73228:13)
    at pipelineEmitUnspecified (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:72034:28)
    at pipelineEmitWithHint (/home/klaus/code/wotan/node_modules/typescript/lib/typescript.js:71831:50)

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions