Skip to content

Commit

Permalink
feat(harden-exports): handle TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jul 18, 2024
1 parent b07a159 commit 387409c
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 154 deletions.
33 changes: 30 additions & 3 deletions packages/eslint-plugin/lib/rules/harden-exports.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* @fileoverview Ensure each named export is followed by a call to `harden` function
*/

'use strict';

/**
* @import {Rule} from 'eslint';
* @import * as ESTree from 'estree';
*/

/**
* ESLint rule module for ensuring each named export is followed by a call to `harden` function.
* @type {Rule.RuleModule}
*/
module.exports = {
meta: {
type: 'problem',
Expand All @@ -10,20 +25,30 @@ module.exports = {
fixable: 'code',
schema: [],
},
create: function (context) {
/**
* Create function for the rule.
* @param {Rule.RuleContext} context - The rule context.
* @returns {Object} The visitor object.
*/
create(context) {
/** @type {Array<ESTree.ExportNamedDeclaration & Rule.NodeParentExtension>} */
let exportNodes = [];

return {
/** @param {ESTree.ExportNamedDeclaration & Rule.NodeParentExtension} node */
ExportNamedDeclaration(node) {
exportNodes.push(node);
},
'Program:exit'() {
const sourceCode = context.getSourceCode();

for (const exportNode of exportNodes) {
/** @type {string[]} */
let exportNames = [];
if (exportNode.declaration) {
// @ts-expect-error xxx typedef
if (exportNode.declaration.declarations) {
// @ts-expect-error xxx typedef
for (const declaration of exportNode.declaration.declarations) {
if (declaration.id.type === 'ObjectPattern') {
for (const prop of declaration.id.properties) {
Expand All @@ -33,8 +58,8 @@ module.exports = {
exportNames.push(declaration.id.name);
}
}
} else {
// Handling function exports
} else if (exportNode.declaration.type === 'FunctionDeclaration') {
// @ts-expect-error xxx typedef
exportNames.push(exportNode.declaration.id.name);
}
} else if (exportNode.specifiers) {
Expand All @@ -49,8 +74,10 @@ module.exports = {
return (
statement.type === 'ExpressionStatement' &&
statement.expression.type === 'CallExpression' &&
// @ts-expect-error xxx typedef
statement.expression.callee.name === 'harden' &&
statement.expression.arguments.length === 1 &&
// @ts-expect-error xxx typedef
statement.expression.arguments[0].name === exportName
);
});
Expand Down
Loading

0 comments on commit 387409c

Please sign in to comment.