-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Custom construct ID check (#305)
* Implement custom construct ID check to avoid tokens in CDK ID
- Loading branch information
Dmitry Balabanov
authored
Dec 20, 2023
1 parent
ee444b8
commit d573044
Showing
10 changed files
with
124 additions
and
6 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
require("ts-node").register({ | ||
transpileOnly: true, | ||
compilerOptions: { | ||
module: "commonjs", | ||
}, | ||
}); | ||
|
||
module.exports = require("./rules").default; |
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,59 @@ | ||
// Put the following comment above a code line you want to exclude from this check, e.g. after review: | ||
// eslint-disable-next-line local-rules/no-tokens-in-construct-id | ||
|
||
import type { Rule } from "eslint"; | ||
import * as ts from "typescript"; | ||
|
||
export default { | ||
"no-tokens-in-construct-id": { | ||
meta: { | ||
docs: { | ||
description: 'Checks whether a CDK token might appear in a construct ID on instantiation', | ||
recommended: true | ||
}, | ||
schema: [], | ||
}, | ||
create: function (context) { | ||
return { | ||
NewExpression: function (node) { | ||
if (node.callee.type != 'Identifier') { | ||
return; | ||
} | ||
|
||
const typeChecker = context.parserServices.program.getTypeChecker(); | ||
const typescriptNode = <ts.Node>context.parserServices.esTreeNodeToTSNodeMap.get(node); | ||
const instantiatedType = <ts.Type>typeChecker.getTypeAtLocation(typescriptNode); | ||
const baseTypes = instantiatedType.getBaseTypes(); | ||
|
||
if (instantiatedType.symbol.name == 'Construct' || baseTypes && baseTypes.some(t => t.symbol.name == 'Construct')) { | ||
if (node.arguments.length <= 1) { | ||
// Non-standard schema, expected at least 2 parameters | ||
return; | ||
} | ||
|
||
// We expect the second argument to be the ID | ||
const secondArgument = node.arguments[1]; | ||
|
||
if (secondArgument.type == 'Literal') { | ||
if (secondArgument.value?.toString().startsWith('${TOKEN')) { | ||
context.report({ | ||
node, | ||
message: 'The ID argument of a construct contains a token, this is not allowed.' | ||
}); | ||
} | ||
|
||
// No further check needed for literals | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: 'An expression is used for the construct ID. it might contain tokens, please review. ' + | ||
'Suppress this finding by putting this comment above the code line: // eslint-disable-next-line local-rules/no-tokens-in-construct-id' | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}, | ||
} satisfies Record<string, Rule.RuleModule>; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.