Skip to content

Commit

Permalink
fix: fix the ts-estree tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Jul 12, 2019
1 parent 3f5a6cd commit f2af20c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
4 changes: 3 additions & 1 deletion packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ export function parseAndGenerateServices<
* Convert the TypeScript AST to an ESTree-compatible one, and optionally preserve
* mappings between converted and original AST nodes
*/
const { estree, astMaps } = convert(ast, extra, true);
const preserveNodeMaps =
typeof extra.preserveNodeMaps === 'boolean' ? extra.preserveNodeMaps : true;
const { estree, astMaps } = convert(ast, extra, preserveNodeMaps);
/**
* Even if TypeScript parsed the source code ok, and we had no problems converting the AST,
* there may be other syntactic or semantic issues in the code that we can optionally report on.
Expand Down
81 changes: 41 additions & 40 deletions packages/typescript-estree/tests/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('parse()', () => {
tokens: expect.any(Array),
tsconfigRootDir: expect.any(String),
useJSXTextNode: false,
preserveNodeMaps: false,
preserveNodeMaps: true,
},
false,
);
Expand Down Expand Up @@ -159,72 +159,73 @@ describe('parse()', () => {
);
});

it('should not preserve node maps by default for parseAndGenerateServices(), unless `project` is set', () => {
it('should preserve node maps by default for parseAndGenerateServices()', () => {
const noOptionSet = parser.parseAndGenerateServices(code, baseConfig);

expect(noOptionSet.services.esTreeNodeToTSNodeMap).toBeUndefined();
expect(noOptionSet.services.tsNodeToESTreeNodeMap).toBeUndefined();

const withProjectNoOptionSet = parser.parseAndGenerateServices(code, {
...baseConfig,
project: './tsconfig.json',
});

expect(withProjectNoOptionSet.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(withProjectNoOptionSet.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);
});

it('should preserve node maps for parseAndGenerateServices() when option is `true`, regardless of `project` config', () => {
const optionSetToTrue = parser.parseAndGenerateServices(code, {
...baseConfig,
preserveNodeMaps: true,
});

expect(optionSetToTrue.services.esTreeNodeToTSNodeMap).toEqual(
expect(noOptionSet.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(optionSetToTrue.services.tsNodeToESTreeNodeMap).toEqual(
expect(noOptionSet.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);

const withProjectOptionSetToTrue = parser.parseAndGenerateServices(code, {
const withProjectNoOptionSet = parser.parseAndGenerateServices(code, {
...baseConfig,
preserveNodeMaps: true,
project: './tsconfig.json',
});

expect(withProjectOptionSetToTrue.services.esTreeNodeToTSNodeMap).toEqual(
expect(withProjectNoOptionSet.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(withProjectOptionSetToTrue.services.tsNodeToESTreeNodeMap).toEqual(
expect(withProjectNoOptionSet.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);
});

it('should not preserve node maps for parseAndGenerateServices() when option is `false`, regardless of `project` config', () => {
function checkNodeMaps(setting: boolean): void {
const optionSetToFalse = parser.parseAndGenerateServices(code, {
...baseConfig,
preserveNodeMaps: false,
preserveNodeMaps: setting,
});

expect(optionSetToFalse.services.esTreeNodeToTSNodeMap).toBeUndefined();
expect(optionSetToFalse.services.tsNodeToESTreeNodeMap).toBeUndefined();
expect(
optionSetToFalse.services.esTreeNodeToTSNodeMap.has(
optionSetToFalse.ast.body[0],
),
).toBe(setting);
expect(
optionSetToFalse.services.tsNodeToESTreeNodeMap.has(
optionSetToFalse.services.program.getSourceFile('estree.ts'),
),
).toBe(setting);

const withProjectOptionSetToFalse = parser.parseAndGenerateServices(
code,
{ ...baseConfig, preserveNodeMaps: false, project: './tsconfig.json' },
{
...baseConfig,
preserveNodeMaps: setting,
project: './tsconfig.json',
},
);

expect(
withProjectOptionSetToFalse.services.esTreeNodeToTSNodeMap,
).toBeUndefined();
withProjectOptionSetToFalse.services.esTreeNodeToTSNodeMap.has(
withProjectOptionSetToFalse.ast.body[0],
),
).toBe(setting);
expect(
withProjectOptionSetToFalse.services.tsNodeToESTreeNodeMap,
).toBeUndefined();
});
withProjectOptionSetToFalse.services.tsNodeToESTreeNodeMap.has(
withProjectOptionSetToFalse.services.program.getSourceFile(
'estree.ts',
),
),
).toBe(setting);
}

it('should preserve node maps for parseAndGenerateServices() when option is `true`, regardless of `project` config', () =>
checkNodeMaps(true));

it('should not preserve node maps for parseAndGenerateServices() when option is `false`, regardless of `project` config', () =>
checkNodeMaps(false));
});
});

0 comments on commit f2af20c

Please sign in to comment.