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

Commit

Permalink
Fix: error on multiple super classes (fixes #493) (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang authored and JamesHenry committed Jun 27, 2018
1 parent 11d9169 commit 42f29a1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
8 changes: 1 addition & 7 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,7 @@ const convert = require("./convert"),
* @returns {Object} converted error object
*/
function convertError(error) {
const loc = error.file.getLineAndCharacterOfPosition(error.start);
return {
index: error.start,
lineNumber: loc.line + 1,
column: loc.character,
message: error.message || error.messageText
};
return nodeUtils.createError(error.file, error.start, error.message || error.messageText);
}

//------------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,14 @@ module.exports = function convert(config) {
const openBrace = nodeUtils.findNextToken(lastClassToken, ast);
const superClass = heritageClauses.find(clause => clause.token === SyntaxKind.ExtendsKeyword);

if (superClass && superClass.types[0] && superClass.types[0].typeArguments) {
result.superTypeParameters = convertTypeArgumentsToTypeParameters(superClass.types[0].typeArguments);
if (superClass) {
if (superClass.types.length > 1) {
throw nodeUtils.createError(ast, superClass.types[1].pos, "Classes can only extend a single class.");
}

if (superClass.types[0] && superClass.types[0].typeArguments) {
result.superTypeParameters = convertTypeArgumentsToTypeParameters(superClass.types[0].typeArguments);
}
}

const implementsClause = heritageClauses.find(clause => clause.token === SyntaxKind.ImplementsKeyword);
Expand Down
19 changes: 18 additions & 1 deletion lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ module.exports = {
isWithinTypeAnnotation,
isTypeKeyword,
isComment,
isJSDocComment
isJSDocComment,
createError
};
/* eslint-enable no-use-before-define */

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

return container;
}

/**
* @param {Object} ast the AST object
* @param {int} start the index at which the error starts
* @param {string} message the error message
* @returns {Object} converted error object
*/
function createError(ast, start, message) {
const loc = ast.getLineAndCharacterOfPosition(start);
return {
index: start,
lineNumber: loc.line + 1,
column: loc.character,
message
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class A extends B, C {}
2 changes: 2 additions & 0 deletions tests/lib/__snapshots__/ecma-features.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22287,6 +22287,8 @@ Object {
}
`;

exports[`ecmaFeatures fixtures/classes/invalid-class-two-super-classes.src 1`] = `"Classes can only extend a single class."`;

exports[`ecmaFeatures fixtures/classes/named-class-expression.src 1`] = `
Object {
"body": Array [
Expand Down

0 comments on commit 42f29a1

Please sign in to comment.