Skip to content

Commit

Permalink
Merge pull request #458 from sveltejs/gh-425
Browse files Browse the repository at this point in the history
better error for attempts to use getters/setters for methods
  • Loading branch information
Rich-Harris authored Apr 10, 2017
2 parents 2e946e0 + 99d6502 commit 9a3b4b1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/validate/js/propValidators/methods.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import checkForAccessors from '../utils/checkForAccessors.js';
import checkForDupes from '../utils/checkForDupes.js';
import checkForComputedKeys from '../utils/checkForComputedKeys.js';
import usesThisOrArguments from '../utils/usesThisOrArguments.js';
Expand All @@ -10,6 +11,7 @@ export default function methods ( validator, prop ) {
return;
}

checkForAccessors( validator, prop.value.properties, 'Methods' );
checkForDupes( validator, prop.value.properties );
checkForComputedKeys( validator, prop.value.properties );

Expand Down
7 changes: 7 additions & 0 deletions src/validate/js/utils/checkForAccessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function checkForAccessors ( validator, properties, label ) {
properties.forEach( prop => {
if ( prop.kind !== 'init' ) {
validator.error( `${label} cannot use getters and setters`, prop.start );
}
});
}
5 changes: 3 additions & 2 deletions test/validator/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as fs from 'fs';
import assert from 'assert';
import { svelte, exists, tryToLoadJson } from '../helpers.js';
import { svelte, tryToLoadJson } from '../helpers.js';

describe( 'validate', () => {
fs.readdirSync( 'test/validator/samples' ).forEach( dir => {
if ( dir[0] === '.' ) return;

const solo = exists( `test/validator/samples/${dir}/solo` );
// add .solo to a sample directory name to only run that test
const solo = /\.solo/.test( dir );

if ( solo && process.env.CI ) {
throw new Error( 'Forgot to remove `solo: true` from test' );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Methods cannot use getters and setters",
"loc": {
"line": 4,
"column": 3
},
"pos": 43
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export default {
methods: {
get foo () {

}
}
};
</script>

0 comments on commit 9a3b4b1

Please sign in to comment.