Skip to content

Commit c8dfac3

Browse files
armano2bradzacher
authored andcommitted
fix(typescript-estree): error on unexpected jsdoc nodes (typescript-eslint#1525)
1 parent 6d1d2a2 commit c8dfac3

File tree

3 files changed

+71
-305
lines changed

3 files changed

+71
-305
lines changed

packages/typescript-estree/src/convert.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,27 @@ export class Converter {
379379
* property instead of a kind property. Recursively copies all children.
380380
*/
381381
private deeplyCopy(node: TSNode): any {
382+
if (
383+
node.kind >= SyntaxKind.FirstJSDocNode &&
384+
node.kind <= SyntaxKind.LastJSDocNode
385+
) {
386+
throw createError(
387+
this.ast,
388+
node.pos,
389+
'JSDoc types can only be used inside documentation comments.',
390+
);
391+
}
392+
382393
const customType = `TS${SyntaxKind[node.kind]}` as AST_NODE_TYPES;
394+
383395
/**
384396
* If the "errorOnUnknownASTType" option is set to true, throw an error,
385397
* otherwise fallback to just including the unknown type as-is.
386398
*/
387399
if (this.options.errorOnUnknownASTType && !AST_NODE_TYPES[customType]) {
388400
throw new Error(`Unknown AST_NODE_TYPE: "${customType}"`);
389401
}
402+
390403
const result = this.createNode<any>(node, {
391404
type: customType,
392405
});

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

Lines changed: 16 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,6 @@ exports[`convert deeplyCopy should convert node correctly 1`] = `
179179
Object {
180180
"body": Array [
181181
Object {
182-
"id": Object {
183-
"loc": Object {
184-
"end": Object {
185-
"column": 8,
186-
"line": 1,
187-
},
188-
"start": Object {
189-
"column": 5,
190-
"line": 1,
191-
},
192-
},
193-
"name": "foo",
194-
"range": Array [
195-
5,
196-
8,
197-
],
198-
"type": "Identifier",
199-
},
200182
"loc": Object {
201183
"end": Object {
202184
"column": 35,
@@ -207,294 +189,34 @@ Object {
207189
"line": 1,
208190
},
209191
},
210-
"range": Array [
211-
0,
212-
35,
213-
],
214-
"type": "TSTypeAliasDeclaration",
215-
"typeAnnotation": Object {
192+
"modifiers": undefined,
193+
"name": Object {
194+
"escapedText": "foo",
216195
"loc": Object {
217196
"end": Object {
218-
"column": 35,
197+
"column": 8,
219198
"line": 1,
220199
},
221200
"start": Object {
222-
"column": 11,
201+
"column": 5,
223202
"line": 1,
224203
},
225204
},
226205
"range": Array [
227-
11,
228-
35,
206+
5,
207+
8,
229208
],
230209
"transformFlags": 0,
231-
"type": "TSJSDocNullableType",
232-
"typeAnnotation": Object {
233-
"loc": Object {
234-
"end": Object {
235-
"column": 35,
236-
"line": 1,
237-
},
238-
"start": Object {
239-
"column": 11,
240-
"line": 1,
241-
},
242-
},
243-
"range": Array [
244-
11,
245-
35,
246-
],
247-
"type": "TSTypeAnnotation",
248-
"typeAnnotation": Object {
249-
"loc": Object {
250-
"end": Object {
251-
"column": 35,
252-
"line": 1,
253-
},
254-
"start": Object {
255-
"column": 12,
256-
"line": 1,
257-
},
258-
},
259-
"range": Array [
260-
12,
261-
35,
262-
],
263-
"type": "TSUnionType",
264-
"types": Array [
265-
Object {
266-
"loc": Object {
267-
"end": Object {
268-
"column": 18,
269-
"line": 1,
270-
},
271-
"start": Object {
272-
"column": 12,
273-
"line": 1,
274-
},
275-
},
276-
"range": Array [
277-
12,
278-
18,
279-
],
280-
"type": "TSTypeReference",
281-
"typeName": Object {
282-
"loc": Object {
283-
"end": Object {
284-
"column": 15,
285-
"line": 1,
286-
},
287-
"start": Object {
288-
"column": 12,
289-
"line": 1,
290-
},
291-
},
292-
"name": "foo",
293-
"range": Array [
294-
12,
295-
15,
296-
],
297-
"type": "Identifier",
298-
},
299-
"typeParameters": Object {
300-
"loc": Object {
301-
"end": Object {
302-
"column": 18,
303-
"line": 1,
304-
},
305-
"start": Object {
306-
"column": 15,
307-
"line": 1,
308-
},
309-
},
310-
"params": Array [
311-
Object {
312-
"loc": Object {
313-
"end": Object {
314-
"column": 17,
315-
"line": 1,
316-
},
317-
"start": Object {
318-
"column": 16,
319-
"line": 1,
320-
},
321-
},
322-
"range": Array [
323-
16,
324-
17,
325-
],
326-
"type": "TSTypeReference",
327-
"typeName": Object {
328-
"loc": Object {
329-
"end": Object {
330-
"column": 17,
331-
"line": 1,
332-
},
333-
"start": Object {
334-
"column": 16,
335-
"line": 1,
336-
},
337-
},
338-
"name": "T",
339-
"range": Array [
340-
16,
341-
17,
342-
],
343-
"type": "Identifier",
344-
},
345-
"typeParameters": undefined,
346-
},
347-
],
348-
"range": Array [
349-
15,
350-
18,
351-
],
352-
"type": "TSTypeParameterInstantiation",
353-
},
354-
},
355-
Object {
356-
"loc": Object {
357-
"end": Object {
358-
"column": 35,
359-
"line": 1,
360-
},
361-
"start": Object {
362-
"column": 21,
363-
"line": 1,
364-
},
365-
},
366-
"range": Array [
367-
21,
368-
35,
369-
],
370-
"transformFlags": 0,
371-
"type": "TSJSDocNullableType",
372-
"typeAnnotation": Object {
373-
"loc": Object {
374-
"end": Object {
375-
"column": 35,
376-
"line": 1,
377-
},
378-
"start": Object {
379-
"column": 21,
380-
"line": 1,
381-
},
382-
},
383-
"range": Array [
384-
21,
385-
35,
386-
],
387-
"type": "TSTypeAnnotation",
388-
"typeAnnotation": Object {
389-
"loc": Object {
390-
"end": Object {
391-
"column": 35,
392-
"line": 1,
393-
},
394-
"start": Object {
395-
"column": 22,
396-
"line": 1,
397-
},
398-
},
399-
"range": Array [
400-
22,
401-
35,
402-
],
403-
"transformFlags": 0,
404-
"type": "TSJSDocNullableType",
405-
"typeAnnotation": Object {
406-
"loc": Object {
407-
"end": Object {
408-
"column": 34,
409-
"line": 1,
410-
},
411-
"start": Object {
412-
"column": 21,
413-
"line": 1,
414-
},
415-
},
416-
"range": Array [
417-
21,
418-
34,
419-
],
420-
"type": "TSTypeAnnotation",
421-
"typeAnnotation": Object {
422-
"loc": Object {
423-
"end": Object {
424-
"column": 34,
425-
"line": 1,
426-
},
427-
"start": Object {
428-
"column": 22,
429-
"line": 1,
430-
},
431-
},
432-
"range": Array [
433-
22,
434-
34,
435-
],
436-
"type": "TSParenthesizedType",
437-
"typeAnnotation": Object {
438-
"loc": Object {
439-
"end": Object {
440-
"column": 33,
441-
"line": 1,
442-
},
443-
"start": Object {
444-
"column": 23,
445-
"line": 1,
446-
},
447-
},
448-
"params": Array [],
449-
"range": Array [
450-
23,
451-
33,
452-
],
453-
"returnType": Object {
454-
"loc": Object {
455-
"end": Object {
456-
"column": 33,
457-
"line": 1,
458-
},
459-
"start": Object {
460-
"column": 26,
461-
"line": 1,
462-
},
463-
},
464-
"range": Array [
465-
26,
466-
33,
467-
],
468-
"type": "TSTypeAnnotation",
469-
"typeAnnotation": Object {
470-
"loc": Object {
471-
"end": Object {
472-
"column": 33,
473-
"line": 1,
474-
},
475-
"start": Object {
476-
"column": 29,
477-
"line": 1,
478-
},
479-
},
480-
"range": Array [
481-
29,
482-
33,
483-
],
484-
"type": "TSVoidKeyword",
485-
},
486-
},
487-
"type": "TSFunctionType",
488-
},
489-
},
490-
},
491-
},
492-
},
493-
},
494-
],
495-
},
496-
},
210+
"type": "TSUnparsedPrologue",
497211
},
212+
"range": Array [
213+
0,
214+
35,
215+
],
216+
"transformFlags": 0,
217+
"type": "TSUnparsedPrologue",
218+
"typeAnnotation": null,
219+
"typeParameters": null,
498220
},
499221
],
500222
"loc": Object {

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