From 0e64f26712eb72c55e46e1700a7ed77aa4357ab2 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 6 Dec 2016 11:48:24 -0500 Subject: [PATCH] gather names during validation, for later deoncflicting (#88) --- compiler/index.js | 4 ++-- compiler/validate/html/index.js | 14 ++++++++++++++ compiler/validate/index.js | 11 +++++++---- test/test.js | 4 +++- test/validator/names/input.html | 3 +++ test/validator/names/names.json | 4 ++++ 6 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 compiler/validate/html/index.js create mode 100644 test/validator/names/input.html create mode 100644 test/validator/names/names.json diff --git a/compiler/index.js b/compiler/index.js index 1ee0dcfa42..01d0685b9c 100644 --- a/compiler/index.js +++ b/compiler/index.js @@ -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 }; diff --git a/compiler/validate/html/index.js b/compiler/validate/html/index.js new file mode 100644 index 0000000000..d7d7378fbd --- /dev/null +++ b/compiler/validate/html/index.js @@ -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 ); +} diff --git a/compiler/validate/index.js b/compiler/validate/index.js index a2ee763a2c..89811421e5 100644 --- a/compiler/validate/index.js +++ b/compiler/validate/index.js @@ -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 }; } diff --git a/test/test.js b/test/test.js index 297406c72f..f53bef8a19 100644 --- a/test/test.js +++ b/test/test.js @@ -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; diff --git a/test/validator/names/input.html b/test/validator/names/input.html new file mode 100644 index 0000000000..a944d5fec0 --- /dev/null +++ b/test/validator/names/input.html @@ -0,0 +1,3 @@ +{{#each things as thing, index}} +

{{index}}: {{thing}}

+{{/each}} diff --git a/test/validator/names/names.json b/test/validator/names/names.json new file mode 100644 index 0000000000..01151a29fd --- /dev/null +++ b/test/validator/names/names.json @@ -0,0 +1,4 @@ +[ + "thing", + "index" +]