Skip to content

Commit

Permalink
handled issue BuilderIO#963 cases (BuilderIO#965)
Browse files Browse the repository at this point in the history
Co-authored-by: Sahil H. Mobaidin <[email protected]>
  • Loading branch information
sahilmob and sahilmob-rb authored Dec 22, 2022
1 parent 53e6619 commit e7bb474
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ export default function MyComponent(props) {
}
```

```js
import { useStore } from '@builder.io/mitosis';

export default function MyComponent(props) {
const state = useStore({
foo: 'bar',
});

function myFunction() {
const { foo } = props.obj;

state.foo = foo;
}

return <div />;
}
```

Examples of **correct** code for this rule:

```js
Expand All @@ -72,3 +90,21 @@ export default function MyComponent(props) {
return <div />;
}
```

```js
import { useStore } from '@builder.io/mitosis';

export default function MyComponent(props) {
const state = useStore({
foo: 'bar',
});

function myFunction() {
const { foo: foo1 } = props.obj;

state.foo = foo;
}

return <div />;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ ruleTester.run('no-var-name-same-as-state-property', rule, {
}
`,
},
{
...opts,
code: `
import { useStore } from '@builder.io/mitosis';
export default function MyComponent(props) {
const state = useStore({
foo: 'bar',
});
function myFunction() {
const { foo: foo1 } = props.obj
state.foo = foo;
}
return <div />;
}
`,
},
// Doesn't apply to none mitosis files
{
...opts,
Expand Down Expand Up @@ -113,6 +134,27 @@ ruleTester.run('no-var-name-same-as-state-property', rule, {
}
return <div />;
}
`,
errors: ['variables with the same name as a state property will shadow it'],
},
{
...opts,
code: `
import { useStore } from '@builder.io/mitosis';
export default function MyComponent(props) {
const state = useStore({
foo: 'bar',
});
function myFunction() {
const { foo } = props.obj
state.foo = foo;
}
return <div />;
}
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,22 @@ const rule: Rule.RuleModule = {
});

for (const d of varDeclarators) {
if (!types.isIdentifier(d.id)) continue;

if (d.id.name === name) {
if (!types.isIdentifier(d.id) && !types.isObjectPattern(d.id)) continue;
if (types.isObjectPattern(d.id)) {
for (const p of d.id.properties) {
if (
types.isProperty(p) &&
types.isIdentifier(p.value) &&
p.value.name == name
) {
context.report({
node: prop,
message:
'variables with the same name as a state property will shadow it',
});
}
}
} else if (d.id.name === name) {
context.report({
node: prop,
message: 'variables with the same name as a state property will shadow it',
Expand Down

0 comments on commit e7bb474

Please sign in to comment.