Skip to content

Commit

Permalink
fix(typescript-estree): options range loc being always true (#704)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
Mark1626 and bradzacher committed Nov 12, 2019
1 parent 864c811 commit db1aa18
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/parser/src/analyze-scope.ts
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
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
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
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
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';
@@ -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
File renamed without changes.
122 changes: 122 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
@@ -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
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
@@ -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

0 comments on commit db1aa18

Please sign in to comment.