Skip to content

Commit fa1cfcc

Browse files
authored
Add ignoreWarnings option to valid-compile rule (#42)
* Add ignoreWarnings option to valid-compile rule * Add test
1 parent 5176019 commit fa1cfcc

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

docs/rules/valid-compile.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,34 @@ Note that we exclude reports for some checks, such as `missing-declaration`, whi
3737

3838
## :wrench: Options
3939

40-
Nothing.
40+
```json
41+
{
42+
"@ota-meshi/svelte/valid-compile": [
43+
"error",
44+
{
45+
"ignoreWarnings": false
46+
}
47+
]
48+
}
49+
```
50+
51+
- `ignoreWarnings` ... If set to `true`, ignores any warnings other than fatal errors reported by the svelte compiler.
52+
53+
<eslint-code-block>
54+
55+
<!--eslint-skip-->
56+
57+
```svelte
58+
<script>
59+
/* eslint @ota-meshi/svelte/valid-compile: ["error", { ignoreWarnings: true }] */
60+
let src = "tutorial/image.gif"
61+
</script>
62+
63+
<!-- Ignore -->
64+
<img {src} />
65+
```
66+
67+
</eslint-code-block>
4168

4269
## :rocket: Version
4370

src/rules/valid-compile.ts

+33-21
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ export default createRule("valid-compile", {
6767
category: "Possible Errors",
6868
recommended: false,
6969
},
70-
schema: [],
70+
schema: [
71+
{
72+
type: "object",
73+
properties: {
74+
ignoreWarnings: { type: "boolean" },
75+
},
76+
additionalProperties: false,
77+
},
78+
],
7179
messages: {},
7280
type: "problem",
7381
},
7482
create(context) {
83+
const ignoreWarnings = Boolean(context.options[0]?.ignoreWarnings)
7584
const sourceCode = context.getSourceCode()
7685
const text = sourceCode.text
7786

@@ -320,29 +329,32 @@ export default createRule("valid-compile", {
320329
report(getWarnings(code), (warn) => remapContext.remapLocs(warn))
321330
},
322331
}
332+
333+
/**
334+
* Get compile warnings
335+
*/
336+
function getWarnings(code: string): Warning[] {
337+
try {
338+
const result = compiler.compile(code, { generate: false })
339+
340+
if (ignoreWarnings) {
341+
return []
342+
}
343+
return result.warnings
344+
} catch (e) {
345+
// console.log(code)
346+
return [
347+
{
348+
message: e.message,
349+
start: e.start,
350+
end: e.end,
351+
},
352+
]
353+
}
354+
}
323355
},
324356
})
325357

326-
/**
327-
* Get compile warnings
328-
*/
329-
function getWarnings(code: string): Warning[] {
330-
try {
331-
const result = compiler.compile(code, { generate: false })
332-
333-
return result.warnings
334-
} catch (e) {
335-
// console.log(code)
336-
return [
337-
{
338-
message: e.message,
339-
start: e.start,
340-
end: e.end,
341-
},
342-
]
343-
}
344-
}
345-
346358
/**
347359
* Checks if the given visitorKeys are the equals.
348360
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"options": [{ "ignoreWarnings": true }]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let src = "tutorial/image.gif"
3+
</script>
4+
5+
<img {src} />

0 commit comments

Comments
 (0)