@grafana/plugin-meta-extractor 0.0.1 → 0.0.2-canary.902.bd718a3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utils.js CHANGED
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.debug = exports.parseExtensionPointOptions = exports.parseExtensionPointId = exports.parseString = exports.getExtensionConfigs = exports.getComponentExtensionConfigs = exports.getLinkExtensionsConfigs = void 0;
30
30
  const ts = __importStar(require("typescript"));
31
31
  const debug_1 = __importDefault(require("debug"));
32
- const tsquery = __importStar(require("@phenomnomnominal/tsquery"));
33
32
  const types_1 = require("./types");
34
33
  const _debug = (0, debug_1.default)('plugin-meta-extractor');
35
34
  const CONFIGURE_FN_NAME_TO_TYPE = {
@@ -45,7 +44,7 @@ function getComponentExtensionConfigs(ast, checker) {
45
44
  }
46
45
  exports.getComponentExtensionConfigs = getComponentExtensionConfigs;
47
46
  function getExtensionConfigs(functionName, ast, checker) {
48
- const identifiers = tsquery.query(ast, `Identifier[name=${functionName}]`);
47
+ const identifiers = query(ast, createIdentifierMatcher(functionName));
49
48
  const extensionConfigs = [];
50
49
  for (const identifier of identifiers) {
51
50
  const callExpression = identifier.parent.parent;
@@ -63,6 +62,40 @@ function getExtensionConfigs(functionName, ast, checker) {
63
62
  return extensionConfigs;
64
63
  }
65
64
  exports.getExtensionConfigs = getExtensionConfigs;
65
+ function createIdentifierMatcher(functionName) {
66
+ return (node) => {
67
+ if (!ts.isIdentifier(node)) {
68
+ return false;
69
+ }
70
+ return node.escapedText === functionName;
71
+ };
72
+ }
73
+ function query(node, findMatches) {
74
+ const results = [];
75
+ traverse(node, (childNode, ancestry) => {
76
+ if (findMatches(childNode, ancestry)) {
77
+ results.push(childNode);
78
+ }
79
+ });
80
+ return results;
81
+ }
82
+ function traverse(node, iterator, ancestors = []) {
83
+ if (node.parent != null) {
84
+ ancestors.unshift(node.parent);
85
+ }
86
+ iterator(node, ancestors);
87
+ let children = [];
88
+ try {
89
+ // We need to use `getChildren()` to traverse JSDoc nodes
90
+ children = node.getChildren();
91
+ }
92
+ catch {
93
+ // but it will fail for synthetic nodes, in which case we fall back:
94
+ node.forEachChild((child) => traverse(child, iterator, ancestors));
95
+ }
96
+ children.forEach((child) => traverse(child, iterator, ancestors));
97
+ ancestors.shift();
98
+ }
66
99
  function parseString(node) {
67
100
  // If value is simple string
68
101
  if (ts.isStringLiteral(node)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/plugin-meta-extractor",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-canary.902.bd718a3.0",
4
4
  "description": "Extract meta information from a Grafana plugin source code.",
5
5
  "types": "./dist/index.d.ts",
6
6
  "bin": "./dist/bin/run.js",
@@ -32,9 +32,8 @@
32
32
  "url": "https://github.com/grafana/plugin-tools/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@phenomnomnominal/tsquery": "^6.1.3",
36
35
  "debug": "^4.3.4",
37
- "typescript": "^5.3.3"
36
+ "typescript": "^5.4.5"
38
37
  },
39
38
  "devDependencies": {
40
39
  "@grafana/data": "^10.4.1",
@@ -44,5 +43,5 @@
44
43
  "react": "^18.2.0",
45
44
  "ts-jest": "^29.1.2"
46
45
  },
47
- "gitHead": "5533099402b7b5f626223a8d58592984c45b13eb"
46
+ "gitHead": "bd718a345d9eaf999d039d8eaac6aa41a27b0542"
48
47
  }
@@ -33,7 +33,6 @@ export function extractPluginExtensions(entry: string): PluginExtensionMeta[] {
33
33
 
34
34
  const linkExtensionConfigs = getLinkExtensionsConfigs(appNode, checker);
35
35
  const componentExtensionConfigs = getComponentExtensionConfigs(appNode, checker);
36
-
37
36
  return [...linkExtensionConfigs, ...componentExtensionConfigs];
38
37
  }
39
38
 
package/src/utils.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as ts from 'typescript';
2
2
  import createDebug from 'debug';
3
- import * as tsquery from '@phenomnomnominal/tsquery';
4
3
  import { PluginExtensionTypes } from './types';
5
4
 
6
5
  const _debug = createDebug('plugin-meta-extractor');
@@ -19,7 +18,7 @@ export function getComponentExtensionConfigs(ast: ts.Node, checker: ts.TypeCheck
19
18
  }
20
19
 
21
20
  export function getExtensionConfigs(functionName: string, ast: ts.Node, checker: ts.TypeChecker) {
22
- const identifiers = tsquery.query(ast, `Identifier[name=${functionName}]`) as ts.Identifier[];
21
+ const identifiers = query(ast, createIdentifierMatcher(functionName));
23
22
  const extensionConfigs = [];
24
23
 
25
24
  for (const identifier of identifiers) {
@@ -39,6 +38,48 @@ export function getExtensionConfigs(functionName: string, ast: ts.Node, checker:
39
38
  return extensionConfigs;
40
39
  }
41
40
 
41
+ function createIdentifierMatcher(functionName: string): (node: ts.Node) => boolean {
42
+ return (node) => {
43
+ if (!ts.isIdentifier(node)) {
44
+ return false;
45
+ }
46
+ return node.escapedText === functionName;
47
+ };
48
+ }
49
+
50
+ function query(node: ts.Node, findMatches: (node: ts.Node, ancestry: ts.Node[]) => boolean) {
51
+ const results: ts.Node[] = [];
52
+
53
+ traverse(node, (childNode: ts.Node, ancestry: ts.Node[]) => {
54
+ if (findMatches(childNode, ancestry)) {
55
+ results.push(childNode);
56
+ }
57
+ });
58
+
59
+ return results;
60
+ }
61
+
62
+ function traverse(
63
+ node: ts.Node,
64
+ iterator: (node: ts.Node, ancestors: ts.Node[]) => void,
65
+ ancestors: ts.Node[] = []
66
+ ): void {
67
+ if (node.parent != null) {
68
+ ancestors.unshift(node.parent);
69
+ }
70
+ iterator(node, ancestors);
71
+ let children: ts.Node[] = [];
72
+ try {
73
+ // We need to use `getChildren()` to traverse JSDoc nodes
74
+ children = node.getChildren();
75
+ } catch {
76
+ // but it will fail for synthetic nodes, in which case we fall back:
77
+ node.forEachChild((child) => traverse(child, iterator, ancestors));
78
+ }
79
+ children.forEach((child) => traverse(child, iterator, ancestors));
80
+ ancestors.shift();
81
+ }
82
+
42
83
  export function parseString(node: ts.Expression): string {
43
84
  // If value is simple string
44
85
  if (ts.isStringLiteral(node)) {