-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Enforce valid names for computed properties #1106
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
Conversation
…rs are used in computed names Approach: For each property name, construct a string that defines a function and see if parsing that string with Acorn throws an exception. If it does, assemble an informative error message that states which property is invalid, the first invalid character, and the location of that character within the name. Changes to codebase: - Added new validator test "properties-computed-must-be-valid-function-names" - Added new check into src/validate/js/propValidators/computed.ts, "checkForValidIdentifiers" - this check was added to src/validate/js/utils/checkForValidIdentifiers.ts like the other checks in "computed.ts"
…asweingarten-master
Codecov Report
@@ Coverage Diff @@
## master #1106 +/- ##
==========================================
+ Coverage 91.56% 91.59% +0.02%
==========================================
Files 123 125 +2
Lines 4493 4602 +109
Branches 1447 1514 +67
==========================================
+ Hits 4114 4215 +101
- Misses 162 164 +2
- Partials 217 223 +6
Continue to review full report at Codecov.
|
In |
src/utils/isValidIdentifier.ts
Outdated
@@ -0,0 +1,11 @@ | |||
import { isIdentifierStart, isIdentifierChar } from 'acorn'; | |||
|
|||
export default function isValidIdentifier(str) { |
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.
ts maybe?
export default function isValidIdentifier(str: string) : boolean {
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.
good catch, fixed
const name = getName(computation.key); | ||
|
||
if (!isValidIdentifier(name)) { | ||
const suggestion = name.replace(/[^_$a-z0-9]/ig, '_').replace(/^\d/, '_$&'); |
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.
should this be moved to utilities?
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.
I tend to favour leaving things like this inline until you need them in more than one place, otherwise I find you can end up with a lot of premature abstractions
I've updated |
@@ -0,0 +1,3 @@ | |||
{{#each things as 𐊧}} | |||
<p>𐊧</p> |
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.
Did you intend for this to be <p>{{𐊧}}</p>
?
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.
yep! fixed
* Fixing missing parentheses and using u as in doc above * added to inital app
This is based on @asweingarten's PR #1099 (thanks!) and adds a couple of extra commits:
try
ornew
) while we're doing this checkacorn.isIdentifierStart
andacorn.isIdentifierChar
to determine whether or not something is a valid identifier — same result, but more directerr.pos
was pointing to the error inside thefunction ${name}(){}
program, which doesn't correspond to the error in the component. Elsewhere, we just point to the start of the offending key, so I've done that here insteadcomputed
properties, we may as well do the check inside the existingforEach
rather than iterating over the properties another timea-b-c
becomesa_b_c
and so on)