X Tutup
Skip to content

Commit 722268d

Browse files
ark120202Perryvw
authored andcommitted
Drop support for legacy annotations syntax (#805)
* Drop support for legacy annotations syntax * Rename `decorators` test directory to `annotations`
1 parent 0f82db6 commit 722268d

File tree

21 files changed

+31
-54
lines changed

21 files changed

+31
-54
lines changed

src/transformation/utils/annotations.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,7 @@ function createAnnotation(name: string, args: string[]): Annotation | undefined
3333

3434
export type AnnotationsMap = Map<AnnotationKind, Annotation>;
3535

36-
function collectAnnotations(
37-
context: TransformationContext,
38-
source: ts.Symbol | ts.Signature,
39-
annotationsMap: AnnotationsMap
40-
): void {
41-
const comments = source.getDocumentationComment(context.checker);
42-
const oldStyleAnnotations = comments
43-
.filter(comment => comment.kind === "text")
44-
.map(comment => comment.text.split("\n"))
45-
.reduce((a, b) => a.concat(b), [])
46-
.map(line => line.trim())
47-
.filter(comment => comment[0] === "!");
48-
49-
for (const line of oldStyleAnnotations) {
50-
const [name, ...args] = line.slice(1).split(" ");
51-
const annotation = createAnnotation(name, args);
52-
if (annotation) {
53-
annotationsMap.set(annotation.kind, annotation);
54-
console.warn(`[Deprecated] Annotations with ! are being deprecated, use '@${annotation.kind}' instead`);
55-
} else {
56-
console.warn(`Encountered unknown annotation '${line}'.`);
57-
}
58-
}
59-
36+
function collectAnnotations(source: ts.Symbol | ts.Signature, annotationsMap: AnnotationsMap): void {
6037
for (const tag of source.getJsDocTags()) {
6138
const annotation = createAnnotation(tag.name, tag.text ? tag.text.split(" ") : []);
6239
if (annotation) {
@@ -65,17 +42,17 @@ function collectAnnotations(
6542
}
6643
}
6744

68-
export function getSymbolAnnotations(context: TransformationContext, symbol: ts.Symbol): AnnotationsMap {
45+
export function getSymbolAnnotations(symbol: ts.Symbol): AnnotationsMap {
6946
const annotationsMap: AnnotationsMap = new Map();
70-
collectAnnotations(context, symbol, annotationsMap);
47+
collectAnnotations(symbol, annotationsMap);
7148
return annotationsMap;
7249
}
7350

74-
export function getTypeAnnotations(context: TransformationContext, type: ts.Type): AnnotationsMap {
51+
export function getTypeAnnotations(type: ts.Type): AnnotationsMap {
7552
const annotationsMap: AnnotationsMap = new Map();
7653

77-
if (type.symbol) collectAnnotations(context, type.symbol, annotationsMap);
78-
if (type.aliasSymbol) collectAnnotations(context, type.aliasSymbol, annotationsMap);
54+
if (type.symbol) collectAnnotations(type.symbol, annotationsMap);
55+
if (type.aliasSymbol) collectAnnotations(type.aliasSymbol, annotationsMap);
7956

8057
return annotationsMap;
8158
}
@@ -116,14 +93,14 @@ export function getFileAnnotations(sourceFile: ts.SourceFile): AnnotationsMap {
11693

11794
export function getSignatureAnnotations(context: TransformationContext, signature: ts.Signature): AnnotationsMap {
11895
const annotationsMap: AnnotationsMap = new Map();
119-
collectAnnotations(context, signature, annotationsMap);
96+
collectAnnotations(signature, annotationsMap);
12097

12198
// Function properties on interfaces have the JSDoc tags on the parent PropertySignature
12299
const declaration = signature.getDeclaration();
123100
if (declaration && declaration.parent && ts.isPropertySignature(declaration.parent)) {
124101
const symbol = context.checker.getSymbolAtLocation(declaration.parent.name);
125102
if (symbol) {
126-
collectAnnotations(context, symbol, annotationsMap);
103+
collectAnnotations(symbol, annotationsMap);
127104
}
128105
}
129106

@@ -154,7 +131,7 @@ export function isTupleReturnCall(context: TransformationContext, node: ts.Node)
154131
}
155132

156133
const type = context.checker.getTypeAtLocation(node.expression);
157-
return getTypeAnnotations(context, type).has(AnnotationKind.TupleReturn);
134+
return getTypeAnnotations(type).has(AnnotationKind.TupleReturn);
158135
}
159136

160137
export function isInTupleReturnFunction(context: TransformationContext, node: ts.Node): boolean {
@@ -185,20 +162,20 @@ export function isInTupleReturnFunction(context: TransformationContext, node: ts
185162
return true;
186163
}
187164

188-
return getTypeAnnotations(context, functionType).has(AnnotationKind.TupleReturn);
165+
return getTypeAnnotations(functionType).has(AnnotationKind.TupleReturn);
189166
}
190167

191168
export function isLuaIteratorType(context: TransformationContext, node: ts.Node): boolean {
192169
const type = context.checker.getTypeAtLocation(node);
193-
return getTypeAnnotations(context, type).has(AnnotationKind.LuaIterator);
170+
return getTypeAnnotations(type).has(AnnotationKind.LuaIterator);
194171
}
195172

196173
export function isVarArgType(context: TransformationContext, node: ts.Node): boolean {
197174
const type = context.checker.getTypeAtLocation(node);
198-
return getTypeAnnotations(context, type).has(AnnotationKind.VarArg);
175+
return getTypeAnnotations(type).has(AnnotationKind.VarArg);
199176
}
200177

201178
export function isForRangeType(context: TransformationContext, node: ts.Node): boolean {
202179
const type = context.checker.getTypeAtLocation(node);
203-
return getTypeAnnotations(context, type).has(AnnotationKind.ForRange);
180+
return getTypeAnnotations(type).has(AnnotationKind.ForRange);
204181
}

src/transformation/utils/assignment-validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function validatePropertyAssignment(
1919
if (!ts.isPropertyAccessExpression(node.left)) return;
2020

2121
const leftType = context.checker.getTypeAtLocation(node.left.expression);
22-
const annotations = getTypeAnnotations(context, leftType);
22+
const annotations = getTypeAnnotations(leftType);
2323
if (annotations.has(AnnotationKind.LuaTable) && node.left.name.text === "length") {
2424
throw ForbiddenLuaTableUseException(`A LuaTable object's length cannot be re-assigned.`, node);
2525
}

src/transformation/visitors/access.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const transformPropertyAccessExpression: FunctionVisitor<ts.PropertyAcces
7272
const property = expression.name.text;
7373
const type = context.checker.getTypeAtLocation(expression.expression);
7474

75-
const annotations = getTypeAnnotations(context, type);
75+
const annotations = getTypeAnnotations(type);
7676
// Do not output path for member only enums
7777
if (annotations.has(AnnotationKind.CompileMembersOnly)) {
7878
if (ts.isPropertyAccessExpression(expression.expression)) {

src/transformation/visitors/binary-expression/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export const transformBinaryExpression: FunctionVisitor<ts.BinaryExpression> = (
178178
const lhs = context.transformExpression(node.left);
179179
const rhs = context.transformExpression(node.right);
180180
const rhsType = context.checker.getTypeAtLocation(node.right);
181-
const annotations = getTypeAnnotations(context, rhsType);
181+
const annotations = getTypeAnnotations(rhsType);
182182

183183
if (annotations.has(AnnotationKind.Extension) || annotations.has(AnnotationKind.MetaExtension)) {
184184
// Cannot use instanceof on extension classes

src/transformation/visitors/class/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function transformClassDeclaration(
8888
throw MissingClassName(classDeclaration);
8989
}
9090

91-
const annotations = getTypeAnnotations(context, context.checker.getTypeAtLocation(classDeclaration));
91+
const annotations = getTypeAnnotations(context.checker.getTypeAtLocation(classDeclaration));
9292

9393
// Find out if this class is extension of existing class
9494
const extensionDirective = annotations.get(AnnotationKind.Extension);
@@ -113,15 +113,15 @@ export function transformClassDeclaration(
113113

114114
if (!(isExtension || isMetaExtension) && extendsType) {
115115
// Non-extensions cannot extend extension classes
116-
const extendsAnnotations = getTypeAnnotations(context, extendsType);
116+
const extendsAnnotations = getTypeAnnotations(extendsType);
117117
if (extendsAnnotations.has(AnnotationKind.Extension) || extendsAnnotations.has(AnnotationKind.MetaExtension)) {
118118
throw InvalidExtendsExtension(classDeclaration);
119119
}
120120
}
121121

122122
// You cannot extend LuaTable classes
123123
if (extendsType) {
124-
const annotations = getTypeAnnotations(context, extendsType);
124+
const annotations = getTypeAnnotations(extendsType);
125125
if (annotations.has(AnnotationKind.LuaTable)) {
126126
throw InvalidExtendsLuaTable(classDeclaration);
127127
}

src/transformation/visitors/class/new.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const transformNewExpression: FunctionVisitor<ts.NewExpression> = (node,
6363

6464
checkForLuaLibType(context, type);
6565

66-
const annotations = getTypeAnnotations(context, type);
66+
const annotations = getTypeAnnotations(type);
6767

6868
if (annotations.has(AnnotationKind.Extension) || annotations.has(AnnotationKind.MetaExtension)) {
6969
throw InvalidNewExpressionOnExtension(node);

src/transformation/visitors/class/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getExtendedTypeNode(
1414
for (const clause of node.heritageClauses) {
1515
if (clause.token === ts.SyntaxKind.ExtendsKeyword) {
1616
const superType = context.checker.getTypeAtLocation(clause.types[0]);
17-
const annotations = getTypeAnnotations(context, superType);
17+
const annotations = getTypeAnnotations(superType);
1818
if (!annotations.has(AnnotationKind.PureAbstract)) {
1919
return clause.types[0];
2020
}

src/transformation/visitors/enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const transformEnumDeclaration: FunctionVisitor<ts.EnumDeclaration> = (no
2525
}
2626

2727
const type = context.checker.getTypeAtLocation(node);
28-
const membersOnly = getTypeAnnotations(context, type).has(AnnotationKind.CompileMembersOnly);
28+
const membersOnly = getTypeAnnotations(type).has(AnnotationKind.CompileMembersOnly);
2929
const result: lua.Statement[] = [];
3030

3131
if (!membersOnly) {

src/transformation/visitors/loops/for-of.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function transformForOfLuaIteratorStatement(
123123
): lua.Statement {
124124
const luaIterator = context.transformExpression(statement.expression);
125125
const type = context.checker.getTypeAtLocation(statement.expression);
126-
const tupleReturn = getTypeAnnotations(context, type).has(AnnotationKind.TupleReturn);
126+
const tupleReturn = getTypeAnnotations(type).has(AnnotationKind.TupleReturn);
127127
if (tupleReturn) {
128128
// LuaIterator + TupleReturn
129129
if (ts.isVariableDeclarationList(statement.initializer)) {

src/transformation/visitors/lua-table.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function transformLuaTableExpressionStatement(
7171

7272
if (ts.isCallExpression(expression) && ts.isPropertyAccessExpression(expression.expression)) {
7373
const ownerType = context.checker.getTypeAtLocation(expression.expression.expression);
74-
const annotations = getTypeAnnotations(context, ownerType);
74+
const annotations = getTypeAnnotations(ownerType);
7575
if (annotations.has(AnnotationKind.LuaTable)) {
7676
return transformLuaTableExpressionAsExpressionStatement(context, expression);
7777
}
@@ -84,7 +84,7 @@ export function transformLuaTableCallExpression(
8484
): lua.Expression | undefined {
8585
if (ts.isPropertyAccessExpression(node.expression) || ts.isElementAccessExpression(node.expression)) {
8686
const ownerType = context.checker.getTypeAtLocation(node.expression.expression);
87-
const annotations = getTypeAnnotations(context, ownerType);
87+
const annotations = getTypeAnnotations(ownerType);
8888

8989
if (annotations.has(AnnotationKind.LuaTable)) {
9090
const [luaTable, methodName] = parseLuaTableExpression(context, node.expression);
@@ -107,7 +107,7 @@ export function transformLuaTablePropertyAccessExpression(
107107
node: ts.PropertyAccessExpression
108108
): lua.Expression | undefined {
109109
const type = context.checker.getTypeAtLocation(node.expression);
110-
const annotations = getTypeAnnotations(context, type);
110+
const annotations = getTypeAnnotations(type);
111111
if (annotations.has(AnnotationKind.LuaTable)) {
112112
const [luaTable, propertyName] = parseLuaTableExpression(context, node);
113113
switch (node.name.text) {
@@ -123,7 +123,7 @@ export function transformLuaTableElementAccessExpression(
123123
context: TransformationContext,
124124
node: ts.ElementAccessExpression
125125
): void {
126-
const annotations = getTypeAnnotations(context, context.checker.getTypeAtLocation(node.expression));
126+
const annotations = getTypeAnnotations(context.checker.getTypeAtLocation(node.expression));
127127
if (annotations.has(AnnotationKind.LuaTable)) {
128128
throw UnsupportedKind("LuaTable access expression", node.kind, node);
129129
}
@@ -134,7 +134,7 @@ export function transformLuaTableNewExpression(
134134
node: ts.NewExpression
135135
): lua.Expression | undefined {
136136
const type = context.checker.getTypeAtLocation(node);
137-
const annotations = getTypeAnnotations(context, type);
137+
const annotations = getTypeAnnotations(type);
138138
if (annotations.has(AnnotationKind.LuaTable)) {
139139
if (node.arguments && node.arguments.length > 0) {
140140
throw ForbiddenLuaTableUseException("No parameters are allowed when constructing a LuaTable object.", node);

0 commit comments

Comments
 (0)
X Tutup