-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check against this usage in computed functions
- Loading branch information
Showing
9 changed files
with
105 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Node } from '../interfaces'; | ||
|
||
export default function isThisGetCallExpression(node: Node): boolean { | ||
return node.type === 'CallExpression' && | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'ThisExpression' && | ||
node.callee.property.name === 'get'; | ||
} |
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,21 @@ | ||
import { Node } from '../interfaces'; | ||
import { walk } from 'estree-walker'; | ||
|
||
export default function walkThroughTopFunctionScope(body: Node, callback: Function) { | ||
let lexicalDepth = 0; | ||
walk(body, { | ||
enter(node: Node) { | ||
if (/^Function/.test(node.type)) { | ||
lexicalDepth += 1; | ||
} else if (lexicalDepth === 0) { | ||
callback(node) | ||
} | ||
}, | ||
|
||
leave(node: Node) { | ||
if (/^Function/.test(node.type)) { | ||
lexicalDepth -= 1; | ||
} | ||
}, | ||
}); | ||
} |
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
8 changes: 8 additions & 0 deletions
8
test/validator/samples/computed-purify-check-no-this/errors.json
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 @@ | ||
[{ | ||
"message": "Computed should be pure functions — they do not have access to the component instance and cannot use 'this'. Did you mean to put this in 'methods'?", | ||
"pos": 83, | ||
"loc": { | ||
"line": 7, | ||
"column": 4 | ||
} | ||
}] |
11 changes: 11 additions & 0 deletions
11
test/validator/samples/computed-purify-check-no-this/input.html
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,11 @@ | ||
<button>{{foo}}</button> | ||
|
||
<script> | ||
export default { | ||
computed: { | ||
foo () { | ||
this.set({ foo: true }); | ||
} | ||
} | ||
} | ||
</script> |
8 changes: 8 additions & 0 deletions
8
test/validator/samples/computed-purify-check-this-get/errors.json
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 @@ | ||
[{ | ||
"message": "Cannot use this.get(...) — it must be passed into the computed function as an argument", | ||
"pos": 73, | ||
"loc": { | ||
"line": 7, | ||
"column": 11 | ||
} | ||
}] |
11 changes: 11 additions & 0 deletions
11
test/validator/samples/computed-purify-check-this-get/input.html
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,11 @@ | ||
{{foo}} | ||
|
||
<script> | ||
export default { | ||
computed: { | ||
foo () { | ||
return this.get( 'bar' ); | ||
} | ||
} | ||
} | ||
</script> |
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 |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
} | ||
} | ||
} | ||
</script> | ||
</script> |