Skip to content

Commit

Permalink
Merge e20a1e6 into 05deefa
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 7, 2022
2 parents 05deefa + e20a1e6 commit 161ce35
Show file tree
Hide file tree
Showing 168 changed files with 19,629 additions and 1,523 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -18,6 +18,7 @@ module.exports = {
"require-jsdoc": "error",
"no-warning-comments": "warn",
"no-lonely-if": "off",
"one-var": "off",
},
overrides: [
{
Expand Down Expand Up @@ -59,6 +60,8 @@ module.exports = {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-invalid-this": "off",
"@typescript-eslint/no-invalid-this": ["error"],
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/NodeCI.yml
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 17.x]
node-version: [14.x, 16.x, 17.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
80 changes: 80 additions & 0 deletions benchmark/index.ts
@@ -0,0 +1,80 @@
// 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 path from "path"
import { parseForESLint } from ".."
import { parseForESLint as parseOld } from "../node_modules/yaml-eslint-parser"

const contents = `${fs.readFileSync(
path.resolve(
__dirname,
"../tests/fixtures/parser/ast/astexplorer-input.yaml",
),
"utf-8",
)}
`.repeat(10)

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 yaml-eslint-parser`, function () {
parseForESLint(contents, {
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
})
})
suite.add(`${no} old yaml-eslint-parser`, function () {
parseOld(contents, {
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
})
})
}

suite.run()
26 changes: 26 additions & 0 deletions explorer/src/components/AstExplorer.vue
Expand Up @@ -25,6 +25,30 @@
import MonacoEditor from "./MonacoEditor.vue"
import AstOptions from "./AstOptions.vue"
import * as yamlEslintParser from "../../.."
import { LineCounter, Parser, Composer } from "yaml"
/* eslint-disable no-unused-vars -- ignore */
/** parse CST for test */
function parseAllDocsToCST(
/* eslint-enable no-unused-vars -- ignore */
code,
) {
const lineCounter = new LineCounter()
const parser = new Parser(lineCounter.addNewLine)
const composer = new Composer()
const cstNodes = []
const nodes = Array.from(
composer.compose(
(function* () {
for (const cst of parser.parse(code)) {
cstNodes.push(cst)
yield cst
}
})(),
),
)
return { cstNodes, nodes }
}
export default {
name: "AstExplorer",
Expand Down Expand Up @@ -61,6 +85,8 @@ export default {
const start = Date.now()
try {
ast = yamlEslintParser.parseForESLint(this.yamlValue).ast
// for test
// ast = parseAllDocsToCST(this.yamlValue)
} catch (e) {
ast = {
message: e.message,
Expand Down
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"lib"
],
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
"node": "^14.17.0 || >=16.0.0"
},
"scripts": {
"prebuild": "npm run -s clean",
Expand All @@ -17,9 +17,10 @@
"eslint-fix": "npm run lint -- --fix",
"test": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"cover": "nyc --reporter=lcov npm run test",
"debug": "mocha --require ts-node/register/transpile-only --inspect \"tests/src/**/*.ts\" --reporter dot",
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot",
"preversion": "npm run lint && npm test",
"update-fixtures": "ts-node ./tools/update-fixtures.ts"
"update-fixtures": "ts-node ./tools/update-fixtures.ts",
"benchmark": "ts-node --transpile-only benchmark/index.ts"
},
"repository": {
"type": "git",
Expand All @@ -40,17 +41,19 @@
"dependencies": {
"eslint-visitor-keys": "^3.0.0",
"lodash": "^4.17.21",
"yaml": "^1.10.2"
"yaml": "^2.0.0"
},
"devDependencies": {
"@ota-meshi/eslint-plugin": "^0.10.0",
"@types/benchmark": "^2.1.1",
"@types/eslint": "^8.0.0",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/lodash": "^4.14.167",
"@types/mocha": "^9.0.0",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"benchmark": "^2.1.4",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.0.0",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand Down
22 changes: 21 additions & 1 deletion src/ast.ts
Expand Up @@ -67,11 +67,30 @@ export interface YAMLDocument extends BaseYAMLNode {
anchors: { [key: string]: YAMLAnchor[] }
}

export interface YAMLDirective extends BaseYAMLNode {
interface BaseYAMLDirective extends BaseYAMLNode {
type: "YAMLDirective"
value: string
kind: "YAML" | "TAG" | null
parent: YAMLDocument
}
export interface YAMLDirectiveForYAML extends BaseYAMLDirective {
kind: "YAML"
version: string
}
export interface YAMLDirectiveForTAG extends BaseYAMLDirective {
kind: "TAG"
handle: string
prefix: string
}

export interface YAMLDirectiveForUnknown extends BaseYAMLDirective {
kind: null
}

export type YAMLDirective =
| YAMLDirectiveForYAML
| YAMLDirectiveForTAG
| YAMLDirectiveForUnknown

export interface YAMLWithMeta extends BaseYAMLNode {
type: "YAMLWithMeta"
Expand All @@ -90,6 +109,7 @@ export interface YAMLAnchor extends BaseYAMLNode {
export interface YAMLTag extends BaseYAMLNode {
type: "YAMLTag"
tag: string
raw: string
parent: YAMLWithMeta
}

Expand Down

0 comments on commit 161ce35

Please sign in to comment.