Skip to content

Commit

Permalink
generate cjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
cedeber committed Sep 13, 2024
1 parent be5f3fa commit 09b5abe
Show file tree
Hide file tree
Showing 16 changed files with 349 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
node_modules
/cjs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/tests
eslint.config.js
prettier.config.js
tsconfig.json
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"editor.formatOnSave": true
"editor.formatOnSave": true,
"cSpell.words": ["estree"]
}
7 changes: 5 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import globals from "globals";
import pluginJs from "@eslint/js";
import eslintPlugin from "eslint-plugin-eslint-plugin";
import nodePlugin from "eslint-plugin-n";
import avaPlugin from "eslint-plugin-ava";

/** @type import("eslint").Linter.Config[] */
export default [
Expand All @@ -15,8 +16,10 @@ export default [
"n/exports-style": ["error", "module.exports"],
},
},
{ languageOptions: { ecmaVersion: 2020, sourceType: "module" } },
{
languageOptions: { ecmaVersion: 2021, sourceType: "module" },
ignores: ["/examples"],
files: ["tests/*.js"],
...avaPlugin.configs["flat/recommended"],
},
{ ignores: ["node_modules", "cjs", "examples"] },
];
2 changes: 1 addition & 1 deletion examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions lib/index.mjs → lib/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import fs from "fs";
import throwDocumentation from "./rules/require-throws-doc.mjs";
import throwNaming from "./rules/throw-function-naming.mjs";
import { readFileSync } from "fs";
import throwDocumentation from "./rules/require-throws-doc.js";
import throwNaming from "./rules/throw-function-naming.js";

const pkg = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));

/**
* @type {import("eslint").ESLint.Plugin}
*/
/** @type {import("eslint").ESLint.Plugin} */
const plugin = {
meta: {
name: pkg.name,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { getThrowTypes } from "../utils.mjs";
import { getThrowTypes } from "../utils.js";

/**
* @type {import("eslint").Rule.RuleModule}
*/
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "suggestion",
docs: {
description: "enforce JSDoc @throws tag for functions that throw exceptions",
category: "Best Practices",
recommended: "warn",
recommended: true,
},
messages: {
missingThrows: "Function throws '{{type}}' but lacks a @throws tag in JSDoc.",
Expand All @@ -18,7 +16,7 @@ export default {
fixable: "code",
},
create(context) {
/** @param {(import("estree").ArrowFunctionExpression | (import("estree").FunctionDeclaration)) & import("eslint").Rule.NodeParentExtension} node */
/** @param {(import("estree").ArrowFunctionExpression | import("estree").FunctionExpression | import("estree").FunctionDeclaration) & import("eslint").Rule.NodeParentExtension} node */
function checkFunctionForThrowsTag(node) {
const sourceCode = context.sourceCode;
const jsDocComment = sourceCode.getJSDocComment(node);
Expand All @@ -28,7 +26,7 @@ export default {
// Missing JSDoc @throws
if (!jsDocComment) {
context.report({
node: node,
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
fix(fixer) {
Expand All @@ -41,7 +39,7 @@ export default {

// Calculate indentation
const line = sourceCode.lines[node.loc.start.line - 1];
const indentation = line.match(/^\s*/)[0];
const indentation = line.match(/^\s*/)?.[0];

const jsDoc = `/**\n${indentation} * @throws {${Array.from(throwTypes).join(", ")}}\n${indentation} */\n${indentation}`;

Expand All @@ -58,15 +56,15 @@ export default {
const isMultiLine = jsDocValue.includes("\n");

context.report({
node: node,
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
fix(fixer) {
const jsDoc = `@throws {${Array.from(throwTypes).join(", ")}}`;

// Calculate indentation
const line = sourceCode.lines[node.loc.start.line - 1];
const indentation = line.match(/^\s*/)[0];
const indentation = line.match(/^\s*/)?.[0];

const updatedJsDoc = isMultiLine
? `${jsDocValue}* ${jsDoc}`
Expand All @@ -81,7 +79,7 @@ export default {

if (!throwsTag) {
context.report({
node: node,
node,
messageId: "missingThrows",
data: { type: Array.from(throwTypes).join(", ") },
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { hasThrowInBlock } from "../utils.mjs";
import { hasThrowInBlock } from "../utils.js";

/**
* @type {import("eslint").Rule.RuleModule}
*/
/** @type {import("eslint").Rule.RuleModule} */
export default {
meta: {
type: "suggestion",
docs: {
description:
"enforce function names to end with a specified suffix (default to 'OrThrow') if they throw exceptions",
category: "Best Practices",
recommended: "error",
recommended: true,
},
messages: {
missingSuffix:
Expand All @@ -34,7 +32,7 @@ export default {
const options = context.options[0] ?? {};
const suffix = options.suffix ?? "OrThrow";

/** @param {(import("estree").ArrowFunctionExpression | (import("estree").FunctionDeclaration)) & import("eslint").Rule.NodeParentExtension} node */
/** @param {(import("estree").ArrowFunctionExpression | import("estree").FunctionExpression | import("estree").FunctionDeclaration) & import("eslint").Rule.NodeParentExtension} node */
function checkFunctionName(node) {
const hasThrow = hasThrowInBlock(node.body.body);

Expand Down
14 changes: 11 additions & 3 deletions lib/utils.mjs → lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export function getThrowTypes(block = []) {
function checkAndAddThrowType(block = []) {
const handlerThrowTypes = getThrowTypes(block);
if (handlerThrowTypes.size > 0) {
throwTypes.add(...handlerThrowTypes);
for (const type of handlerThrowTypes) {
throwTypes.add(type);
}
}
}

Expand Down Expand Up @@ -42,7 +44,10 @@ export function getThrowTypes(block = []) {
// Handle IfStatement
if (statement.type === "IfStatement") {
checkAndAddThrowType([statement.consequent]);
statement.alternate && checkAndAddThrowType([statement.alternate]);

if (statement.alternate) {
checkAndAddThrowType([statement.alternate]);
}
}

// Handle DoWhileStatement and WhileStatement
Expand Down Expand Up @@ -76,7 +81,10 @@ export function getThrowTypes(block = []) {
return throwTypes;
}

/** @param {import("estree").Statement[]} block */
/**
* @param {import("estree").Statement[]} block
* @returns {boolean}
*/
export function hasThrowInBlock(block = []) {
if (!Array.isArray(block)) return false;

Expand Down
Loading

0 comments on commit 09b5abe

Please sign in to comment.