From cea47466e3bc6a1335a999b41570483d8e1309e5 Mon Sep 17 00:00:00 2001 From: d0whc3r Date: Tue, 17 Sep 2019 16:27:31 +0200 Subject: [PATCH] fix(strict-mutable): search props inside jsx --- src/rules/strict-mutable.ts | 43 ++++++++++++------- .../strict-mutable/strict-mutable.good.tsx | 7 ++- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/rules/strict-mutable.ts b/src/rules/strict-mutable.ts index 32baf46..2e9b82f 100644 --- a/src/rules/strict-mutable.ts +++ b/src/rules/strict-mutable.ts @@ -39,26 +39,37 @@ const rule: Rule.RuleModule = { return null; } + function parseExpression(expression: any): any { + if (expression.expression) { + return parseExpression(expression.expression); + } + if (expression.openingElement) { + return expression.openingElement.attributes && + expression.openingElement.attributes.nextContainer && + parseExpression(expression.openingElement.attributes.nextContainer.body) + } + return expression && getName(expression); + } + function removeUsedVars(statements: any[]) { statements - .filter((st) => st.expression) - .map((st) => getName(st.expression)) - .filter((name) => !!name) - .forEach((name) => { - mutableProps.delete(name.escapedText); - }); + .map((st) => st.expression && parseExpression(st.expression)) + .filter((name) => !!name) + .forEach((name) => { + mutableProps.delete(name.escapedText); + }); statements - .filter((st) => st.thenStatement && st.thenStatement.statements) - .map((st) => st.thenStatement.statements) - .forEach((st) => { - removeUsedVars(st); - }); + .filter((st) => st.thenStatement && st.thenStatement.statements) + .map((st) => st.thenStatement.statements) + .forEach((st) => { + removeUsedVars(st); + }); statements - .filter((st) => st.elseStatement) - .forEach((st) => { - const sts = Array.isArray(st.elseStatement) ? st.elseStatement : [st.elseStatement]; - removeUsedVars(sts); - }); + .filter((st) => st.elseStatement) + .forEach((st) => { + const sts = Array.isArray(st.elseStatement) ? st.elseStatement : [st.elseStatement]; + removeUsedVars(sts); + }); } return { diff --git a/tests/lib/rules/strict-mutable/strict-mutable.good.tsx b/tests/lib/rules/strict-mutable/strict-mutable.good.tsx index e494978..7e4f530 100644 --- a/tests/lib/rules/strict-mutable/strict-mutable.good.tsx +++ b/tests/lib/rules/strict-mutable/strict-mutable.good.tsx @@ -4,6 +4,8 @@ export class SampleTag { @Prop({ mutable: true }) testMutable?: string; @Prop({ mutable: true }) testMutable2?: boolean; @Prop({ mutable: false }) readonly testNotMutable?: boolean; + @Prop({ mutable: true }) mutableInJsx?: boolean; + @Prop({ mutable: true }) mutableInJsx2?: boolean; private internalMethod() { const test = 'hi'; @@ -15,12 +17,13 @@ export class SampleTag { e.preventDefault(); if (!this.testNotMutable) { this.testMutable = true; - } else if(this.testMutable === undefined) { + } else if (this.testMutable === undefined) { this.testMutable2 = !this.testMutable2; } } render() { - return (
test
); + this.mutableInJsx2 = false; + return (
this.mutableInJsx = true}>test
); } }