Skip to content

Commit d130714

Browse files
authored
Minor refactor (#124)
1 parent 8bb2ac7 commit d130714

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

benchmark/index.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// eslint-disable-next-line eslint-comments/disable-enable-pair -- ignore
2+
/* eslint-disable require-jsdoc, no-console -- ignore */
3+
import * as Benchmark from "benchmark"
4+
import fs from "fs"
5+
import { parseForESLint } from "../src/index"
6+
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser"
7+
8+
const contents = `${fs.readFileSync(
9+
require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"),
10+
"utf-8",
11+
)}// comments`
12+
13+
type Result = { name: string; hz: number }
14+
const results: Result[] = []
15+
16+
function format(hz: number): string {
17+
return (~~(hz * 100) / 100).toString().padEnd(4, " ").padStart(6, " ")
18+
}
19+
20+
function onCycle(event: { target: Result }): void {
21+
const { name, hz } = event.target
22+
results.push({ name, hz })
23+
24+
console.log(event.target.toString())
25+
}
26+
27+
function onComplete(): void {
28+
console.log("-".repeat(72))
29+
const map: Record<string, number[]> = {}
30+
for (const result of results) {
31+
const r = (map[result.name.slice(2)] ??= [])
32+
r.push(result.hz)
33+
}
34+
for (const name of Object.keys(map)) {
35+
console.log(
36+
`${name.padEnd(15)} ${format(
37+
map[name].reduce((p, a) => p + a, 0) / map[name].length,
38+
)} ops/sec`,
39+
)
40+
}
41+
for (let i = 0; i < results.length; ++i) {
42+
const result = results[i]
43+
44+
console.log(`${result.name.padEnd(15)} ${format(result.hz)} ops/sec`)
45+
}
46+
}
47+
48+
const suite = new Benchmark.Suite("benchmark", { onCycle, onComplete })
49+
50+
for (const no of [1, 2, 3]) {
51+
suite.add(`${no} new svelte-eslint-parser`, function () {
52+
parseForESLint(contents, {
53+
loc: true,
54+
range: true,
55+
raw: true,
56+
tokens: true,
57+
comment: true,
58+
eslintVisitorKeys: true,
59+
eslintScopeManager: true,
60+
})
61+
})
62+
suite.add(`${no} old svelte-eslint-parser`, function () {
63+
parseOld(contents, {
64+
loc: true,
65+
range: true,
66+
raw: true,
67+
tokens: true,
68+
comment: true,
69+
eslintVisitorKeys: true,
70+
eslintScopeManager: true,
71+
})
72+
})
73+
}
74+
75+
suite.run()

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot",
2121
"preversion": "npm run lint && npm test",
2222
"update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
23-
"eslint-playground": "eslint tests/fixtures --ext .svelte --config .eslintrc-for-playground.js --format codeframe"
23+
"eslint-playground": "eslint tests/fixtures --ext .svelte --config .eslintrc-for-playground.js --format codeframe",
24+
"benchmark": "ts-node --transpile-only benchmark/index.ts"
2425
},
2526
"repository": {
2627
"type": "git",
@@ -48,7 +49,8 @@
4849
},
4950
"devDependencies": {
5051
"@ota-meshi/eslint-plugin": "^0.10.0",
51-
"@ota-meshi/eslint-plugin-svelte": "^0.18.0",
52+
"@ota-meshi/eslint-plugin-svelte": "^0.18.1",
53+
"@types/benchmark": "^2.1.1",
5254
"@types/eslint": "^8.0.0",
5355
"@types/eslint-scope": "^3.7.0",
5456
"@types/eslint-visitor-keys": "^1.0.0",
@@ -57,6 +59,7 @@
5759
"@types/semver": "^7.3.9",
5860
"@typescript-eslint/eslint-plugin": "^5.4.0",
5961
"@typescript-eslint/parser": "^5.4.0",
62+
"benchmark": "^2.1.4",
6063
"code-red": "^0.2.3",
6164
"eslint": "^8.2.0",
6265
"eslint-config-prettier": "^8.3.0",

src/context/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class ScriptsSourceCode {
6161
this.trimmedRaw.slice(end)
6262
}
6363
}
64+
6465
export type ContextSourceCode = {
6566
template: string
6667
scripts: ScriptsSourceCode
@@ -91,26 +92,28 @@ export class Context {
9192
this.parserOptions = parserOptions
9293
this.locs = new LinesAndColumns(code)
9394

95+
const spaces = code.replace(/[^\n\r ]/g, " ")
96+
9497
let templateCode = ""
9598
let scriptCode = ""
9699
let scriptAttrs: Record<string, string | undefined> = {}
97100

98101
let start = 0
99102
for (const block of extractBlocks(code)) {
100-
const before = code.slice(start, block.codeRange[0])
101-
const blankCode = block.code.replace(/[^\n\r ]/g, " ")
102-
templateCode += before + blankCode
103+
templateCode +=
104+
code.slice(start, block.codeRange[0]) +
105+
spaces.slice(block.codeRange[0], block.codeRange[1])
103106
if (block.tag === "script") {
104-
scriptCode += before.replace(/[^\n\r ]/g, " ") + block.code
107+
scriptCode +=
108+
spaces.slice(start, block.codeRange[0]) + block.code
105109
scriptAttrs = Object.assign(scriptAttrs, block.attrs)
106110
} else {
107-
scriptCode += before.replace(/[^\n\r ]/g, " ") + blankCode
111+
scriptCode += spaces.slice(start, block.codeRange[1])
108112
}
109113
start = block.codeRange[1]
110114
}
111-
const after = code.slice(start)
112-
templateCode += after
113-
scriptCode += after.replace(/[^\n\r ]/g, " ")
115+
templateCode += code.slice(start)
116+
scriptCode += spaces.slice(start)
114117

115118
this.sourceCode = {
116119
template: templateCode,

tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,10 @@
1212
"noUnusedParameters": true,
1313
"esModuleInterop": true
1414
},
15-
"include": ["src/**/*.ts", "tests/**/*.ts", "tools/**/*.ts"]
15+
"include": [
16+
"src/**/*.ts",
17+
"tests/**/*.ts",
18+
"tools/**/*.ts",
19+
"benchmark/**/*.ts"
20+
]
1621
}

0 commit comments

Comments
 (0)