add option to disable two-way binding (#54)

pull/1059/head
Rich Harris 7 years ago
parent 8b5dba9f4a
commit 20962f9d82

@ -22,6 +22,7 @@ class ParseError extends CompileError {
interface ParserOptions { interface ParserOptions {
filename?: string; filename?: string;
bind?: boolean;
} }
type ParserState = (parser: Parser) => (ParserState | void); type ParserState = (parser: Parser) => (ParserState | void);
@ -38,6 +39,8 @@ export class Parser {
js: Node; js: Node;
metaTags: {}; metaTags: {};
allowBindings: boolean;
constructor(template: string, options: ParserOptions) { constructor(template: string, options: ParserOptions) {
if (typeof template !== 'string') { if (typeof template !== 'string') {
throw new TypeError('Template must be a string'); throw new TypeError('Template must be a string');
@ -46,6 +49,8 @@ export class Parser {
this.template = template.replace(/\s+$/, ''); this.template = template.replace(/\s+$/, '');
this.filename = options.filename; this.filename = options.filename;
this.allowBindings = options.bind !== false;
this.index = 0; this.index = 0;
this.stack = []; this.stack = [];
this.metaTags = {}; this.metaTags = {};

@ -182,6 +182,10 @@ export default function tag(parser: Parser) {
let attribute; let attribute;
while ((attribute = readAttribute(parser, uniqueNames))) { while ((attribute = readAttribute(parser, uniqueNames))) {
if (attribute.type === 'Binding' && !parser.allowBindings) {
parser.error(`Two-way binding is disabled`, attribute.start);
}
element.attributes.push(attribute); element.attributes.push(attribute);
parser.allowWhitespace(); parser.allowWhitespace();
} }

@ -1,6 +1,6 @@
import assert from 'assert'; import assert from 'assert';
import fs from 'fs'; import fs from 'fs';
import { svelte } from '../helpers.js'; import { svelte, tryToLoadJson } from '../helpers.js';
describe('parse', () => { describe('parse', () => {
fs.readdirSync('test/parser/samples').forEach(dir => { fs.readdirSync('test/parser/samples').forEach(dir => {
@ -20,8 +20,10 @@ describe('parse', () => {
.readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8') .readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8')
.replace(/\s+$/, ''); .replace(/\s+$/, '');
const options = tryToLoadJson(`test/parser/samples/${dir}/options.json`) || {};
try { try {
const actual = svelte.parse(input); const actual = svelte.parse(input, options);
fs.writeFileSync( fs.writeFileSync(
`test/parser/samples/${dir}/_actual.json`, `test/parser/samples/${dir}/_actual.json`,
JSON.stringify(actual, null, '\t') JSON.stringify(actual, null, '\t')

@ -0,0 +1,8 @@
{
"message": "Two-way binding is disabled",
"loc": {
"line": 1,
"column": 7
},
"pos": 7
}
Loading…
Cancel
Save