Skip to content
Permalink
Browse files

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

  • Loading branch information
bradzacher committed Nov 17, 2020
1 parent 3333040 commit 3e4a0ed0d615fd22a2f28c7c8af6179673e195f8
Showing with 35 additions and 4 deletions.
  1. +12 −4 packages/typescript-estree/src/parser.ts
  2. +23 −0 packages/typescript-estree/tests/lib/parse.test.ts
@@ -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
@@ -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>,
@@ -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,
@@ -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();
});
});

describe('modules', () => {

0 comments on commit 3e4a0ed

Please sign in to comment.