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

Limit top level functions in notebook webview preloads #229998

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .eslintplugin/code-limited-top-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as eslint from 'eslint';
import { dirname, relative } from 'path';
import minimatch from 'minimatch';

export = new class implements eslint.Rule.RuleModule {

readonly meta: eslint.Rule.RuleMetaData = {
messages: {
layerbreaker: 'You are only allowed to define limited top level functions.'
},
schema: {
type: "array",
items: {
type: "object",
additionalProperties: {
type: "array",
items: {
type: "string"
}
}
}
}
};

create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
let fileRelativePath = relative(dirname(__dirname), context.getFilename());
if (!fileRelativePath.endsWith('/')) {
fileRelativePath += '/';
}
const ruleArgs = <Record<string, string[]>>context.options[0];

const matchingKey = Object.keys(ruleArgs).find(key => fileRelativePath.startsWith(key) || minimatch(fileRelativePath, key));
if (!matchingKey) {
// nothing
return {};
}

const restrictedFunctions = ruleArgs[matchingKey];

return {
FunctionDeclaration: (node: any) => {
const isTopLevel = node.parent.type === 'Program';
const functionName = node.id.name;
if (isTopLevel && !restrictedFunctions.includes(node.id.name)) {
context.report({
node,
message: `Top-level function '${functionName}' is restricted in this file. Allowed functions are: ${restrictedFunctions.join(', ')}.`
});
}
},
ExportNamedDeclaration(node: any) {
if (node.declaration && node.declaration.type === 'FunctionDeclaration') {
const functionName = node.declaration.id.name;
const isTopLevel = node.parent.type === 'Program';
if (isTopLevel && !restrictedFunctions.includes(node.declaration.id.name)) {
context.report({
node,
message: `Top-level function '${functionName}' is restricted in this file. Allowed functions are: ${restrictedFunctions.join(', ')}.`
});
}
}
}
}
}
};
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,15 @@
"**/*"
]
}
],
"local/code-limited-top-functions": [
"error",
{
"src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts": [
"webviewPreloads",
"preloadsScriptStr"
]
}
]
}
}
Expand Down
Loading