-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validate bindings #494
Merged
+121
−32
Merged
validate bindings #494
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import flattenReference from '../../utils/flattenReference.js'; | ||
|
||
export default function validateElement ( validator, node ) { | ||
const isComponent = node.name === ':Self' || validator.components.has( node.name ); | ||
|
||
node.attributes.forEach( attribute => { | ||
if ( !isComponent && attribute.type === 'Binding' ) { | ||
const { name } = attribute; | ||
|
||
if ( name === 'value' ) { | ||
if ( node.name !== 'input' && node.name !== 'textarea' && node.name !== 'select' ) { | ||
validator.error( `'value' is not a valid binding on <${node.name}> elements`, attribute.start ); | ||
} | ||
} | ||
|
||
else if ( name === 'checked' ) { | ||
if ( node.name !== 'input' ) { | ||
validator.error( `'checked' is not a valid binding on <${node.name}> elements`, attribute.start ); | ||
} | ||
|
||
if ( getType( validator, node ) !== 'checkbox' ) { | ||
validator.error( `'checked' binding can only be used with <input type="checkbox">` ); | ||
} | ||
} | ||
|
||
else if ( name === 'group' ) { | ||
if ( node.name !== 'input' ) { | ||
validator.error( `'group' is not a valid binding on <${node.name}> elements`, attribute.start ); | ||
} | ||
|
||
const type = getType( validator, node ); | ||
|
||
if ( type !== 'checkbox' && type !== 'radio' ) { | ||
validator.error( `'checked' binding can only be used with <input type="checkbox"> or <input type="radio">` ); | ||
} | ||
} | ||
|
||
else { | ||
validator.error( `'${attribute.name}' is not a valid binding`, attribute.start ); | ||
} | ||
} | ||
|
||
if ( attribute.type === 'EventHandler' ) { | ||
const { callee, start, type } = attribute.expression; | ||
|
||
if ( type !== 'CallExpression' ) { | ||
validator.error( `Expected a call expression`, start ); | ||
} | ||
|
||
const { name } = flattenReference( callee ); | ||
|
||
if ( name === 'this' || name === 'event' ) return; | ||
if ( callee.type === 'Identifier' && callee.name === 'set' || callee.name === 'fire' || validator.methods.has( callee.name ) ) return; | ||
|
||
const validCallees = list( [ 'this.*', 'event.*', 'set', 'fire' ].concat( Array.from( validator.methods.keys() ) ) ); | ||
let message = `'${validator.source.slice( callee.start, callee.end )}' is an invalid callee (should be one of ${validCallees})`; | ||
|
||
if ( callee.type === 'Identifier' && validator.helpers.has( callee.name ) ) { | ||
message += `. '${callee.name}' exists on 'helpers', did you put it in the wrong place?`; | ||
} | ||
|
||
validator.error( message, start ); | ||
} | ||
}); | ||
} | ||
|
||
function getType ( validator, node ) { | ||
const attribute = node.attributes.find( attribute => attribute.name === 'type' ); | ||
if ( !attribute ) return null; | ||
|
||
if ( attribute.value === true ) { | ||
validator.error( `'type' attribute must be specified`, attribute.start ); | ||
} | ||
|
||
if ( attribute.value.length > 1 || attribute.value[0].type !== 'Text' ) { | ||
validator.error( `'type attribute cannot be dynamic`, attribute.start ); | ||
} | ||
|
||
return attribute.value[0].data; | ||
} | ||
|
||
function list ( items, conjunction = 'or' ) { | ||
if ( items.length === 1 ) return items[0]; | ||
return `${items.slice( 0, -1 ).join( ', ' )} ${conjunction} ${items[ items.length - 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,3 @@ | ||
export default function validateWindow () { | ||
// TODO | ||
} |
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/binding-invalid-on-element/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": "'value' is not a valid binding on <div> elements", | ||
"pos": 5, | ||
"loc": { | ||
"line": 1, | ||
"column": 5 | ||
} | ||
}] |
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 @@ | ||
<div bind:value></div> |
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": "'whatever' is not a valid binding", | ||
"pos": 5, | ||
"loc": { | ||
"line": 1, | ||
"column": 5 | ||
} | ||
}] |
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 @@ | ||
<div bind:whatever></div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const meta = new Map( ... );
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 👍