@@ -2,7 +2,6 @@ import * as ts from "typescript";
22import { flatMap } from "../../../utils" ;
33import { TransformationContext } from "../../context" ;
44
5- export type AnnotationName = string & { _annotationNameBrand : any } ;
65export enum AnnotationKind {
76 Extension = "Extension" ,
87 MetaExtension = "MetaExtension" ,
@@ -20,21 +19,15 @@ export enum AnnotationKind {
2019 ForRange = "ForRange" ,
2120}
2221
23- function getAnnotationKindByName ( name : AnnotationName ) : AnnotationKind ;
24- function getAnnotationKindByName ( name : string ) : AnnotationKind | undefined ;
25- function getAnnotationKindByName ( name : string ) : AnnotationKind | undefined {
26- return Object . values ( AnnotationKind ) . find ( k => k . toLowerCase ( ) === name . toLowerCase ( ) ) ;
22+ export interface Annotation {
23+ kind : AnnotationKind ;
24+ args : string [ ] ;
2725}
2826
29- function isValidAnnotationName ( name : string ) : name is AnnotationName {
30- return getAnnotationKindByName ( name ) !== undefined ;
31- }
32-
33- export class Annotation {
34- public kind : AnnotationKind ;
35-
36- constructor ( name : AnnotationName , public args : string [ ] ) {
37- this . kind = getAnnotationKindByName ( name ) ;
27+ function createAnnotation ( name : string , args : string [ ] ) : Annotation | undefined {
28+ const annotationKind = Object . values ( AnnotationKind ) . find ( k => k . toLowerCase ( ) === name . toLowerCase ( ) ) ;
29+ if ( annotationKind !== undefined ) {
30+ return { kind : annotationKind , args } ;
3831 }
3932}
4033
@@ -55,8 +48,8 @@ function collectAnnotations(
5548
5649 for ( const line of oldStyleAnnotations ) {
5750 const [ name , ...args ] = line . slice ( 1 ) . split ( " " ) ;
58- if ( isValidAnnotationName ( name ) ) {
59- const annotation = new Annotation ( name , args ) ;
51+ const annotation = createAnnotation ( name , args ) ;
52+ if ( annotation ) {
6053 annotationsMap . set ( annotation . kind , annotation ) ;
6154 console . warn ( `[Deprecated] Annotations with ! are being deprecated, use '@${ annotation . kind } ' instead` ) ;
6255 } else {
@@ -65,8 +58,8 @@ function collectAnnotations(
6558 }
6659
6760 for ( const tag of source . getJsDocTags ( ) ) {
68- if ( isValidAnnotationName ( tag . name ) ) {
69- const annotation = new Annotation ( tag . name , tag . text ? tag . text . split ( " " ) : [ ] ) ;
61+ const annotation = createAnnotation ( tag . name , tag . text ? tag . text . split ( " " ) : [ ] ) ;
62+ if ( annotation ) {
7063 annotationsMap . set ( annotation . kind , annotation ) ;
7164 }
7265 }
@@ -92,8 +85,8 @@ export function getNodeAnnotations(node: ts.Node): AnnotationsMap {
9285
9386 for ( const tag of ts . getJSDocTags ( node ) ) {
9487 const tagName = tag . tagName . text ;
95- if ( isValidAnnotationName ( tagName ) ) {
96- const annotation = new Annotation ( tagName , tag . comment ? tag . comment . split ( " " ) : [ ] ) ;
88+ const annotation = createAnnotation ( tagName , tag . comment ? tag . comment . split ( " " ) : [ ] ) ;
89+ if ( annotation ) {
9790 annotationsMap . set ( annotation . kind , annotation ) ;
9891 }
9992 }
@@ -110,9 +103,9 @@ export function getFileAnnotations(sourceFile: ts.SourceFile): AnnotationsMap {
110103 if ( jsDoc ) {
111104 for ( const tag of flatMap ( jsDoc , x => x . tags || [ ] ) ) {
112105 const tagName = tag . tagName . text ;
113- if ( isValidAnnotationName ( tagName ) ) {
114- const dec = new Annotation ( tagName , tag . comment ? tag . comment . split ( " " ) : [ ] ) ;
115- annotationsMap . set ( dec . kind , dec ) ;
106+ const annotation = createAnnotation ( tagName , tag . comment ? tag . comment . split ( " " ) : [ ] ) ;
107+ if ( annotation ) {
108+ annotationsMap . set ( annotation . kind , annotation ) ;
116109 }
117110 }
118111 }
0 commit comments