Skip to content
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

throw if default export is not an object literal #190

Merged
merged 2 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions src/validate/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,41 @@ export default function validateJs ( validator, js ) {
}

if ( node.type === 'ExportDefaultDeclaration' ) {
if ( validator.defaultExport ) {
validator.error( `Duplicate default export`, node.start );
if ( node.declaration.type !== 'ObjectExpression' ) {
return validator.error( `Default export must be an object literal`, node.declaration.start );
}

validator.defaultExport = node;
}
});
checkForComputedKeys( validator, node.declaration.properties );
checkForDupes( validator, node.declaration.properties );

node.declaration.properties.forEach( prop => {
validator.templateProperties[ prop.key.name ] = prop;
});

// ensure all exported props are valid
if ( validator.defaultExport ) {
checkForComputedKeys( validator, validator.defaultExport.declaration.properties );
checkForDupes( validator, validator.defaultExport.declaration.properties );

validator.defaultExport.declaration.properties.forEach( prop => {
validator.templateProperties[ prop.key.name ] = prop;
});

validator.defaultExport.declaration.properties.forEach( prop => {
const propValidator = propValidators[ prop.key.name ];

if ( propValidator ) {
propValidator( validator, prop );
} else {
const matches = fuzzySet.get( prop.key.name );
if ( matches && matches[0] && matches[0][0] > 0.7 ) {
validator.error( `Unexpected property '${prop.key.name}' (did you mean '${matches[0][1]}'?)`, prop.start );
} else if ( /FunctionExpression/.test( prop.value.type ) ) {
validator.error( `Unexpected property '${prop.key.name}' (did you mean to include it in 'methods'?)`, prop.start );
// ensure all exported props are valid
node.declaration.properties.forEach( prop => {
const propValidator = propValidators[ prop.key.name ];

if ( propValidator ) {
propValidator( validator, prop );
} else {
validator.error( `Unexpected property '${prop.key.name}'`, prop.start );
const matches = fuzzySet.get( prop.key.name );
if ( matches && matches[0] && matches[0][0] > 0.7 ) {
validator.error( `Unexpected property '${prop.key.name}' (did you mean '${matches[0][1]}'?)`, prop.start );
} else if ( /FunctionExpression/.test( prop.value.type ) ) {
validator.error( `Unexpected property '${prop.key.name}' (did you mean to include it in 'methods'?)`, prop.start );
} else {
validator.error( `Unexpected property '${prop.key.name}'`, prop.start );
}
}
});

if ( validator.templateProperties.namespace ) {
const ns = validator.templateProperties.namespace.value.value;
validator.namespace = namespaces[ ns ] || ns;
}
});

if ( validator.templateProperties.namespace ) {
const ns = validator.templateProperties.namespace.value.value;
validator.namespace = namespaces[ ns ] || ns;
validator.defaultExport = node;
}
}
});
}
4 changes: 2 additions & 2 deletions test/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ describe( 'validate', () => {
if ( err.name !== 'ParseError' ) throw err;

try {
const expected = require( `./validator/${dir}/error.json` );
const expected = require( `./validator/${dir}/errors.json` )[0];

assert.equal( err.shortMessage, expected.message );
assert.equal( err.message, expected.message );
assert.deepEqual( err.loc, expected.loc );
assert.equal( err.pos, expected.pos );
} catch ( err2 ) {
Expand Down
8 changes: 8 additions & 0 deletions test/validator/export-default-duplicated/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Duplicate export 'default'",
"pos": 37,
"loc": {
"line": 3,
"column": 8
}
}]
4 changes: 4 additions & 0 deletions test/validator/export-default-duplicated/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
export default {};
export default {};
</script>
8 changes: 8 additions & 0 deletions test/validator/export-default-must-be-object/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{
"message": "Default export must be an object literal",
"pos": 25,
"loc": {
"line": 2,
"column": 16
}
}]
3 changes: 3 additions & 0 deletions test/validator/export-default-must-be-object/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
export default potato;
</script>