Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript-estree): parseWithNodeMaps returning empty maps #2773

Merged
merged 1 commit into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/typescript-estree/src/parser.ts
Expand Up @@ -331,13 +331,14 @@ function parse<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options?: T,
): AST<T> {
const { ast } = parseWithNodeMaps(code, options);
const { ast } = parseWithNodeMapsInternal(code, options, false);
return ast;
}

function parseWithNodeMaps<T extends TSESTreeOptions = TSESTreeOptions>(
function parseWithNodeMapsInternal<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options?: T,
options: T | undefined,
shouldPreserveNodeMaps: boolean,
): ParseWithNodeMapsResult<T> {
/**
* Reset the parse configuration
Expand Down Expand Up @@ -380,7 +381,7 @@ function parseWithNodeMaps<T extends TSESTreeOptions = TSESTreeOptions>(
/**
* Convert the TypeScript AST to an ESTree-compatible one
*/
const { estree, astMaps } = astConverter(ast, extra, false);
const { estree, astMaps } = astConverter(ast, extra, shouldPreserveNodeMaps);

return {
ast: estree as AST<T>,
Expand All @@ -389,6 +390,13 @@ function parseWithNodeMaps<T extends TSESTreeOptions = TSESTreeOptions>(
};
}

function parseWithNodeMaps<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options?: T,
): ParseWithNodeMapsResult<T> {
return parseWithNodeMapsInternal(code, options, true);
}

function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
code: string,
options: T,
Expand Down
23 changes: 23 additions & 0 deletions packages/typescript-estree/tests/lib/parse.test.ts
Expand Up @@ -21,6 +21,29 @@ describe('parseWithNodeMaps()', () => {
parser.parse(code),
);
});

it('should simple code', () => {
const result = parser.parseWithNodeMaps('1;');
expect(result.ast).toMatchInlineSnapshot(`
Object {
"body": Array [
Object {
"expression": Object {
"raw": "1",
"type": "Literal",
"value": 1,
},
"type": "ExpressionStatement",
},
],
"sourceType": "script",
"type": "Program",
}
`);
const tsNode = result.esTreeNodeToTSNodeMap.get(result.ast.body[0]);
expect(tsNode).toBeDefined();
expect(result.tsNodeToESTreeNodeMap.get(tsNode)).toBeDefined();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

expect(result.tsNodeToESTreeNodeMap.get(tsNode)).toBe(result.ast.body[0]);

Isn't it more safer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes - but there are other tests which test the maps (sort of). So it's "good enough" to assert that there's something there.

});
});

describe('modules', () => {
Expand Down