Skip to content

Commit

Permalink
fix(strict-mutable): search props inside jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
d0whc3r committed Sep 17, 2019
1 parent 7a81e4b commit cea4746
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
43 changes: 27 additions & 16 deletions src/rules/strict-mutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions tests/lib/rules/strict-mutable/strict-mutable.good.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 (<div>test</div>);
this.mutableInJsx2 = false;
return (<div onClick={(e) => this.mutableInJsx = true}>test</div>);
}
}

0 comments on commit cea4746

Please sign in to comment.