Skip to content

refactor(typescript-estree): simplify range calculation for body nodes #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/parser/tests/lib/__snapshots__/javascript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30326,6 +30326,8 @@ Object {
}
`;

exports[`javascript fixtures/classes/class-with-no-body.src 1`] = `"'{' expected."`;

exports[`javascript fixtures/classes/derived-class-assign-to-var.src 1`] = `
Object {
"body": Array [
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95244,6 +95244,8 @@ Object {
}
`;

exports[`typescript fixtures/errorRecovery/interface-with-no-body.src 1`] = `"'{' expected."`;

exports[`typescript fixtures/errorRecovery/object-assertion-not-allowed.src 1`] = `
Object {
"body": Array [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface Foo
69 changes: 11 additions & 58 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,40 +1363,14 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression: {
const heritageClauses = node.heritageClauses || [];

let classNodeType = SyntaxKind[node.kind];
let lastClassToken: any = heritageClauses.length
? heritageClauses[heritageClauses.length - 1]
: node.name;

if (node.typeParameters && node.typeParameters.length) {
const lastTypeParameter =
node.typeParameters[node.typeParameters.length - 1];

if (!lastClassToken || lastTypeParameter.pos > lastClassToken.pos) {
lastClassToken = findNextToken(lastTypeParameter, ast, ast);
}
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters
);
}

if (node.modifiers && node.modifiers.length) {
/**
* We need check for modifiers, and use the last one, as there
* could be multiple before the open brace
*/
const lastModifier = node.modifiers![node.modifiers!.length - 1];

if (!lastClassToken || lastModifier.pos > lastClassToken.pos) {
lastClassToken = findNextToken(lastModifier, ast, ast);
}
} else if (!lastClassToken) {
// no name
lastClassToken = node.getFirstToken();
}

const openBrace = findNextToken(lastClassToken, ast, ast)!;
const superClass = heritageClauses.find(
clause => clause.token === SyntaxKind.ExtendsKeyword
);
Expand All @@ -1421,14 +1395,16 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
clause => clause.token === SyntaxKind.ImplementsKeyword
);

const classBodyRange = [node.members.pos - 1, node.end];

Object.assign(result, {
type: classNodeType,
id: convertChild(node.name),
body: {
type: AST_NODE_TYPES.ClassBody,
body: [],
range: [openBrace.getStart(ast), node.end],
loc: getLocFor(openBrace.getStart(ast), node.end, ast)
range: classBodyRange,
loc: getLocFor(classBodyRange[0], classBodyRange[1], ast)
},
superClass:
superClass && superClass.types[0]
Expand Down Expand Up @@ -2335,45 +2311,22 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
case SyntaxKind.InterfaceDeclaration: {
const interfaceHeritageClauses = node.heritageClauses || [];

let interfaceLastClassToken = interfaceHeritageClauses.length
? interfaceHeritageClauses[interfaceHeritageClauses.length - 1]
: node.name;

if (node.typeParameters && node.typeParameters.length) {
const interfaceLastTypeParameter =
node.typeParameters[node.typeParameters.length - 1];

if (
!interfaceLastClassToken ||
interfaceLastTypeParameter.pos > interfaceLastClassToken.pos
) {
interfaceLastClassToken = findNextToken(
interfaceLastTypeParameter,
ast,
ast
) as any;
}
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters
);
}

const interfaceOpenBrace = findNextToken(
interfaceLastClassToken,
ast,
ast
)!;

const interfaceBody = {
type: AST_NODE_TYPES.TSInterfaceBody,
body: node.members.map(member => convertChild(member)),
range: [interfaceOpenBrace.getStart(ast), node.end],
loc: getLocFor(interfaceOpenBrace.getStart(ast), node.end, ast)
};
const interfaceBodyRange = [node.members.pos - 1, node.end];

Object.assign(result, {
type: AST_NODE_TYPES.TSInterfaceDeclaration,
body: interfaceBody,
body: {
type: AST_NODE_TYPES.TSInterfaceBody,
body: node.members.map(member => convertChild(member)),
range: interfaceBodyRange,
loc: getLocFor(interfaceBodyRange[0], interfaceBodyRange[1], ast)
},
id: convertChild(node.name)
});

Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ export function getTSNodeAccessibility(
/**
* Finds the next token based on the previous one and its parent
* Had to copy this from TS instead of using TS's version because theirs doesn't pass the ast to getChildren
* @param {ts.Node} previousToken The previous TSToken
* @param {ts.TextRange} previousToken The previous TSToken
* @param {ts.Node} parent The parent TSNode
* @param {ts.SourceFile} ast The TS AST
* @returns {ts.Node|undefined} the next TSToken
*/
export function findNextToken(
previousToken: ts.Node,
previousToken: ts.TextRange,
parent: ts.Node,
ast: ts.SourceFile
): ts.Node | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30246,6 +30246,8 @@ Object {
}
`;

exports[`javascript fixtures/classes/class-with-no-body.src 1`] = `"'{' expected."`;

exports[`javascript fixtures/classes/derived-class-assign-to-var.src 1`] = `
Object {
"body": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/classes/class-with-constructor-with-space.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/classes/class-with-no-body.src 1`] = `
Object {
"column": 0,
"index": 10,
"lineNumber": 2,
"message": "'{' expected.",
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/classes/derived-class-assign-to-var.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/classes/derived-class-expression.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down Expand Up @@ -2465,6 +2474,15 @@ Object {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/interface-with-no-body.src 1`] = `
Object {
"column": 0,
"index": 14,
"lineNumber": 2,
"message": "'{' expected.",
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/object-assertion-not-allowed.src 1`] = `
Object {
"column": 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94859,6 +94859,8 @@ Object {
}
`;

exports[`typescript fixtures/errorRecovery/interface-with-no-body.src 1`] = `"'{' expected."`;

exports[`typescript fixtures/errorRecovery/object-assertion-not-allowed.src 1`] = `
Object {
"body": Array [
Expand Down