Skip to content

Commit 994a537

Browse files
support lazy-loading of TypeScript compiler (#93)
Also, support passing `true` for non-CJS config files. Co-authored-by: Conduitry <git@chor.date>
1 parent 57ba6ec commit 994a537

File tree

8 files changed

+452
-3
lines changed

8 files changed

+452
-3
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ module.exports = {
7777
// ...
7878
},
7979
settings: {
80-
'svelte3/typescript': require('typescript'), // pass the TypeScript package to the Svelte plugin
80+
'svelte3/typescript': () => require('typescript'), // pass the TypeScript package to the Svelte plugin
81+
// OR
82+
'svelte3/typescript': true, // load TypeScript as peer dependency
8183
// ...
8284
}
8385
};
@@ -162,7 +164,11 @@ The default is to not use named code blocks.
162164

163165
### `svelte3/typescript`
164166

165-
If you use TypeScript inside your Svelte components and want ESLint support, you need to set this option. It expects an instance of the TypeScript package. This probably means doing `'svelte3/typescript': require('typescript')`.
167+
If you use TypeScript inside your Svelte components and want ESLint support, you need to set this option. It expects a function returning an instance of the TypeScript package. This probably means doing `'svelte3/typescript': () => require('typescript')`.
168+
169+
To support ESLint configuration files that are not written in CommonJS, this can also be set to `true`, which behaves the same as `() => require('typescript')`.
170+
171+
For backwards compatibility, it also supports being passed the TypeScript package directly, but this is not generally recommended as it unnecessarily loads the package in some situations.
166172

167173
The default is to not enable TypeScript support.
168174

src/processor_options.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ Linter.prototype.verify = function(code, config, options) {
1717
processor_options.ignore_styles = settings['svelte3/ignore-styles'];
1818
processor_options.compiler_options = settings['svelte3/compiler-options'];
1919
processor_options.named_blocks = settings['svelte3/named-blocks'];
20-
processor_options.typescript = settings['svelte3/typescript'];
20+
processor_options.typescript =
21+
settings['svelte3/typescript'] === true
22+
? require('typescript')
23+
: typeof settings['svelte3/typescript'] === 'function'
24+
? settings['svelte3/typescript']()
25+
: settings['svelte3/typescript'];
2126
// call original Linter#verify
2227
return verify.call(this, code, config, options);
2328
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
4+
plugins: ['@typescript-eslint'],
5+
settings: {
6+
'svelte3/typescript': () => require('typescript'),
7+
},
8+
rules: {
9+
indent: ['error', 'tab'],
10+
semi: 'error',
11+
},
12+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script context="module">
2+
export const z = '';
3+
$: z2 = z;
4+
</script>
5+
6+
<script lang="ts">
7+
export let a: any;
8+
9+
export let b: any
10+
export let d: number;
11+
12+
const asd = d.asd();
13+
14+
interface A {}
15+
16+
function c(x) {}
17+
c(1);
18+
19+
$: z1 = z;
20+
</script>
21+
22+
<o>{b}</o>
23+
<p>{x}</p>
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
[
2+
{
3+
"ruleId": "module-script-reactive-declaration",
4+
"severity": 1,
5+
"line": 3,
6+
"column": 2,
7+
"endLine": 3,
8+
"endColumn": 12
9+
},
10+
{
11+
"ruleId": "no-undef",
12+
"severity": 2,
13+
"line": 3,
14+
"column": 5,
15+
"endLine": 3,
16+
"endColumn": 7
17+
},
18+
{
19+
"ruleId": "unused-export-let",
20+
"severity": 1,
21+
"line": 7,
22+
"column": 13,
23+
"endLine": 7,
24+
"endColumn": 19
25+
},
26+
{
27+
"ruleId": "@typescript-eslint/no-explicit-any",
28+
"severity": 1,
29+
"line": 7,
30+
"column": 16,
31+
"endLine": 7,
32+
"endColumn": 19,
33+
"suggestions": [
34+
{
35+
"messageId": "suggestUnknown",
36+
"fix": {
37+
"range": [
38+
58,
39+
61
40+
],
41+
"text": "unknown"
42+
},
43+
"desc": "Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct."
44+
},
45+
{
46+
"messageId": "suggestNever",
47+
"fix": {
48+
"range": [
49+
58,
50+
61
51+
],
52+
"text": "never"
53+
},
54+
"desc": "Use `never` instead, this is useful when instantiating generic type parameters that you don't need to know the type of."
55+
}
56+
]
57+
},
58+
{
59+
"ruleId": "indent",
60+
"severity": 2,
61+
"line": 9,
62+
"column": 2,
63+
"endLine": 9,
64+
"endColumn": 3,
65+
"fix": {
66+
"range": [
67+
112,
68+
113
69+
],
70+
"text": ""
71+
}
72+
},
73+
{
74+
"ruleId": "@typescript-eslint/no-explicit-any",
75+
"severity": 1,
76+
"line": 9,
77+
"column": 17,
78+
"endLine": 9,
79+
"endColumn": 20,
80+
"suggestions": [
81+
{
82+
"messageId": "suggestUnknown",
83+
"fix": {
84+
"range": [
85+
79,
86+
82
87+
],
88+
"text": "unknown"
89+
},
90+
"desc": "Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct."
91+
},
92+
{
93+
"messageId": "suggestNever",
94+
"fix": {
95+
"range": [
96+
79,
97+
82
98+
],
99+
"text": "never"
100+
},
101+
"desc": "Use `never` instead, this is useful when instantiating generic type parameters that you don't need to know the type of."
102+
}
103+
]
104+
},
105+
{
106+
"ruleId": "semi",
107+
"severity": 2,
108+
"line": 9,
109+
"column": 20,
110+
"endLine": 10,
111+
"endColumn": 2,
112+
"fix": {
113+
"range": [
114+
130,
115+
130
116+
],
117+
"text": ";"
118+
}
119+
},
120+
{
121+
"ruleId": "@typescript-eslint/no-unused-vars",
122+
"severity": 1,
123+
"line": 12,
124+
"column": 8,
125+
"endLine": 12,
126+
"endColumn": 11
127+
},
128+
{
129+
"ruleId": "@typescript-eslint/no-empty-interface",
130+
"severity": 2,
131+
"line": 14,
132+
"column": 12,
133+
"endLine": 14,
134+
"endColumn": 13
135+
},
136+
{
137+
"ruleId": "@typescript-eslint/no-unused-vars",
138+
"severity": 1,
139+
"line": 14,
140+
"column": 12,
141+
"endLine": 14,
142+
"endColumn": 13
143+
},
144+
{
145+
"ruleId": "@typescript-eslint/no-unused-vars",
146+
"severity": 1,
147+
"line": 16,
148+
"column": 13,
149+
"endLine": 16,
150+
"endColumn": 14
151+
},
152+
{
153+
"ruleId": "@typescript-eslint/no-empty-function",
154+
"severity": 2,
155+
"line": 16,
156+
"column": 16,
157+
"endLine": 16,
158+
"endColumn": 18
159+
},
160+
{
161+
"ruleId": "module-script-reactive-declaration",
162+
"severity": 1,
163+
"line": 19,
164+
"column": 10,
165+
"endLine": 19,
166+
"endColumn": 11
167+
},
168+
{
169+
"ruleId": "missing-declaration",
170+
"severity": 1,
171+
"line": 23,
172+
"column": 5,
173+
"endLine": 23,
174+
"endColumn": 6
175+
},
176+
{
177+
"ruleId": "no-undef",
178+
"severity": 2,
179+
"line": 23,
180+
"column": 5,
181+
"endLine": 23,
182+
"endColumn": 6
183+
}
184+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
4+
plugins: ['@typescript-eslint'],
5+
settings: {
6+
'svelte3/typescript': true,
7+
},
8+
rules: {
9+
indent: ['error', 'tab'],
10+
semi: 'error',
11+
},
12+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script context="module">
2+
export const z = '';
3+
$: z2 = z;
4+
</script>
5+
6+
<script lang="ts">
7+
export let a: any;
8+
9+
export let b: any
10+
export let d: number;
11+
12+
const asd = d.asd();
13+
14+
interface A {}
15+
16+
function c(x) {}
17+
c(1);
18+
19+
$: z1 = z;
20+
</script>
21+
22+
<o>{b}</o>
23+
<p>{x}</p>

0 commit comments

Comments
 (0)