diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000000..1520f88d5b --- /dev/null +++ b/.eslintrc @@ -0,0 +1,40 @@ +{ + "root": true, + "rules": { + "indent": [ 2, "tab", { "SwitchCase": 1 } ], + "semi": [ 2, "always" ], + "keyword-spacing": [ 2, { "before": true, "after": true } ], + "space-before-blocks": [ 2, "always" ], + "space-before-function-paren": [ 2, "always" ], + "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], + "no-cond-assign": 0, + "no-unused-vars": 2, + "object-shorthand": [ 2, "always" ], + "no-const-assign": 2, + "no-class-assign": 2, + "no-this-before-super": 2, + "no-var": 2, + "no-unreachable": 2, + "valid-typeof": 2, + "quote-props": [ 2, "as-needed" ], + "one-var": [ 2, "never" ], + "prefer-arrow-callback": 2, + "prefer-const": [ 2, { "destructuring": "all" } ], + "arrow-spacing": 2 + }, + "env": { + "es6": true, + "browser": true, + "node": true, + "mocha": true + }, + "extends": [ + "eslint:recommended", + "plugin:import/errors", + "plugin:import/warnings" + ], + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + } +} diff --git a/compiler/generate/index.js b/compiler/generate/index.js index e69de29bb2..50d83e9b42 100644 --- a/compiler/generate/index.js +++ b/compiler/generate/index.js @@ -0,0 +1,7 @@ +export default function generate ( parsed ) { + return ` + export default function createComponent ( options ) { + console.log( 'TODO' ); + } + `; +} diff --git a/compiler/index.js b/compiler/index.js index 773d68506b..4a0199203c 100644 --- a/compiler/index.js +++ b/compiler/index.js @@ -1,2 +1,9 @@ -export { default as parse } from './parse/index.js'; -export { default as generate } from './generate/index.js'; +import parse from './parse/index.js'; +import generate from './generate/index.js'; + +export function compile ( template ) { + const parsed = parse( template ); + const generated = generate( parsed ); + + return generated; +} diff --git a/package.json b/package.json index 1675e4e9b5..c9620036c7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "The frameworkless UI framework", "main": "dist/svelte-compiler.js", "scripts": { - "test": "mocha --opts mocha.opts --recursive ./**/__test__.js" + "test": "mocha --opts mocha.opts --recursive ./**/__test__.js test/test.js", + "lint": "eslint compiler" }, "repository": { "type": "git", @@ -23,6 +24,9 @@ }, "homepage": "https://gitlab.com/Rich-Harris/svelte#README", "devDependencies": { + "eslint": "^3.10.2", + "eslint-plugin-import": "^2.2.0", + "jsdom": "^9.8.3", "mocha": "^3.1.2", "reify": "^0.4.0" }, diff --git a/test/samples/single-static-element/_config.js b/test/samples/single-static-element/_config.js new file mode 100644 index 0000000000..59a8ceef8d --- /dev/null +++ b/test/samples/single-static-element/_config.js @@ -0,0 +1,4 @@ +export default { + description: 'creates a factory with a single static element', + html: 'test' +}; diff --git a/test/samples/single-static-element/main.svelte b/test/samples/single-static-element/main.svelte new file mode 100644 index 0000000000..61dba8bc46 --- /dev/null +++ b/test/samples/single-static-element/main.svelte @@ -0,0 +1 @@ +test diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000000..6edf31dcee --- /dev/null +++ b/test/test.js @@ -0,0 +1,59 @@ +import { compile } from '../compiler/index.js'; +import * as assert from 'assert'; +import * as fs from 'fs'; +import jsdom from 'jsdom'; + +require.extensions[ '.svelte' ] = function ( module, filename ) { + const source = fs.readFileSync( filename, 'utf-8' ); + const compiled = compile( source ); + + return module._compile( compiled, filename ); +}; + +describe( 'svelte', () => { + function loadConfig ( dir ) { + try { + return require( `./samples/${dir}/_config.js` ).default; + } catch ( err ) { + if ( err.code === 'E_NOT_FOUND' ) { + return {}; + } + + throw err; + } + } + + function env () { + return new Promise( ( fulfil, reject ) => { + jsdom.env( '
', ( err, window ) => { + if ( err ) { + reject( err ); + } else { + fulfil( window ); + } + }); + }); + } + + fs.readdirSync( 'test/samples' ).forEach( dir => { + if ( dir[0] === '.' ) return; + + it( dir, () => { + const config = loadConfig( dir ); + const factory = require( `./samples/${dir}/main.svelte` ).default; + + return env().then( window => { + const target = window.document.querySelector( 'main' ); + + const component = factory({ + target, + data: config.data + }); + + if ( config.html ) { + assert.equal( target.innerHTML, config.html ); + } + }); + }); + }); +});