Skip to content

Commit

Permalink
Use YAML v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 6, 2022
1 parent 05deefa commit 0c40d5e
Show file tree
Hide file tree
Showing 108 changed files with 18,983 additions and 1,338 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
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
26 changes: 26 additions & 0 deletions explorer/src/components/AstExplorer.vue
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"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"
},
Expand All @@ -40,7 +40,7 @@
"dependencies": {
"eslint-visitor-keys": "^3.0.0",
"lodash": "^4.17.21",
"yaml": "^1.10.2"
"yaml": "^2.0.0-0"
},
"devDependencies": {
"@ota-meshi/eslint-plugin": "^0.10.0",
Expand Down
52 changes: 30 additions & 22 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import type {
Token,
YAMLProgram,
} from "./ast"
import type { ASTNode } from "./yaml"
import lodash from "lodash"
import { traverseNodes } from "./traverse"
import type { CST } from "yaml"
import { ParseError } from "."

type CSTRangeData = {
start: number
end: number
}
export class Context {
public readonly code: string

Expand Down Expand Up @@ -109,12 +106,9 @@ export class Context {
}

/**
* Get the location information of the given node.
* @param node The node.
* Get the location information of the given range.
*/
public getConvertLocation(node: { range: Range } | ASTNode): Locations {
const [start, end] = node.range!

public getConvertLocation(start: number, end: number): Locations {
return {
range: [start, end],
loc: {
Expand All @@ -124,32 +118,46 @@ export class Context {
}
}

/**
* Get the location information of the given CSTRange.
* @param node The node.
*/
public getConvertLocationFromCSTRange(
range: CSTRangeData | undefined | null,
): Locations {
return this.getConvertLocation({ range: [range!.start, range!.end] })
}

public addComment(comment: Comment): void {
this.comments.push(comment)
}

/**
* Add token to tokens
*/
public addToken(type: Token["type"], range: Range): Token {
public addToken(type: Token["type"], range: Readonly<Range>): Token {
const token = {
type,
value: this.code.slice(...range),
...this.getConvertLocation({ range }),
...this.getConvertLocation(...range),
}
this.tokens.push(token)
return token
}

public throwUnexpectedTokenError(cst: CST.Token): ParseError {
const token = "source" in cst ? `'${cst.source}'` : cst.type
throw this.throwError(`Unexpected token: ${token}`, cst)
}

public throwError(message: string, cst: CST.Token | number): ParseError {
const offset = typeof cst === "number" ? cst : cst.offset
const loc = this.getLocFromIndex(offset)
throw new ParseError(message, offset, loc.line, loc.column)
}

/**
* Gets the last index with whitespace skipped.
*/
public lastSkipSpaces(startIndex: number, endIndex: number): number {
const str = this.code
for (let index = endIndex - 1; index >= startIndex; index--) {
if (str[index].trim()) {
return index + 1
}
}
return startIndex
}
}

class LinesAndColumns {
Expand Down

0 comments on commit 0c40d5e

Please sign in to comment.