Skip to content

Commit db1aa18

Browse files
Mark1626bradzacher
andcommitted
fix(typescript-estree): options range loc being always true (typescript-eslint#704)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent 864c811 commit db1aa18

File tree

10 files changed

+163
-5
lines changed

10 files changed

+163
-5
lines changed

packages/parser/src/analyze-scope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getKeys as fallback } from 'eslint-visitor-keys';
77

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

1212
/**
1313
* Define the override function of `Scope#__define` for global augmentation.

packages/parser/src/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
ParserServices,
66
TSESTreeOptions,
77
TSESTree,
8+
simpleTraverse,
9+
visitorKeys,
810
} from '@typescript-eslint/typescript-estree';
911
import { analyzeScope } from './analyze-scope';
10-
import { simpleTraverse } from './simple-traverse';
11-
import { visitorKeys } from './visitor-keys';
1212

1313
type ParserOptions = TSESLint.ParserOptions;
1414

packages/typescript-estree/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"dependencies": {
4242
"debug": "^4.1.1",
43+
"eslint-visitor-keys": "^1.1.0",
4344
"glob": "^7.1.4",
4445
"is-glob": "^4.0.1",
4546
"lodash.unescape": "4.0.1",

packages/typescript-estree/src/ast-converter.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { convertComments } from './convert-comments';
44
import { convertTokens } from './node-utils';
55
import { Extra } from './parser-options';
66
import { TSESTree } from './ts-estree';
7+
import { simpleTraverse } from './simple-traverse';
78

89
export function astConverter(
910
ast: SourceFile,
@@ -32,6 +33,22 @@ export function astConverter(
3233

3334
const estree = instance.convertProgram();
3435

36+
/**
37+
* Optionally remove range and loc if specified
38+
*/
39+
if (extra.range || extra.loc) {
40+
simpleTraverse(estree, {
41+
enter: node => {
42+
if (!extra.range) {
43+
delete node.range;
44+
}
45+
if (!extra.loc) {
46+
delete node.loc;
47+
}
48+
},
49+
});
50+
}
51+
3552
/**
3653
* Optionally convert and include all tokens in the AST
3754
*/

packages/typescript-estree/src/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,5 +419,7 @@ export {
419419
TSESTreeOptions,
420420
version,
421421
};
422+
export { simpleTraverse } from './simple-traverse';
423+
export { visitorKeys } from './visitor-keys';
422424
export * from './ts-estree';
423425
export { clearCaches } from './create-program/createWatchProgram';

packages/parser/src/simple-traverse.ts renamed to packages/typescript-estree/src/simple-traverse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TSESTree } from '@typescript-eslint/typescript-estree';
1+
import { TSESTree } from './ts-estree';
22
import { visitorKeys } from './visitor-keys';
33

44
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,127 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`parse() general output should not contain loc 1`] = `
4+
Object {
5+
"body": Array [
6+
Object {
7+
"declarations": Array [
8+
Object {
9+
"id": Object {
10+
"name": "foo",
11+
"range": Array [
12+
4,
13+
7,
14+
],
15+
"type": "Identifier",
16+
},
17+
"init": Object {
18+
"name": "bar",
19+
"range": Array [
20+
10,
21+
13,
22+
],
23+
"type": "Identifier",
24+
},
25+
"range": Array [
26+
4,
27+
13,
28+
],
29+
"type": "VariableDeclarator",
30+
},
31+
],
32+
"kind": "let",
33+
"range": Array [
34+
0,
35+
14,
36+
],
37+
"type": "VariableDeclaration",
38+
},
39+
],
40+
"range": Array [
41+
0,
42+
14,
43+
],
44+
"sourceType": "script",
45+
"type": "Program",
46+
}
47+
`;
48+
49+
exports[`parse() general output should not contain range 1`] = `
50+
Object {
51+
"body": Array [
52+
Object {
53+
"declarations": Array [
54+
Object {
55+
"id": Object {
56+
"loc": Object {
57+
"end": Object {
58+
"column": 7,
59+
"line": 1,
60+
},
61+
"start": Object {
62+
"column": 4,
63+
"line": 1,
64+
},
65+
},
66+
"name": "foo",
67+
"type": "Identifier",
68+
},
69+
"init": Object {
70+
"loc": Object {
71+
"end": Object {
72+
"column": 13,
73+
"line": 1,
74+
},
75+
"start": Object {
76+
"column": 10,
77+
"line": 1,
78+
},
79+
},
80+
"name": "bar",
81+
"type": "Identifier",
82+
},
83+
"loc": Object {
84+
"end": Object {
85+
"column": 13,
86+
"line": 1,
87+
},
88+
"start": Object {
89+
"column": 4,
90+
"line": 1,
91+
},
92+
},
93+
"type": "VariableDeclarator",
94+
},
95+
],
96+
"kind": "let",
97+
"loc": Object {
98+
"end": Object {
99+
"column": 14,
100+
"line": 1,
101+
},
102+
"start": Object {
103+
"column": 0,
104+
"line": 1,
105+
},
106+
},
107+
"type": "VariableDeclaration",
108+
},
109+
],
110+
"loc": Object {
111+
"end": Object {
112+
"column": 14,
113+
"line": 1,
114+
},
115+
"start": Object {
116+
"column": 0,
117+
"line": 1,
118+
},
119+
},
120+
"sourceType": "script",
121+
"type": "Program",
122+
}
123+
`;
124+
3125
exports[`parse() general output tokens, comments, locs, and ranges when called with those options 1`] = `
4126
Object {
5127
"body": Array [

packages/typescript-estree/tests/lib/parse.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ describe('parse()', () => {
3737
'output tokens, comments, locs, and ranges when called with those options',
3838
createSnapshotTestBlock(code, config),
3939
);
40+
41+
it(
42+
'output should not contain loc',
43+
createSnapshotTestBlock(code, {
44+
range: true,
45+
loc: false,
46+
}),
47+
);
48+
49+
it(
50+
'output should not contain range',
51+
createSnapshotTestBlock(code, {
52+
range: false,
53+
loc: true,
54+
}),
55+
);
4056
});
4157

4258
describe('non string code', () => {

packages/parser/tests/lib/visitor-keys.ts renamed to packages/typescript-estree/tests/lib/visitor-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
1+
import { AST_NODE_TYPES } from '../../src/ts-estree';
22
import { visitorKeys } from '../../src/visitor-keys';
33

44
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy