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): options range loc being always true #704

Merged
merged 7 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion packages/parser/src/analyze-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getKeys as fallback } from 'eslint-visitor-keys';

import { ParserOptions } from './parser-options';
import { ScopeManager } from './scope/scope-manager';
import { visitorKeys as childVisitorKeys } from './visitor-keys';
import { visitorKeys as childVisitorKeys } from '@typescript-eslint/typescript-estree';

/**
* Define the override function of `Scope#__define` for global augmentation.
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
ParserServices,
TSESTreeOptions,
TSESTree,
simpleTraverse,
visitorKeys,
} from '@typescript-eslint/typescript-estree';
import { analyzeScope } from './analyze-scope';
import { simpleTraverse } from './simple-traverse';
import { visitorKeys } from './visitor-keys';

type ParserOptions = TSESLint.ParserOptions;

Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"dependencies": {
"debug": "^4.1.1",
"eslint-visitor-keys": "^1.1.0",
"glob": "^7.1.4",
"is-glob": "^4.0.1",
"lodash.unescape": "4.0.1",
Expand Down
17 changes: 17 additions & 0 deletions packages/typescript-estree/src/ast-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { convertComments } from './convert-comments';
import { convertTokens } from './node-utils';
import { Extra } from './parser-options';
import { TSESTree } from './ts-estree';
import { simpleTraverse } from './simple-traverse';

export function astConverter(
ast: SourceFile,
Expand Down Expand Up @@ -32,6 +33,22 @@ export function astConverter(

const estree = instance.convertProgram();

/**
* Optionally remove range and loc if specified
*/
if (extra.range || extra.loc) {
simpleTraverse(estree, {
enter: node => {
if (!extra.range) {
delete node.range;
}
if (!extra.loc) {
delete node.loc;
}
},
});
}

/**
* Optionally convert and include all tokens in the AST
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,7 @@ export {
TSESTreeOptions,
version,
};
export { simpleTraverse } from './simple-traverse';
export { visitorKeys } from './visitor-keys';
export * from './ts-estree';
export { clearCaches } from './create-program/createWatchProgram';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TSESTree } from '@typescript-eslint/typescript-estree';
import { TSESTree } from './ts-estree';
import { visitorKeys } from './visitor-keys';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
122 changes: 122 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,127 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parse() general output should not contain loc 1`] = `
Object {
"body": Array [
Object {
"declarations": Array [
Object {
"id": Object {
"name": "foo",
"range": Array [
4,
7,
],
"type": "Identifier",
},
"init": Object {
"name": "bar",
"range": Array [
10,
13,
],
"type": "Identifier",
},
"range": Array [
4,
13,
],
"type": "VariableDeclarator",
},
],
"kind": "let",
"range": Array [
0,
14,
],
"type": "VariableDeclaration",
},
],
"range": Array [
0,
14,
],
"sourceType": "script",
"type": "Program",
}
`;

exports[`parse() general output should not contain range 1`] = `
Object {
"body": Array [
Object {
"declarations": Array [
Object {
"id": Object {
"loc": Object {
"end": Object {
"column": 7,
"line": 1,
},
"start": Object {
"column": 4,
"line": 1,
},
},
"name": "foo",
"type": "Identifier",
},
"init": Object {
"loc": Object {
"end": Object {
"column": 13,
"line": 1,
},
"start": Object {
"column": 10,
"line": 1,
},
},
"name": "bar",
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 13,
"line": 1,
},
"start": Object {
"column": 4,
"line": 1,
},
},
"type": "VariableDeclarator",
},
],
"kind": "let",
"loc": Object {
"end": Object {
"column": 14,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"type": "VariableDeclaration",
},
],
"loc": Object {
"end": Object {
"column": 14,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"sourceType": "script",
"type": "Program",
}
`;

exports[`parse() general output tokens, comments, locs, and ranges when called with those options 1`] = `
Object {
"body": Array [
Expand Down
16 changes: 16 additions & 0 deletions packages/typescript-estree/tests/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ describe('parse()', () => {
'output tokens, comments, locs, and ranges when called with those options',
createSnapshotTestBlock(code, config),
);

it(
'output should not contain loc',
createSnapshotTestBlock(code, {
range: true,
loc: false,
}),
);

it(
'output should not contain range',
createSnapshotTestBlock(code, {
range: false,
loc: true,
}),
);
});

describe('non string code', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import { AST_NODE_TYPES } from '../../src/ts-estree';
import { visitorKeys } from '../../src/visitor-keys';

//------------------------------------------------------------------------------
Expand Down