Skip to content
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

🚨 enforce scripts/ files conventions #3022

Merged
merged 8 commits into from
Oct 1, 2024
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ module.exports = {
rules: {
'unicorn/filename-case': ['error', { case: 'kebabCase' }],
'local-rules/secure-command-execution': 'error',
'local-rules/disallow-non-scripts': 'error',
},
},
{
Expand Down
29 changes: 29 additions & 0 deletions eslint-local-rules/disallowNonScripts.js
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 thought: ‏ Doesn't seems to be needed, but we could also enforce that scripts don't export anything (because whatever is exported should be in a lib)
Reciprocally, files in lib folders should not contain runMain and have at least one export.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
meta: {
docs: {
description: 'Disallow JS files that are not used as a "script"',
},
schema: [],
},
create: (context) => ({
Program: (node) => {
if (!node.body.some(isMain)) {
context.report({
node,
message: 'This file should be a script and contain a `runMain()` expression',
})
}
},
}),
}

/**
* Check if the node is like `runMain(fn)`
*/
function isMain(node) {
return (
node.type === 'ExpressionStatement' &&
node.expression.type === 'CallExpression' &&
node.expression.callee.name === 'runMain'
)
}
1 change: 1 addition & 0 deletions eslint-local-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
'disallow-zone-js-patched-values': require('./disallowZoneJsPatchedValues'),
'disallow-url-constructor-patched-values': require('./disallowUrlConstructorPatchValues.js'),
'disallow-generic-utils': require('./disallowGenericUtils'),
'disallow-non-scripts': require('./disallowNonScripts'),
'secure-command-execution': require('./secureCommandExecution'),
}