-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8f377f
commit 067e90c
Showing
16 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
18.16.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/no-undefined-print' | ||
description: 'Disallow from printing `undefined`' | ||
since: 'v0.0.1' | ||
--- | ||
|
||
# svelte/no-undefined-print | ||
|
||
> Disallow from printing `undefined` | ||
- :gear: This rule is included in `"plugin:svelte/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports all uses of `{@html}` in order to reduce the risk of injecting potentially unsafe / unescaped html into the browser leading to Cross-Site Scripting (XSS) attacks. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/no-at-html-tags: "error" */ | ||
</script> | ||
<!-- ✓ GOOD --> | ||
{foo} | ||
<!-- ✗ BAD --> | ||
{@html foo} | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mute: When Not To Use It | ||
|
||
If you are certain the content passed to `{@html}` is sanitized HTML you can disable this rule. | ||
|
||
## :books: Further Reading | ||
|
||
- [Svelte - Tutorial > 1. Introduction / HTML tags](https://svelte.dev/tutorial/html-tags) | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in eslint-plugin-svelte v0.0.1 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/no-undefined-print.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/no-undefined-print.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/eslint-plugin-svelte/src/rules/no-undefined-print.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { AST } from 'svelte-eslint-parser'; | ||
import { createRule } from '../utils'; | ||
|
||
export default createRule('no-undefined-print', { | ||
meta: { | ||
docs: { | ||
description: 'Disallow from printing `undefined`', | ||
category: 'Possible Errors', | ||
recommended: true | ||
}, | ||
schema: [], | ||
messages: { | ||
unexpected: 'Unexpected `undefined`.' | ||
}, | ||
type: 'problem' | ||
}, | ||
create(context) { | ||
return { | ||
'SvelteMustacheTag[kind=text]'(node: AST.SvelteMustacheTag) { | ||
if (node.expression.type === 'Identifier' && node.expression.name === 'undefined') { | ||
context.report({ | ||
node, | ||
messageId: 'unexpected' | ||
}); | ||
} | ||
|
||
if (node.expression.type === 'LogicalExpression' && node.expression.operator === '||') { | ||
const left = node.expression.left; | ||
const right = node.expression.right; | ||
|
||
if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpected' | ||
}); | ||
} | ||
} | ||
|
||
if (node.expression.type === 'LogicalExpression' && node.expression.operator === '??') { | ||
const left = node.expression.left; | ||
const right = node.expression.right; | ||
|
||
if (left.type === 'Identifier' && right.type === 'Literal' && right.value === undefined) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpected' | ||
}); | ||
} | ||
} | ||
|
||
if (node.expression.type === 'ConditionalExpression') { | ||
const consequent = node.expression.consequent; | ||
const alternate = node.expression.alternate; | ||
|
||
if ( | ||
(consequent.type === 'Literal' && consequent.value === undefined) || | ||
(alternate.type === 'Literal' && alternate.value === undefined) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpected' | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...ges/eslint-plugin-svelte/tests/fixtures/rules/no-undefined-print/invalid/html-errors.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- message: 'Unexpected `undefined`aa.' | ||
line: 52 | ||
column: 44 | ||
suggestions: null |
5 changes: 5 additions & 0 deletions
5
...es/eslint-plugin-svelte/tests/fixtures/rules/no-undefined-print/invalid/html-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string = `this string contains some <strong>HTML!!!</strong>`; | ||
</script> | ||
|
||
<p>{@html string}</p> |
13 changes: 13 additions & 0 deletions
13
...s/eslint-plugin-svelte/tests/fixtures/rules/no-undefined-print/valid/debug01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let user = { | ||
firstname: 'Ada', | ||
lastname: 'Lovelace' | ||
}; | ||
</script> | ||
|
||
<input bind:value={user.firstname} /> | ||
<input bind:value={user.lastname} /> | ||
|
||
{@debug} | ||
|
||
<h1>Hello {user.firstname}!</h1> |
13 changes: 13 additions & 0 deletions
13
...s/eslint-plugin-svelte/tests/fixtures/rules/no-undefined-print/valid/debug02-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script> | ||
let user = { | ||
firstname: 'Ada', | ||
lastname: 'Lovelace' | ||
}; | ||
</script> | ||
|
||
<input bind:value={user.firstname} /> | ||
<input bind:value={user.lastname} /> | ||
|
||
{@debug user} | ||
|
||
<h1>Hello {user.firstname}!</h1> |
5 changes: 5 additions & 0 deletions
5
...ages/eslint-plugin-svelte/tests/fixtures/rules/no-undefined-print/valid/html-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string; | ||
</script> | ||
|
||
<p>{string}</p> |
5 changes: 5 additions & 0 deletions
5
...int-plugin-svelte/tests/fixtures/rules/no-undefined-print/valid/text-mustash-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
let string = 'abc'; | ||
</script> | ||
|
||
<p>{string}</p> |
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-svelte/tests/src/rules/no-undefined-print.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RuleTester } from '../../utils/eslint-compat'; | ||
import rule from '../../../src/rules/no-undefined-print'; | ||
import { loadTestCases } from '../../utils/utils'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('no-undefined-print', rule as any, loadTestCases('no-undefined-print')); |