"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, configurable: true, get: () => obj[key]}); }); } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts var _napi = require('@ast-grep/napi'); _createStarExport(_napi); // src/check.ts function isTypeOf(node, types) { types = Array.isArray(types) ? types : [types]; return !!node && types.includes(node.kind()); } function isCallOf(node, test) { if (!node || node.kind() !== "call_expression") return false; const functionFieldText = _optionalChain([node, 'access', _ => _.field, 'call', _2 => _2("function"), 'optionalAccess', _3 => _3.text, 'call', _4 => _4()]); return !!functionFieldText && (typeof test === "string" ? functionFieldText === test : Array.isArray(test) ? test.includes(functionFieldText) : test(functionFieldText)); } function isIdentifierOf(node, test) { return !!node && node.kind() === "identifier" && (typeof test === "string" ? node.text() === test : test.includes(node.text())); } // src/parse.ts var LangMap = { html: _napi.html, css: _napi.css, js: _napi.js, jsx: _napi.jsx, ts: _napi.ts, tsx: _napi.tsx }; function sgParse(code, lang = "ts") { const instance = LangMap[lang]; return instance.parse(code).root(); } // src/walk.ts function walkImportDeclaration(imports, node) { const importStats = node.findAll(_napi.ts.kind("import_statement")); for (const stat of importStats) { const sourceFragment = stat.find(_napi.ts.kind("string_fragment")); const importClause = stat.find(_napi.ts.kind("import_clause")); if (!sourceFragment || !importClause) { continue; } const bindings = []; const source = sourceFragment.text(); const statText = stat.text(); let isType = statText.startsWith("import type "); const defaultImport = importClause.find({ rule: { kind: "identifier", inside: { kind: "import_clause" } } }); if (defaultImport) { bindings.push({ imported: "default", local: defaultImport.text(), isType }); } const namespaceImport = importClause.find({ rule: { kind: "identifier", inside: { kind: "namespace_import" } } }); if (namespaceImport) { bindings.push({ imported: "*", local: namespaceImport.text(), isType }); } const specifiers = importClause.findAll({ rule: { kind: "import_specifier", inside: { kind: "named_imports" } } }); specifiers.forEach((s) => { if (!isType) { isType = s.text().startsWith("type "); } const nameSpecifier = s.field("name"); const aliasSpecifier = s.field("alias"); bindings.push({ imported: nameSpecifier.text(), local: aliasSpecifier ? aliasSpecifier.text() : nameSpecifier.text(), isType }); }); imports[source] = bindings; } return imports; } exports.isCallOf = isCallOf; exports.isIdentifierOf = isIdentifierOf; exports.isTypeOf = isTypeOf; exports.sgParse = sgParse; exports.walkImportDeclaration = walkImportDeclaration;