Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 42f29a1

Browse files
ikatyangJamesHenry
authored andcommitted
Fix: error on multiple super classes (fixes #493) (#494)
1 parent 11d9169 commit 42f29a1

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

lib/ast-converter.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,7 @@ const convert = require("./convert"),
2626
* @returns {Object} converted error object
2727
*/
2828
function convertError(error) {
29-
const loc = error.file.getLineAndCharacterOfPosition(error.start);
30-
return {
31-
index: error.start,
32-
lineNumber: loc.line + 1,
33-
column: loc.character,
34-
message: error.message || error.messageText
35-
};
29+
return nodeUtils.createError(error.file, error.start, error.message || error.messageText);
3630
}
3731

3832
//------------------------------------------------------------------------------

lib/convert.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,8 +1397,14 @@ module.exports = function convert(config) {
13971397
const openBrace = nodeUtils.findNextToken(lastClassToken, ast);
13981398
const superClass = heritageClauses.find(clause => clause.token === SyntaxKind.ExtendsKeyword);
13991399

1400-
if (superClass && superClass.types[0] && superClass.types[0].typeArguments) {
1401-
result.superTypeParameters = convertTypeArgumentsToTypeParameters(superClass.types[0].typeArguments);
1400+
if (superClass) {
1401+
if (superClass.types.length > 1) {
1402+
throw nodeUtils.createError(ast, superClass.types[1].pos, "Classes can only extend a single class.");
1403+
}
1404+
1405+
if (superClass.types[0] && superClass.types[0].typeArguments) {
1406+
result.superTypeParameters = convertTypeArgumentsToTypeParameters(superClass.types[0].typeArguments);
1407+
}
14021408
}
14031409

14041410
const implementsClause = heritageClauses.find(clause => clause.token === SyntaxKind.ImplementsKeyword);

lib/node-utils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ module.exports = {
191191
isWithinTypeAnnotation,
192192
isTypeKeyword,
193193
isComment,
194-
isJSDocComment
194+
isJSDocComment,
195+
createError
195196
};
196197
/* eslint-enable no-use-before-define */
197198

@@ -767,3 +768,19 @@ function getNodeContainer(ast, start, end) {
767768

768769
return container;
769770
}
771+
772+
/**
773+
* @param {Object} ast the AST object
774+
* @param {int} start the index at which the error starts
775+
* @param {string} message the error message
776+
* @returns {Object} converted error object
777+
*/
778+
function createError(ast, start, message) {
779+
const loc = ast.getLineAndCharacterOfPosition(start);
780+
return {
781+
index: start,
782+
lineNumber: loc.line + 1,
783+
column: loc.character,
784+
message
785+
};
786+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A extends B, C {}

tests/lib/__snapshots__/ecma-features.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22287,6 +22287,8 @@ Object {
2228722287
}
2228822288
`;
2228922289

22290+
exports[`ecmaFeatures fixtures/classes/invalid-class-two-super-classes.src 1`] = `"Classes can only extend a single class."`;
22291+
2229022292
exports[`ecmaFeatures fixtures/classes/named-class-expression.src 1`] = `
2229122293
Object {
2229222294
"body": Array [

0 commit comments

Comments
 (0)