Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to disable two-way binding #1059

Merged
merged 3 commits into from
Dec 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The Svelte compiler optionally takes a second argument, an object of configurati
| `hydratable` | `true`, `false` | Whether to support hydration on the compiled component. | `false` |
| `customElement` | `true`, `false`, `{ tag, props }` | Whether to compile this component to a custom element. If `tag`/`props` are passed, compiles to a custom element and overrides the values exported by the component. | `false` |
| `cascade` | `true`, `false` | Whether to cascade all of the component's styles to child components. If `false`, only selectors wrapped in `:global(...)` and keyframe IDs beginning with `-global-` are cascaded. | `true` |
| `bind` | `boolean` | If `false`, disallows `bind:` directives | `true` |
| | | |
| `shared` | `true`, `false`, `string` | Whether to import various helpers from a shared external library. When you have a project with multiple components, this reduces the overall size of your JavaScript bundle, at the expense of having immediately-usable component. You can pass a string of the module path to use, or `true` will import from `'svelte/shared.js'`. | `false` |
| `legacy` | `true`, `false` | Ensures compatibility with very old browsers, at the cost of some extra code. | `false` |
Expand Down
5 changes: 5 additions & 0 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ParseError extends CompileError {

interface ParserOptions {
filename?: string;
bind?: boolean;
}

type ParserState = (parser: Parser) => (ParserState | void);
Expand All @@ -38,6 +39,8 @@ export class Parser {
js: Node;
metaTags: {};

allowBindings: boolean;

constructor(template: string, options: ParserOptions) {
if (typeof template !== 'string') {
throw new TypeError('Template must be a string');
Expand All @@ -46,6 +49,8 @@ export class Parser {
this.template = template.replace(/\s+$/, '');
this.filename = options.filename;

this.allowBindings = options.bind !== false;

this.index = 0;
this.stack = [];
this.metaTags = {};
Expand Down
4 changes: 4 additions & 0 deletions src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export default function tag(parser: Parser) {

let attribute;
while ((attribute = readAttribute(parser, uniqueNames))) {
if (attribute.type === 'Binding' && !parser.allowBindings) {
parser.error(`Two-way binding is disabled`, attribute.start);
}

element.attributes.push(attribute);
parser.allowWhitespace();
}
Expand Down
6 changes: 4 additions & 2 deletions test/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import fs from 'fs';
import { svelte } from '../helpers.js';
import { svelte, tryToLoadJson } from '../helpers.js';

describe('parse', () => {
fs.readdirSync('test/parser/samples').forEach(dir => {
Expand All @@ -20,8 +20,10 @@ describe('parse', () => {
.readFileSync(`test/parser/samples/${dir}/input.html`, 'utf-8')
.replace(/\s+$/, '');

const options = tryToLoadJson(`test/parser/samples/${dir}/options.json`) || {};

try {
const actual = svelte.parse(input);
const actual = svelte.parse(input, options);
fs.writeFileSync(
`test/parser/samples/${dir}/_actual.json`,
JSON.stringify(actual, null, '\t')
Expand Down
8 changes: 8 additions & 0 deletions test/parser/samples/error-binding-disabled/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"message": "Two-way binding is disabled",
"loc": {
"line": 1,
"column": 7
},
"pos": 7
}
1 change: 1 addition & 0 deletions test/parser/samples/error-binding-disabled/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input bind:value='name'>
3 changes: 3 additions & 0 deletions test/parser/samples/error-binding-disabled/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bind": false
}