@stedi/eslint-plugin-stedi-aws-rules
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,8 @@ | ||
# [1.3.0](https://github.com/Stedi/eslint-plugin-stedi-aws-rules/compare/v1.2.0...v1.3.0) (2020-10-29) | ||
### Features | ||
* extend instrumented-document-clients rule, add tests ([#19](https://github.com/Stedi/eslint-plugin-stedi-aws-rules/issues/19)) ([abb165a](https://github.com/Stedi/eslint-plugin-stedi-aws-rules/commit/abb165a965676a45ba48986d823864abb0d20c38)) | ||
# [1.2.0](https://github.com/Stedi/eslint-plugin-stedi-aws-rules/compare/v1.1.2...v1.2.0) (2020-10-22) | ||
@@ -2,0 +9,0 @@ |
@@ -20,2 +20,7 @@ /** | ||
}, | ||
messages: { | ||
notInstrumented: "Called {{name}} before it was instrumented.", | ||
instrumentService: | ||
"DocumentClient should be instrumented using `service` property.", | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
@@ -28,38 +33,82 @@ schema: [ | ||
create: function (context) { | ||
var trackedVariables = [] | ||
function varInScope(x) { | ||
return context.getScope().set.get(x) | ||
let trackedVariables = new Set() | ||
/** | ||
* Returns a variable for the current scope if exists. | ||
* @param {string} name The node to report. | ||
* @returns {Variable|undefined} | ||
*/ | ||
function varInScope(name) { | ||
return context.getScope().set.get(name) | ||
} | ||
/** | ||
* Checks whether a variable is being tracked | ||
* @param {string} name Variable name | ||
* @returns {Boolean} | ||
*/ | ||
function isTracked(name) { | ||
return trackedVariables.has(varInScope(name)) | ||
} | ||
function reportInstrumentService(node) { | ||
context.report({ | ||
node, | ||
messageId: "instrumentService", | ||
}) | ||
} | ||
function checkMemberExpression(node) { | ||
const noProperty = !node.property | ||
const wrongProperty = node.property && node.property.name !== "service" | ||
if (noProperty || wrongProperty) { | ||
reportInstrumentService(node) | ||
} | ||
} | ||
function checkIdentifier(node) { | ||
if (node.name) { | ||
reportInstrumentService(node) | ||
} | ||
} | ||
return { | ||
"NewExpression[callee.property.name = 'DocumentClient']": (node) => { | ||
const parent = context.getAncestors().reverse()[0] | ||
if (parent.type !== "VariableDeclarator") { | ||
context.report({ | ||
node, | ||
message: | ||
"DocumentClient should be instantiated and instrumented before being used", | ||
}) | ||
"VariableDeclarator > NewExpression[callee.property.name='DocumentClient']": ( | ||
node, | ||
) => { | ||
if (node.parent.id && node.parent.id.name) { | ||
trackedVariables.add(varInScope(node.parent.id.name)) | ||
} | ||
}, | ||
"VariableDeclarator:has(NewExpression[callee.property.name='DocumentClient'])": ( | ||
"CallExpression[callee.property.name='captureAWSClient']:has(NewExpression[callee.property.name='DocumentClient']) .arguments": ( | ||
node, | ||
) => { | ||
if (node.id.name) { | ||
trackedVariables.push(varInScope(node.id.name)) | ||
} | ||
checkMemberExpression(node) | ||
}, | ||
"CallExpression[callee.property.name='captureAWSClient'][arguments.length=1] MemberExpression.arguments *.object Identifier[name]": ( | ||
"CallExpression[callee.property.name='captureAWSClient'][arguments.length=1] .arguments[type!='NewExpression']": ( | ||
node, | ||
) => { | ||
const instrumented = varInScope(node.name) | ||
trackedVariables = trackedVariables.filter((v) => v !== instrumented) | ||
const name = node.name || node.object.name | ||
if (!isTracked(name)) return | ||
switch (node.type) { | ||
case "MemberExpression": | ||
return checkMemberExpression(node) | ||
case "Identifier": | ||
return checkIdentifier(node) | ||
} | ||
}, | ||
"CallExpression[callee.object.name]": (node) => { | ||
const v = varInScope(node.callee.object.name) | ||
if (trackedVariables.includes(v)) { | ||
context.report({ | ||
node, | ||
message: `Called ${v.name} before it was instrumented`, | ||
}) | ||
} | ||
const name = node.callee.object.name | ||
if (!isTracked(name)) return | ||
context.report({ | ||
node, | ||
messageId: "notInstrumented", | ||
data: { name: name }, | ||
}) | ||
}, | ||
@@ -66,0 +115,0 @@ } |
{ | ||
"name": "@stedi/eslint-plugin-stedi-aws-rules", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "AWS coding best practices by Stedi", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
14292
241