-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
More helpful validation
- Loading branch information
Showing
20 changed files
with
273 additions
and
29 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
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
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
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 |
---|---|---|
@@ -1,5 +1,22 @@ | ||
import * as namespaces from '../../../utils/namespaces.js'; | ||
import FuzzySet from '../utils/FuzzySet.js'; | ||
|
||
const fuzzySet = new FuzzySet( namespaces.validNamespaces ); | ||
const valid = new Set( namespaces.validNamespaces ); | ||
|
||
export default function namespace ( validator, prop ) { | ||
if ( prop.value.type !== 'Literal' || typeof prop.value.value !== 'string' ) { | ||
const ns = prop.value.value; | ||
|
||
if ( prop.value.type !== 'Literal' || typeof ns !== 'string' ) { | ||
validator.error( `The 'namespace' property must be a string literal representing a valid namespace`, prop.start ); | ||
} | ||
|
||
if ( !valid.has( ns ) ) { | ||
const matches = fuzzySet.get( ns ); | ||
if ( matches && matches[0] && matches[0][0] > 0.7 ) { | ||
validator.error( `Invalid namespace '${ns}' (did you mean '${matches[0][1]}'?)`, prop.start ); | ||
} else { | ||
validator.error( `Invalid namespace '${ns}'`, prop.start ); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
test/validator/samples/helper-purity-check-needs-arguments/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 { | ||
helpers: { | ||
foo () { | ||
return Math.random(); | ||
} | ||
} | ||
} | ||
</script> |
8 changes: 8 additions & 0 deletions
8
test/validator/samples/helper-purity-check-needs-arguments/warnings.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": "Helpers should be pure functions, with at least one argument", | ||
"pos": 54, | ||
"loc": { | ||
"line": 6, | ||
"column": 3 | ||
} | ||
}] |
8 changes: 8 additions & 0 deletions
8
test/validator/samples/helper-purity-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": "Helpers 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": 95, | ||
"loc": { | ||
"line": 7, | ||
"column": 4 | ||
} | ||
}] |
11 changes: 11 additions & 0 deletions
11
test/validator/samples/helper-purity-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 on:click='foo()'>foo</button> | ||
|
||
<script> | ||
export default { | ||
helpers: { | ||
foo () { | ||
this.set({ foo: true }); | ||
} | ||
} | ||
} | ||
</script> |
8 changes: 8 additions & 0 deletions
8
test/validator/samples/helper-purity-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 helper function as an argument", | ||
"pos": 74, | ||
"loc": { | ||
"line": 7, | ||
"column": 11 | ||
} | ||
}] |
11 changes: 11 additions & 0 deletions
11
test/validator/samples/helper-purity-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 { | ||
helpers: { | ||
foo () { | ||
return this.get( 'bar' ); | ||
} | ||
} | ||
} | ||
</script> |
13 changes: 13 additions & 0 deletions
13
test/validator/samples/helper-purity-check-uses-arguments/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,13 @@ | ||
{{sum(1, 2, 3)}} | ||
|
||
<script> | ||
export default { | ||
helpers: { | ||
sum () { | ||
var total = ''; | ||
for ( var i = 0; i < arguments.length; i += 1 ) total += arguments[i]; | ||
return total; | ||
} | ||
} | ||
} | ||
</script> |
1 change: 1 addition & 0 deletions
1
test/validator/samples/helper-purity-check-uses-arguments/warnings.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 @@ | ||
[] |
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": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar). 'foo' exists on 'helpers', did you put it in the wrong place?", | ||
"pos": 18, | ||
"loc": { | ||
"line": 1, | ||
"column": 18 | ||
} | ||
}] |
17 changes: 17 additions & 0 deletions
17
test/validator/samples/method-nonexistent-helper/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,17 @@ | ||
<button on:click='foo()'></button> | ||
|
||
<script> | ||
export default { | ||
methods: { | ||
bar () { | ||
// ... | ||
} | ||
}, | ||
|
||
helpers: { | ||
foo ( x ) { | ||
return x; | ||
} | ||
} | ||
}; | ||
</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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[{ | ||
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire or bar)", | ||
"pos": 18, | ||
"loc": { | ||
"line": 1, | ||
"column": 18 | ||
} | ||
}] |
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 on:click='foo()'></button> | ||
|
||
<script> | ||
export default { | ||
methods: { | ||
bar () { | ||
// ... | ||
} | ||
} | ||
}; | ||
</script> |
Oops, something went wrong.