-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
harden-exports rule for eslint plugin #2369
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* @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', | ||
docs: { | ||
description: | ||
'Ensure each named export is followed by a call to `harden` function', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
}, | ||
/** | ||
* 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) { | ||
exportNames.push(prop.key.name); | ||
} | ||
} else { | ||
exportNames.push(declaration.id.name); | ||
} | ||
} | ||
} else if (exportNode.declaration.type === 'FunctionDeclaration') { | ||
// @ts-expect-error xxx typedef | ||
exportNames.push(exportNode.declaration.id.name); | ||
} | ||
} else if (exportNode.specifiers) { | ||
for (const spec of exportNode.specifiers) { | ||
exportNames.push(spec.exported.name); | ||
} | ||
} | ||
|
||
const missingHardenCalls = []; | ||
for (const exportName of exportNames) { | ||
const hasHardenCall = sourceCode.ast.body.some(statement => { | ||
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 | ||
); | ||
}); | ||
|
||
if (!hasHardenCall) { | ||
missingHardenCalls.push(exportName); | ||
} | ||
} | ||
|
||
if (missingHardenCalls.length > 0) { | ||
const noun = missingHardenCalls.length === 1 ? 'export' : 'exports'; | ||
context.report({ | ||
node: exportNode, | ||
message: `The named ${noun} '${missingHardenCalls.join(', ')}' should be followed by a call to 'harden'.`, | ||
fix: function (fixer) { | ||
const hardenCalls = missingHardenCalls | ||
.map(name => `harden(${name});`) | ||
.join('\n'); | ||
return fixer.insertTextAfter(exportNode, `\n${hardenCalls}`); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
const { RuleTester } = require('eslint'); | ||
const rule = require('../lib/rules/harden-exports'); | ||
|
||
const jsValid = [ | ||
{ | ||
code: ` | ||
export const a = 1; | ||
harden(a); | ||
export const b = 2; | ||
harden(b); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const a = 1; | ||
harden(a); | ||
export const b = 2; | ||
harden(b); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export function foo() { | ||
console.log("foo"); | ||
} | ||
harden(foo); | ||
export const a = 1; | ||
harden(a); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const a = 1; | ||
harden(a); | ||
export function bar() { | ||
console.log("bar"); | ||
} | ||
harden(bar); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const a = 1; | ||
harden(a); | ||
export function | ||
multilineFunction() { | ||
console.log("This is a multiline function."); | ||
} | ||
harden(multilineFunction); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const { | ||
getEnvironmentOption, | ||
getEnvironmentOptionsList, | ||
environmentOptionsListHas, | ||
} = makeEnvironmentCaptor(); | ||
harden(getEnvironmentOption); | ||
harden(getEnvironmentOptionsList); | ||
harden(environmentOptionsListHas); | ||
`, | ||
}, | ||
]; | ||
|
||
const invalid = [ | ||
{ | ||
code: ` | ||
export const a = 'alreadyHardened'; | ||
export const b = 'toHarden'; | ||
|
||
harden(a); | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named export 'b' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export const a = 'alreadyHardened'; | ||
export const b = 'toHarden'; | ||
harden(b); | ||
|
||
harden(a); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const a = 1; | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named export 'a' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export const a = 1; | ||
harden(a); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export function foo() { | ||
console.log("foo"); | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named export 'foo' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export function foo() { | ||
console.log("foo"); | ||
} | ||
harden(foo); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export function | ||
multilineFunction() { | ||
console.log("This is a multiline function."); | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named export 'multilineFunction' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export function | ||
multilineFunction() { | ||
console.log("This is a multiline function."); | ||
} | ||
harden(multilineFunction); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const a = 1; | ||
export const b = 2; | ||
|
||
export const alreadyHardened = 3; | ||
harden(alreadyHardened); | ||
|
||
export function foo() { | ||
console.log("foo"); | ||
} | ||
export function | ||
multilineFunction() { | ||
console.log("This is a multiline function."); | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named export 'a' should be followed by a call to 'harden'.", | ||
}, | ||
{ | ||
message: | ||
"The named export 'b' should be followed by a call to 'harden'.", | ||
}, | ||
{ | ||
message: | ||
"The named export 'foo' should be followed by a call to 'harden'.", | ||
}, | ||
{ | ||
message: | ||
"The named export 'multilineFunction' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export const a = 1; | ||
harden(a); | ||
export const b = 2; | ||
harden(b); | ||
|
||
export const alreadyHardened = 3; | ||
harden(alreadyHardened); | ||
|
||
export function foo() { | ||
console.log("foo"); | ||
} | ||
harden(foo); | ||
export function | ||
multilineFunction() { | ||
console.log("This is a multiline function."); | ||
} | ||
harden(multilineFunction); | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const { | ||
getEnvironmentOption, | ||
getEnvironmentOptionsList, | ||
environmentOptionsListHas, | ||
} = makeEnvironmentCaptor(); | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"The named exports 'getEnvironmentOption, getEnvironmentOptionsList, environmentOptionsListHas' should be followed by a call to 'harden'.", | ||
}, | ||
], | ||
output: ` | ||
export const { | ||
getEnvironmentOption, | ||
getEnvironmentOptionsList, | ||
environmentOptionsListHas, | ||
} = makeEnvironmentCaptor(); | ||
harden(getEnvironmentOption); | ||
harden(getEnvironmentOptionsList); | ||
harden(environmentOptionsListHas); | ||
`, | ||
}, | ||
]; | ||
|
||
const jsTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}); | ||
jsTester.run('harden JS exports', rule, { | ||
valid: jsValid, | ||
invalid, | ||
}); | ||
|
||
const tsTester = new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}); | ||
tsTester.run('harden TS exports', rule, { | ||
valid: [ | ||
...jsValid, | ||
{ | ||
// harden() on only value exports | ||
code: ` | ||
export type Foo = string; | ||
export interface Bar { | ||
baz: number; | ||
} | ||
`, | ||
}, | ||
], | ||
invalid, | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a handful of cases, it will be somewhat tedious to have to harden primitive literals. Are you up for a little complication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm all ears. I'd like to land this though because it's useful as is