Skip to content

Minor refactor #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair -- ignore
/* eslint-disable require-jsdoc, no-console -- ignore */
import * as Benchmark from "benchmark"
import fs from "fs"
import { parseForESLint } from "../src/index"
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser"

const contents = `${fs.readFileSync(
require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"),
"utf-8",
)}// comments`

type Result = { name: string; hz: number }
const results: Result[] = []

function format(hz: number): string {
return (~~(hz * 100) / 100).toString().padEnd(4, " ").padStart(6, " ")
}

function onCycle(event: { target: Result }): void {
const { name, hz } = event.target
results.push({ name, hz })

console.log(event.target.toString())
}

function onComplete(): void {
console.log("-".repeat(72))
const map: Record<string, number[]> = {}
for (const result of results) {
const r = (map[result.name.slice(2)] ??= [])
r.push(result.hz)
}
for (const name of Object.keys(map)) {
console.log(
`${name.padEnd(15)} ${format(
map[name].reduce((p, a) => p + a, 0) / map[name].length,
)} ops/sec`,
)
}
for (let i = 0; i < results.length; ++i) {
const result = results[i]

console.log(`${result.name.padEnd(15)} ${format(result.hz)} ops/sec`)
}
}

const suite = new Benchmark.Suite("benchmark", { onCycle, onComplete })

for (const no of [1, 2, 3]) {
suite.add(`${no} new svelte-eslint-parser`, function () {
parseForESLint(contents, {
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
})
})
suite.add(`${no} old svelte-eslint-parser`, function () {
parseOld(contents, {
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
})
})
}

suite.run()
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot",
"preversion": "npm run lint && npm test",
"update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
"eslint-playground": "eslint tests/fixtures --ext .svelte --config .eslintrc-for-playground.js --format codeframe"
"eslint-playground": "eslint tests/fixtures --ext .svelte --config .eslintrc-for-playground.js --format codeframe",
"benchmark": "ts-node --transpile-only benchmark/index.ts"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,7 +49,8 @@
},
"devDependencies": {
"@ota-meshi/eslint-plugin": "^0.10.0",
"@ota-meshi/eslint-plugin-svelte": "^0.18.0",
"@ota-meshi/eslint-plugin-svelte": "^0.18.1",
"@types/benchmark": "^2.1.1",
"@types/eslint": "^8.0.0",
"@types/eslint-scope": "^3.7.0",
"@types/eslint-visitor-keys": "^1.0.0",
Expand All @@ -57,6 +59,7 @@
"@types/semver": "^7.3.9",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"benchmark": "^2.1.4",
"code-red": "^0.2.3",
"eslint": "^8.2.0",
"eslint-config-prettier": "^8.3.0",
Expand Down
19 changes: 11 additions & 8 deletions src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class ScriptsSourceCode {
this.trimmedRaw.slice(end)
}
}

export type ContextSourceCode = {
template: string
scripts: ScriptsSourceCode
Expand Down Expand Up @@ -91,26 +92,28 @@ export class Context {
this.parserOptions = parserOptions
this.locs = new LinesAndColumns(code)

const spaces = code.replace(/[^\n\r ]/g, " ")

let templateCode = ""
let scriptCode = ""
let scriptAttrs: Record<string, string | undefined> = {}

let start = 0
for (const block of extractBlocks(code)) {
const before = code.slice(start, block.codeRange[0])
const blankCode = block.code.replace(/[^\n\r ]/g, " ")
templateCode += before + blankCode
templateCode +=
code.slice(start, block.codeRange[0]) +
spaces.slice(block.codeRange[0], block.codeRange[1])
if (block.tag === "script") {
scriptCode += before.replace(/[^\n\r ]/g, " ") + block.code
scriptCode +=
spaces.slice(start, block.codeRange[0]) + block.code
scriptAttrs = Object.assign(scriptAttrs, block.attrs)
} else {
scriptCode += before.replace(/[^\n\r ]/g, " ") + blankCode
scriptCode += spaces.slice(start, block.codeRange[1])
}
start = block.codeRange[1]
}
const after = code.slice(start)
templateCode += after
scriptCode += after.replace(/[^\n\r ]/g, " ")
templateCode += code.slice(start)
scriptCode += spaces.slice(start)

this.sourceCode = {
template: templateCode,
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"noUnusedParameters": true,
"esModuleInterop": true
},
"include": ["src/**/*.ts", "tests/**/*.ts", "tools/**/*.ts"]
"include": [
"src/**/*.ts",
"tests/**/*.ts",
"tools/**/*.ts",
"benchmark/**/*.ts"
]
}