0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

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

This commit is contained in:
Rich Harris 2016-12-06 11:48:24 -05:00
parent f94df9fb6d
commit 0e64f26712
6 changed files with 33 additions and 7 deletions

View File

@ -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 };

View File

@ -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 );
}

View File

@ -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
};
}

View File

@ -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;

View File

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

View File

@ -0,0 +1,4 @@
[
"thing",
"index"
]