-
Notifications
You must be signed in to change notification settings - Fork 132
chore: adds eslint rule to restrict import from config file MCP-130 #473
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41c1391
chore: adds eslint rule to restrict import from config file
himanshusinghs c28045e
chore: refactor Transport implementations to accept driverOptions
himanshusinghs 2c1c3be
chore: updated description
himanshusinghs 35f2cd5
chore: check fixes
himanshusinghs d758629
chore: add check for options
himanshusinghs fdcbcfc
chore: add missing use strict directive
himanshusinghs 6aac510
chore: make no-config-imports rule tied to config itself
himanshusinghs 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 hidden or 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,71 @@ | ||
"use strict"; | ||
import path from "path"; | ||
|
||
// The file from which we wish to discourage importing values | ||
const configFilePath = path.resolve(import.meta.dirname, "../src/common/config.js"); | ||
|
||
// Files that are allowed to import value exports from config.ts | ||
const allowedConfigValueImportFiles = [ | ||
// Main entry point that injects the config | ||
"src/index.ts", | ||
// Config resource definition that works with the some config values | ||
"src/resources/common/config.ts", | ||
]; | ||
|
||
// Ref: https://eslint.org/docs/latest/extend/custom-rules | ||
export default { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"Disallows value imports from config.ts, with a few exceptions, to enforce dependency injection of the config.", | ||
recommended: true, | ||
}, | ||
fixable: null, | ||
messages: { | ||
noConfigImports: | ||
"Value imports from config.ts are not allowed. Use dependency injection instead. Only type imports are permitted.", | ||
kmruiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
create(context) { | ||
const currentFilePath = path.resolve(context.getFilename()); | ||
|
||
const isCurrentFileAllowedToImport = allowedConfigValueImportFiles.some((allowedFile) => { | ||
const resolvedAllowedFile = path.resolve(allowedFile); | ||
return currentFilePath === resolvedAllowedFile; | ||
}); | ||
|
||
if (isCurrentFileAllowedToImport) { | ||
return {}; | ||
} | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const importPath = node.source.value; | ||
|
||
// If the path is not relative, very likely its targeting a | ||
// node_module so we skip it. And also if the entire import is | ||
// marked with a type keyword. | ||
if (typeof importPath !== "string" || !importPath.startsWith(".") || node.importKind === "type") { | ||
return; | ||
} | ||
|
||
const currentDir = path.dirname(currentFilePath); | ||
const resolvedImportPath = path.resolve(currentDir, importPath); | ||
|
||
if (resolvedImportPath === configFilePath) { | ||
const hasValueImportFromConfig = node.specifiers.some((specifier) => { | ||
return specifier.importKind !== "type"; | ||
}); | ||
|
||
if (hasValueImportFromConfig) { | ||
context.report({ | ||
node, | ||
messageId: "noConfigImports", | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains hidden or 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,69 @@ | ||
import path from "path"; | ||
import { RuleTester } from "eslint"; | ||
import { describe, it } from "vitest"; | ||
import tsParser from "@typescript-eslint/parser"; | ||
import rule from "./no-config-imports.js"; | ||
|
||
const ROOT = process.cwd(); | ||
const resolve = (p) => path.resolve(ROOT, p); | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
parser: tsParser, | ||
parserOptions: { ecmaVersion: 2022, sourceType: "module" }, | ||
}, | ||
}); | ||
|
||
describe("no-config-imports", () => { | ||
it("should not report any violations", () => { | ||
ruleTester.run("no-config-imports", rule, { | ||
valid: [ | ||
{ | ||
filename: resolve("src/some/module.ts"), | ||
code: 'import type { UserConfig } from "../common/config.js";\n', | ||
}, | ||
{ | ||
filename: resolve("src/some/module.ts"), | ||
code: 'import { something } from "../common/logger.js";\n', | ||
}, | ||
{ | ||
filename: resolve("src/some/module.ts"), | ||
code: 'import type * as Cfg from "../common/config.js";\n', | ||
}, | ||
{ | ||
filename: resolve("src/index.ts"), | ||
code: 'import { driverOptions } from "../common/config.js";\n', | ||
}, | ||
], | ||
invalid: [], | ||
}); | ||
}); | ||
|
||
it("should report rule violations", () => { | ||
ruleTester.run("no-config-imports", rule, { | ||
valid: [], | ||
invalid: [ | ||
{ | ||
filename: resolve("src/another/module.ts"), | ||
code: 'import { driverOptions } from "../common/config.js";\n', | ||
errors: [{ messageId: "noConfigImports" }], | ||
}, | ||
{ | ||
filename: resolve("src/another/module.ts"), | ||
code: 'import configDefault from "../common/config.js";\n', | ||
errors: [{ messageId: "noConfigImports" }], | ||
}, | ||
{ | ||
filename: resolve("src/another/module.ts"), | ||
code: 'import * as cfg from "../common/config.js";\n', | ||
errors: [{ messageId: "noConfigImports" }], | ||
}, | ||
{ | ||
filename: resolve("src/another/module.ts"), | ||
code: 'import { type UserConfig, driverOptions } from "../common/config.js";\n', | ||
errors: [{ messageId: "noConfigImports" }], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.