Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Last active July 10, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjhea0/3c5ae215d54a237e550711b954ae3186 to your computer and use it in GitHub Desktop.
Save mjhea0/3c5ae215d54a237e550711b954ae3186 to your computer and use it in GitHub Desktop.

Workaround for angular/angular-cli#9036.

In getErrorSpanForNode() in node_modules/typescript/lib/typescript.js...

Add-

if (!sourceFile) {
  return ts.createTextSpan(0, 0);
}

Just before-

if (errorNode === undefined) {
    // If we don't have a better node, then just set the error on the first token of
    // construct.
    return getSpanOfTokenAtPosition(sourceFile, node.pos);
}

Final function-

function getErrorSpanForNode(sourceFile, node) {
  var errorNode = node;
  switch (node.kind) {
      case 268 /* SourceFile */:
          var pos_1 = ts.skipTrivia(sourceFile.text, 0, /*stopAfterLineBreak*/ false);
          if (pos_1 === sourceFile.text.length) {
              // file is empty - return span for the beginning of the file
              return ts.createTextSpan(0, 0);
          }
          return getSpanOfTokenAtPosition(sourceFile, pos_1);
      // This list is a work in progress. Add missing node kinds to improve their error
      // spans.
      case 226 /* VariableDeclaration */:
      case 176 /* BindingElement */:
      case 229 /* ClassDeclaration */:
      case 199 /* ClassExpression */:
      case 230 /* InterfaceDeclaration */:
      case 233 /* ModuleDeclaration */:
      case 232 /* EnumDeclaration */:
      case 267 /* EnumMember */:
      case 228 /* FunctionDeclaration */:
      case 186 /* FunctionExpression */:
      case 151 /* MethodDeclaration */:
      case 153 /* GetAccessor */:
      case 154 /* SetAccessor */:
      case 231 /* TypeAliasDeclaration */:
          errorNode = node.name;
          break;
      case 187 /* ArrowFunction */:
          return getErrorSpanForArrowFunction(sourceFile, node);
  }
  // new
  if (!sourceFile) {
    return ts.createTextSpan(0, 0);
  }
  if (errorNode === undefined) {
      // If we don't have a better node, then just set the error on the first token of
      // construct.
      return getSpanOfTokenAtPosition(sourceFile, node.pos);
  }
  var pos = nodeIsMissing(errorNode)
      ? errorNode.pos
      : ts.skipTrivia(sourceFile.text, errorNode.pos);
  return ts.createTextSpanFromBounds(pos, errorNode.end);
}
@mfrazie2
Copy link

mfrazie2 commented Jul 10, 2018

🔥 🔥 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment