definitionWorker.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // monaco is using 'define' for module dependencies and service lookup.
  2. // hopefully typescript is self-contained
  3. var ts = null;
  4. var define = (id, dependencies, callback) => ts = callback();
  5. importScripts("../node_modules/monaco-editor/dev/vs/language/typescript/lib/typescriptServices.js");
  6. // store deprecated names
  7. var deprecatedCandidates = [];
  8. function canHaveJsDoc(node) {
  9. const kind = node.kind;
  10. switch (kind) {
  11. case ts.SyntaxKind.Parameter:
  12. case ts.SyntaxKind.CallSignature:
  13. case ts.SyntaxKind.ConstructSignature:
  14. case ts.SyntaxKind.MethodSignature:
  15. case ts.SyntaxKind.PropertySignature:
  16. case ts.SyntaxKind.ArrowFunction:
  17. case ts.SyntaxKind.ParenthesizedExpression:
  18. case ts.SyntaxKind.SpreadAssignment:
  19. case ts.SyntaxKind.ShorthandPropertyAssignment:
  20. case ts.SyntaxKind.PropertyAssignment:
  21. case ts.SyntaxKind.FunctionExpression:
  22. case ts.SyntaxKind.FunctionDeclaration:
  23. case ts.SyntaxKind.LabeledStatement:
  24. case ts.SyntaxKind.ExpressionStatement:
  25. case ts.SyntaxKind.VariableStatement:
  26. case ts.SyntaxKind.Constructor:
  27. case ts.SyntaxKind.MethodDeclaration:
  28. case ts.SyntaxKind.PropertyDeclaration:
  29. case ts.SyntaxKind.GetAccessor:
  30. case ts.SyntaxKind.SetAccessor:
  31. case ts.SyntaxKind.ClassDeclaration:
  32. case ts.SyntaxKind.ClassExpression:
  33. case ts.SyntaxKind.InterfaceDeclaration:
  34. case ts.SyntaxKind.TypeAliasDeclaration:
  35. case ts.SyntaxKind.EnumMember:
  36. case ts.SyntaxKind.EnumDeclaration:
  37. case ts.SyntaxKind.ModuleDeclaration:
  38. case ts.SyntaxKind.ImportEqualsDeclaration:
  39. case ts.SyntaxKind.IndexSignature:
  40. case ts.SyntaxKind.FunctionType:
  41. case ts.SyntaxKind.ConstructorType:
  42. case ts.SyntaxKind.JSDocFunctionType:
  43. case ts.SyntaxKind.EndOfFileToken:
  44. case ts.SyntaxKind.ExportDeclaration:
  45. return true;
  46. default:
  47. return false;
  48. }
  49. }
  50. function onFindDeprecatedCandidate(node) {
  51. const name = relatedName(node);
  52. if (name)
  53. deprecatedCandidates.push(name);
  54. }
  55. function relatedName(node) {
  56. if (canHaveJsDoc(node) && node.name)
  57. return node.name.escapedText;
  58. if (node.parent)
  59. return relatedName(parent);
  60. return undefined;
  61. }
  62. function visit(node) {
  63. if (node.jsDoc) {
  64. for (const jsDocEntry of node.jsDoc) {
  65. if (jsDocEntry.tags) {
  66. for (const tag of jsDocEntry.tags) {
  67. if (tag.tagName && tag.tagName.escapedText == 'deprecated')
  68. onFindDeprecatedCandidate(node);
  69. }
  70. }
  71. }
  72. }
  73. ts.forEachChild(node, visit);
  74. }
  75. function processDefinition(code) {
  76. if (deprecatedCandidates.length == 0) {
  77. const sourceFile = ts.createSourceFile('babylon.js', code, ts.ScriptTarget.ESNext, true);
  78. ts.forEachChild(sourceFile, visit);
  79. }
  80. self.postMessage({ result: deprecatedCandidates });
  81. }
  82. self.addEventListener('message', event => {
  83. const { code } = event.data;
  84. processDefinition(code);
  85. });