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:
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 };
|
||||
|
14
compiler/validate/html/index.js
Normal file
14
compiler/validate/html/index.js
Normal 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 );
|
||||
}
|
@ -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;
|
||||
|
||||
|
3
test/validator/names/input.html
Normal file
3
test/validator/names/input.html
Normal file
@ -0,0 +1,3 @@
|
||||
{{#each things as thing, index}}
|
||||
<p>{{index}}: {{thing}}</p>
|
||||
{{/each}}
|
4
test/validator/names/names.json
Normal file
4
test/validator/names/names.json
Normal file
@ -0,0 +1,4 @@
|
||||
[
|
||||
"thing",
|
||||
"index"
|
||||
]
|
Loading…
Reference in New Issue
Block a user