Skip to content

Commit

Permalink
gather names during validation, for later deoncflicting (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 6, 2016
1 parent f94df9f commit 0e64f26
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
14 changes: 14 additions & 0 deletions compiler/validate/html/index.js
Original file line number Diff line number Diff line change
@@ -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 );
}
11 changes: 7 additions & 4 deletions compiler/validate/index.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
};
}
4 changes: 3 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions test/validator/names/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#each things as thing, index}}
<p>{{index}}: {{thing}}</p>
{{/each}}
4 changes: 4 additions & 0 deletions test/validator/names/names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"thing",
"index"
]

0 comments on commit 0e64f26

Please sign in to comment.