catch hardcoded names that should be aliases (#465)

pull/468/head
Rich Harris 8 years ago
parent c61ce134df
commit a793398d63

@ -11,6 +11,8 @@ import getOutro from './shared/utils/getOutro.js';
import processCss from './shared/processCss.js';
import annotateWithScopes from './annotateWithScopes.js';
const test = typeof global !== 'undefined' && global.__svelte_test;
export default class Generator {
constructor ( parsed, source, name, options ) {
this.parsed = parsed;
@ -238,6 +240,7 @@ export default class Generator {
}
getUniqueName ( name ) {
if ( test ) name = `${name}$`;
let alias = name;
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ); alias = `${name}_${i++}` );
this._usedNames.add( alias );
@ -247,6 +250,7 @@ export default class Generator {
getUniqueNameMaker ( params ) {
const localUsedNames = new Set( params );
return name => {
if ( test ) name = `${name}$`;
let alias = name;
for ( let i = 1; reservedNames.has( alias ) || this.importedNames.has( alias ) || this._usedNames.has( alias ) || localUsedNames.has( alias ); alias = `${name}_${i++}` );
localUsedNames.add( alias );

@ -32,7 +32,7 @@ export default function visitEventHandler ( generator, block, state, node, attri
const declarations = usedContexts.map( name => {
if ( name === 'root' ) {
if ( shouldHoist ) state.usesComponent = true;
return 'var root = component.get();';
return `var root = ${block.component}.get();`;
}
const listName = block.listNames.get( name );
@ -52,7 +52,7 @@ export default function visitEventHandler ( generator, block, state, node, attri
if ( state.usesComponent ) {
// TODO the element needs to know to create `thing._svelte = { component: component }`
handlerBody.addLine( `var component = this._svelte.component;` );
handlerBody.addLine( `var ${block.component} = this._svelte.component;` );
}
declarations.forEach( declaration => {

@ -22,7 +22,7 @@ export default function visitWindow ( generator, block, node ) {
const flattened = flattenReference( attribute.expression.callee );
if ( flattened.name !== 'event' && flattened.name !== 'this' ) {
// allow event.stopPropagation(), this.select() etc
generator.code.prependRight( attribute.expression.start, 'component.' );
generator.code.prependRight( attribute.expression.start, `${block.component}.` );
}
const handlerName = block.getUniqueName( `onwindow${attribute.name}` );
@ -68,7 +68,7 @@ export default function visitWindow ( generator, block, node ) {
block.builders.create.addBlock( deindent`
var ${handlerName} = function ( event ) {
component.set({
${block.component}.set({
${props}
});
};

@ -10,9 +10,18 @@ sourceMapSupport.install();
// for coverage purposes, we need to test source files,
// but for sanity purposes, we need to test dist files
export const svelte = process.env.COVERAGE ?
require( '../src/index.js' ) :
require( '../compiler/svelte.js' );
export function loadSvelte ( test ) {
if ( test ) global.__svelte_test = true;
const resolved = process.env.COVERAGE ?
require.resolve( '../src/index.js' ) :
require.resolve( '../compiler/svelte.js' );
delete require.cache[ resolved ];
return require( resolved );
}
export const svelte = loadSvelte();
export function exists ( path ) {
try {

@ -5,7 +5,9 @@ import * as fs from 'fs';
import * as acorn from 'acorn';
import * as babel from 'babel-core';
import { addLineNumbers, loadConfig, svelte, env, setupHtmlEqual } from '../helpers.js';
import { addLineNumbers, loadConfig, loadSvelte, env, setupHtmlEqual } from '../helpers.js';
let svelte;
let showCompiledCode = false;
let compileOptions = null;
@ -33,7 +35,10 @@ require.extensions[ '.html' ] = function ( module, filename ) {
const Object_assign = Object.assign;
describe( 'runtime', () => {
before( setupHtmlEqual );
before( () => {
svelte = loadSvelte( true );
return setupHtmlEqual();
});
function runTest ( dir, shared ) {
if ( dir[0] === '.' ) return;

Loading…
Cancel
Save