Skip to content

Commit c778ba7

Browse files
committed
Refactor
1 parent 32a2621 commit c778ba7

File tree

5 files changed

+40
-44
lines changed

5 files changed

+40
-44
lines changed

explorer/src/components/ESLintEditor.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737
type: Boolean,
3838
},
3939
},
40-
emits: ["update:modelValue", "updateMessages"],
40+
emits: ["update:modelValue", "updateMessages", "time"],
4141
data() {
4242
return {
4343
fixedValue: this.modelValue,
@@ -85,7 +85,12 @@ export default {
8585
const options = this.useEslintPluginSvelte3
8686
? await this.getEslintPluginSvelte3Options()
8787
: {}
88+
89+
const start = Date.now()
8890
const messages = linter.verify(code, config, options)
91+
const time = Date.now() - start
92+
93+
this.$emit("time", time)
8994
9095
this.$emit("updateMessages", messages)
9196

explorer/src/components/ESLintPlayground.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
>svelte-eslint-parser is not used.</span
1414
>
1515
</template>
16+
<span style="margin-left: 16px">{{ time }}</span>
1617
</div>
1718
<div class="playground-content">
1819
<RulesSettings
@@ -27,6 +28,7 @@
2728
class="eslint-playground"
2829
:use-eslint-plugin-svelte3="useEslintPluginSvelte3"
2930
@update-messages="onUpdateMessages"
31+
@time="(t) => (time = `${t}ms`)"
3032
/>
3133
<div class="messages">
3234
<ol>
@@ -91,6 +93,7 @@ export default {
9193
rules: state.rules || Object.assign({}, DEFAULT_RULES_CONFIG),
9294
messages: [],
9395
useEslintPluginSvelte3: Boolean(state.useEslintPluginSvelte3),
96+
time: "",
9497
}
9598
},
9699
computed: {

src/context/index.ts

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import type ESTree from "estree"
44
import type { ScopeManager } from "eslint-scope"
55
import { TemplateScopeManager } from "./template-scope-manager"
66

7+
type ContextSourceCode = {
8+
svelte: string
9+
script: {
10+
code: string
11+
attrs: Record<string, string | undefined>
12+
}
13+
}
714
export class Context {
815
public readonly code: string
916

1017
public readonly parserOptions: any
1118

12-
public readonly sourceCode: {
13-
svelte: string
14-
script: {
15-
code: string
16-
attrs: Record<string, string | undefined>
17-
}
18-
}
19+
public readonly sourceCode: ContextSourceCode
1920

2021
public readonly tokens: Token[] = []
2122

@@ -31,43 +32,30 @@ export class Context {
3132
this.code = code
3233
this.parserOptions = parserOptions
3334
this.locs = new LinesAndColumns(code)
34-
const sourceCode: {
35-
svelte: string
36-
script: {
37-
code: string
38-
ranges: {
39-
code: [number, number]
40-
tag: [number, number]
41-
}[]
42-
attrs: Record<string, string | undefined>
43-
}
44-
} = {
45-
svelte: code,
35+
36+
let svelteCode = ""
37+
let scriptCode = ""
38+
let scriptAttrs: Record<string, string | undefined> = {}
39+
40+
let start = 0
41+
for (const script of extractScriptBlocks(code)) {
42+
const before = code.slice(start, script.codeRange[0])
43+
svelteCode += before + script.code.replace(/[^\n\r ]/g, " ")
44+
scriptCode += before.replace(/[^\n\r ]/g, " ") + script.code
45+
scriptAttrs = Object.assign(scriptAttrs, script.attrs)
46+
start = script.codeRange[1]
47+
}
48+
const before = code.slice(start)
49+
svelteCode += before
50+
scriptCode += before.replace(/[^\n\r ]/g, " ")
51+
52+
this.sourceCode = {
53+
svelte: svelteCode,
4654
script: {
47-
code: code.replace(/[^\n\r ]/g, " "),
48-
ranges: [],
49-
attrs: {},
55+
code: scriptCode,
56+
attrs: scriptAttrs,
5057
},
5158
}
52-
for (const script of extractScriptBlocks(code)) {
53-
sourceCode.svelte =
54-
sourceCode.svelte.slice(0, script.codeRange[0]) +
55-
script.code.replace(/[^\n\r ]/g, " ") +
56-
sourceCode.svelte.slice(script.codeRange[1])
57-
sourceCode.script.code =
58-
sourceCode.script.code.slice(0, script.codeRange[0]) +
59-
script.code +
60-
sourceCode.script.code.slice(script.codeRange[1])
61-
sourceCode.script.ranges.push({
62-
code: script.codeRange,
63-
tag: script.tagRange,
64-
})
65-
sourceCode.script.attrs = Object.assign(
66-
sourceCode.script.attrs,
67-
script.attrs,
68-
)
69-
}
70-
this.sourceCode = sourceCode
7159
}
7260

7361
public getLocFromIndex(index: number): { line: number; column: number } {

src/parser/converts/root.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function extractAttributes(
170170
) {
171171
const script = element.type === "SvelteScriptElement"
172172
const code =
173-
ctx.sourceCode.svelte.slice(0, element.range[0]).replace(/./g, " ") +
173+
" ".repeat(element.range[0]) +
174174
ctx.sourceCode.svelte
175175
.slice(...element.range)
176176
.replace(

src/parser/espree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface ESLintCustomParser {
1717

1818
const createRequire: (filename: string) => (modName: string) => any =
1919
// Added in v12.2.0
20-
(Module as any).createRequire ||
20+
Module.createRequire ||
2121
// Added in v10.12.0, but deprecated in v12.2.0.
2222
Module.createRequireFromPath ||
2323
// Polyfill - This is not executed on the tests on node@>=10.

0 commit comments

Comments
 (0)