gather names during validation, for later deoncflicting (#88)

pull/134/head
Rich Harris 8 years ago
parent f94df9fb6d
commit 0e64f26712

@ -15,9 +15,9 @@ export function compile ( source, options = {} ) {
};
}
validate( parsed, source, options );
const { names } = validate( parsed, source, options );
return generate( parsed, source, options );
return generate( parsed, source, options, names );
}
export { parse, validate };

@ -0,0 +1,14 @@
export default function validateHtml ( validator, html ) {
function visit ( node ) {
if ( node.type === 'EachBlock' ) {
if ( !~validator.names.indexOf( node.context ) ) validator.names.push( node.context );
if ( node.index && !~validator.names.indexOf( node.index ) ) validator.names.push( node.index );
}
if ( node.children ) {
node.children.forEach( visit );
}
}
html.children.forEach( visit );
}

@ -1,4 +1,5 @@
import validateJs from './js/index.js';
import validateHtml from './html/index.js';
import { getLocator } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame.js';
@ -39,16 +40,18 @@ export default function validate ( parsed, source, options ) {
templateProperties: {},
errors: [],
warnings: []
names: []
};
if ( parsed.html ) {
validateHtml( validator, parsed.html );
}
if ( parsed.js ) {
validateJs( validator, parsed.js );
}
return {
errors: validator.errors,
warnings: validator.warnings
names: validator.names
};
}

@ -177,7 +177,7 @@ describe( 'svelte', () => {
const errors = [];
const warnings = [];
svelte.validate( parsed, input, {
const { names } = svelte.validate( parsed, input, {
onerror ( error ) {
errors.push({
message: error.message,
@ -197,9 +197,11 @@ describe( 'svelte', () => {
const expectedErrors = tryToLoadJson( `test/validator/${dir}/errors.json` ) || [];
const expectedWarnings = tryToLoadJson( `test/validator/${dir}/warnings.json` ) || [];
const expectedNames = tryToLoadJson( `test/validator/${dir}/names.json` ) || [];
assert.deepEqual( errors, expectedErrors );
assert.deepEqual( warnings, expectedWarnings );
assert.deepEqual( names, expectedNames );
} catch ( err ) {
if ( err.name !== 'ParseError' ) throw err;

@ -0,0 +1,3 @@
{{#each things as thing, index}}
<p>{{index}}: {{thing}}</p>
{{/each}}

@ -0,0 +1,4 @@
[
"thing",
"index"
]
Loading…
Cancel
Save