next-routes-readme
Advanced tools
Comparing version 0.0.3 to 0.0.4
133
lib/index.js
#!/usr/bin/env node | ||
"use strict"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -9,2 +33,5 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const path_1 = __importDefault(require("path")); | ||
const parser = __importStar(require("@babel/parser")); | ||
const traverse_1 = __importDefault(require("@babel/traverse")); | ||
const t = __importStar(require("@babel/types")); | ||
function parseRouteHandlers(routeFile) { | ||
@@ -15,51 +42,65 @@ const content = fs_1.default.readFileSync(routeFile, 'utf-8'); | ||
let currentHandler = null; | ||
const lines = content.split('\n'); | ||
for (const line of lines) { | ||
const trimmedLine = line.trim(); | ||
const currentLineNumber = lines.indexOf(line) + 1; | ||
if (trimmedLine.startsWith('import {')) { | ||
dependencies.push(trimmedLine); | ||
} | ||
const match = /export async function (\w+)\(([\w: ,]*)\)/.exec(trimmedLine); | ||
if (match) { | ||
currentHandler = { | ||
implementation: match[0], | ||
name: match[1], | ||
method: 'GET', | ||
doc: { variables: [], conditionals: [], errors: [], comments: [] }, | ||
dependencies, | ||
}; | ||
} | ||
if (currentHandler && trimmedLine.includes('=')) { | ||
currentHandler.doc.variables.push({ value: trimmedLine, line: currentLineNumber }); | ||
} | ||
if (currentHandler && (trimmedLine.includes('if') || trimmedLine.includes('?'))) { | ||
currentHandler.doc.conditionals.push({ value: trimmedLine, line: currentLineNumber }); | ||
} | ||
if (currentHandler && | ||
(trimmedLine.includes('throw') || | ||
trimmedLine.includes('new Error') || | ||
trimmedLine.includes('Promise.reject'))) { | ||
currentHandler.doc.errors.push({ value: trimmedLine, line: currentLineNumber }); | ||
} | ||
if (currentHandler && trimmedLine.startsWith('//')) { | ||
currentHandler.doc.comments.push({ value: trimmedLine, line: currentLineNumber }); | ||
} | ||
if (currentHandler && trimmedLine.startsWith('/*') && trimmedLine.endsWith('*/')) { | ||
currentHandler.doc.comments.push({ value: trimmedLine, line: currentLineNumber }); | ||
} | ||
if (currentHandler && trimmedLine.startsWith('/*') && !trimmedLine.endsWith('*/')) { | ||
const commentInLines = { value: trimmedLine, line: currentLineNumber }; | ||
for (let i = currentLineNumber; i < lines.length; i++) { | ||
const nextLine = lines[i].trim(); | ||
commentInLines.value += '\n' + nextLine; | ||
if (nextLine.endsWith('*/')) { | ||
break; | ||
const ast = parser.parse(content, { | ||
sourceType: 'module', | ||
plugins: ['jsx', 'typescript'], | ||
}); | ||
(0, traverse_1.default)(ast, { | ||
enter(path) { | ||
var _a; | ||
if (t.isImportDeclaration(path.node)) { | ||
dependencies.push(path.toString()); | ||
} | ||
else if (t.isFunctionDeclaration(path.node) && path.node.async) { | ||
const { name } = path.node.id; | ||
const params = path.node.params | ||
.map(param => content.substring(param.start, param.end)) | ||
.join(', '); | ||
const returnType = path.node.returnType | ||
? content.substring(path.node.returnType.start, path.node.returnType.end) | ||
: ''; | ||
currentHandler = { | ||
implementation: `async function ${name}(${params})${returnType}`, | ||
name, | ||
method: 'GET', | ||
doc: { | ||
variables: [], | ||
conditionals: [], | ||
errors: [], | ||
comments: [], | ||
}, | ||
dependencies, | ||
}; | ||
if (path.node.leadingComments) { | ||
path.node.leadingComments.forEach(comment => { | ||
const value = content.substring(comment.start, comment.end); | ||
currentHandler.doc.comments.push({ value, line: comment.loc.start.line }); | ||
}); | ||
} | ||
} | ||
currentHandler.doc.comments.push(commentInLines); | ||
} | ||
} | ||
else if (currentHandler) { | ||
const currentLineNumber = ((_a = path.node.loc) === null || _a === void 0 ? void 0 : _a.start.line) || 0; | ||
if (t.isVariableDeclaration(path.node)) { | ||
const value = content.substring(path.node.start, path.node.end); | ||
currentHandler.doc.variables.push({ value, line: currentLineNumber }); | ||
} | ||
else if (t.isIfStatement(path.node) || t.isConditionalExpression(path.node)) { | ||
const value = content.substring(path.node.start, path.node.end); | ||
currentHandler.doc.conditionals.push({ value, line: currentLineNumber }); | ||
} | ||
else if (t.isThrowStatement(path.node) || | ||
t.isNewExpression(path.node) || | ||
t.isCallExpression(path.node)) { | ||
const value = content.substring(path.node.start, path.node.end); | ||
currentHandler.doc.errors.push({ value, line: currentLineNumber }); | ||
} | ||
else if (path.node.leadingComments) { | ||
path.node.leadingComments.forEach(comment => { | ||
const value = content.substring(comment.start, comment.end); | ||
currentHandler.doc.comments.push({ value, line: comment.loc.start.line }); | ||
}); | ||
} | ||
} | ||
}, | ||
}); | ||
if (currentHandler) { | ||
currentHandler.dependencies = dependencies; | ||
handlers.push(currentHandler); | ||
@@ -66,0 +107,0 @@ } |
{ | ||
"name": "next-routes-readme", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Creates README badges from istanbul coverage report", | ||
"type": "commonjs", | ||
"scripts": { | ||
@@ -60,3 +61,8 @@ "build": "rm -rf lib/ && tsc", | ||
"typescript": "^5.2.2" | ||
}, | ||
"dependencies": { | ||
"@babel/parser": "^7.23.0", | ||
"@babel/traverse": "^7.23.0", | ||
"@babel/types": "^7.23.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13750
143
3
+ Added@babel/parser@^7.23.0
+ Added@babel/traverse@^7.23.0
+ Added@babel/types@^7.23.0
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/generator@7.26.3(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/parser@7.26.3(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.26.4(transitive)
+ Added@babel/types@7.26.3(transitive)
+ Added@jridgewell/gen-mapping@0.3.8(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.1.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedpicocolors@1.1.1(transitive)