Skip to content

Commit

Permalink
Don't crash on class bodies with an index signature (#273)
Browse files Browse the repository at this point in the history
Fixes #250

We should skip all type-only lines when processing a class body, and also mark
index signatures as entirely types.
  • Loading branch information
alangpierce authored Jun 25, 2018
1 parent 781cad1 commit f63fc3d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/parser/plugins/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,17 @@ function tsTryParseIndexSignature(): boolean {
return false;
}

const oldIsType = pushTypeContext(0);

expect(tt.bracketL);
parseIdentifier();
tsParseTypeAnnotation();
expect(tt.bracketR);

tsTryParseTypeAnnotation();
tsParseTypeMemberSemicolon();

popTypeContext(oldIsType);
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/getClassInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default function getClassInfo(
({constructorInitializers, constructorInsertPos} = processConstructor(tokens));
} else if (tokens.matches1(tt.semi)) {
tokens.nextToken();
} else if (tokens.currentToken().isType) {
tokens.nextToken();
} else {
// Either a method or a field. Skip to the identifier part.
const statementStartIndex = tokens.currentIndex();
Expand Down
21 changes: 21 additions & 0 deletions test/typescript-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,25 @@ describe("typescript transform", () => {
`,
);
});

it("allows index signatures in classes", () => {
assertTypeScriptResult(
`
export class Foo {
f() {
}
[name: string]: any;
x = 1;
}
`,
`"use strict";${ESMODULE_PREFIX}
class Foo {constructor() { this.x = 1; }
f() {
}
;
} exports.Foo = Foo;
`,
);
});
});

0 comments on commit f63fc3d

Please sign in to comment.