diff --git a/.eslintrc.js b/.eslintrc.js index edc9ebf..5c55384 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -17,6 +17,35 @@ module.exports = { }, rules: { "@typescript-eslint/no-non-null-assertion": "off", + "@typescript-eslint/naming-convention": [ + "error", + { + selector: "default", + format: ["camelCase"], + leadingUnderscore: "allow", + trailingUnderscore: "allow", + }, + + { + selector: "variable", + format: ["camelCase", "UPPER_CASE"], + leadingUnderscore: "allow", + trailingUnderscore: "allow", + }, + + { + selector: "typeLike", + format: ["PascalCase"], + }, + { + selector: "property", + format: ["camelCase", "PascalCase"], + }, + { + selector: "method", + format: ["camelCase", "PascalCase"], + }, + ], }, }, ], diff --git a/package.json b/package.json index cbcc3ab..c3b6f29 100644 --- a/package.json +++ b/package.json @@ -39,16 +39,16 @@ }, "homepage": "https://github.com/ota-meshi/jsonc-eslint-parser#readme", "devDependencies": { - "@ota-meshi/eslint-plugin": "0.0.0", + "@ota-meshi/eslint-plugin": ">=0.0.0", "@types/eslint": "^7.2.0", "@types/eslint-visitor-keys": "^1.0.0", - "@types/estree": "0.0.44", + "@types/estree": "^0.0.45", "@types/mocha": "^7.0.2", "@types/natural-compare": "^1.4.0", "@types/node": "^14.0.13", "@types/semver": "^7.3.1", - "@typescript-eslint/eslint-plugin": "4.0.0-alpha.9", - "@typescript-eslint/parser": "4.0.0-alpha.9", + "@typescript-eslint/eslint-plugin": "^4.0.0-0", + "@typescript-eslint/parser": "^4.0.0-0", "eslint": "^7.3.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-eslint-comments": "^3.2.0", diff --git a/src/index.ts b/src/index.ts index fa6a0ea..8dc4d83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ import { KEYS } from "./parser/visitor-keys" // parser export { parseForESLint } // Keys +// eslint-disable-next-line @typescript-eslint/naming-convention -- parser module export const VisitorKeys = KEYS // tools diff --git a/src/parser/convert.ts b/src/parser/convert.ts index c19edb5..a74db89 100644 --- a/src/parser/convert.ts +++ b/src/parser/convert.ts @@ -412,10 +412,12 @@ function convertLiteralNode( return nn } +/* eslint-disable complexity -- ignore */ /** * Validate literal */ function validateLiteral(node: Literal, ctx: JSONSyntaxContext) { + /* eslint-enable complexity -- ignore */ const value = node.value if ( (!ctx.invalidJsonNumbers || diff --git a/src/parser/errors.ts b/src/parser/errors.ts index 045b922..9d6d5a7 100644 --- a/src/parser/errors.ts +++ b/src/parser/errors.ts @@ -8,7 +8,9 @@ import type { Comment } from "../types" */ export class ParseError extends SyntaxError { public index: number + public lineNumber: number + public column: number /** diff --git a/src/parser/espree.ts b/src/parser/espree.ts index 15ab0a6..83a0f98 100644 --- a/src/parser/espree.ts +++ b/src/parser/espree.ts @@ -40,6 +40,7 @@ export function getEspree(): ESPree { } } } + // eslint-disable-next-line @typescript-eslint/no-require-imports -- special require return espreeCache || (espreeCache = require("espree")) } @@ -47,10 +48,10 @@ export function getEspree(): ESPree { * createRequire */ function createRequire(filename: string) { - // eslint-disable-next-line @typescript-eslint/no-var-requires -- special require + // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports, @typescript-eslint/naming-convention -- special require const Module = require("module") const fn: ( - filename: string, + fileName: string, ) => // eslint-disable-next-line @typescript-eslint/no-explicit-any -- any any = // Added in v12.2.0 diff --git a/src/parser/token-store.ts b/src/parser/token-store.ts index 1d8a399..53a33a6 100644 --- a/src/parser/token-store.ts +++ b/src/parser/token-store.ts @@ -9,7 +9,8 @@ export type MaybeNodeOrToken = { // type TokenType = AST.TokenType | "Template" export class TokenStore { - private tokens: AST.Token[] + private readonly tokens: AST.Token[] + public constructor(tokens: AST.Token[]) { this.tokens = tokens } @@ -19,6 +20,7 @@ export class TokenStore { (token) => token.range[0] <= offset && offset < token.range[1], ) } + public findTokenByOffset(offset: number): AST.Token | null { return this.tokens[this.findIndexByOffset(offset)] } @@ -30,6 +32,7 @@ export class TokenStore { public getFirstToken(nodeOrToken: MaybeNodeOrToken): AST.Token { return this.findTokenByOffset(nodeOrToken.range![0])! } + /** * Get the last token representing the given node. * @@ -37,6 +40,7 @@ export class TokenStore { public getLastToken(nodeOrToken: MaybeNodeOrToken): AST.Token { return this.findTokenByOffset(nodeOrToken.range![1] - 1)! } + /** * Get the first token before the given node or token. */ @@ -54,6 +58,7 @@ export class TokenStore { } return null } + /** * Get the first token after the given node or token. */ diff --git a/src/parser/traverse.ts b/src/parser/traverse.ts index 8bff9a8..b18abb6 100644 --- a/src/parser/traverse.ts +++ b/src/parser/traverse.ts @@ -75,7 +75,7 @@ export function* getNodes( } /** - * Check wheather a given value is a node. + * Check whether a given value is a node. * @param x The value to check. * @returns `true` if the value is a node. */ diff --git a/src/utils/ast.ts b/src/utils/ast.ts index dcc3724..4fab31f 100644 --- a/src/utils/ast.ts +++ b/src/utils/ast.ts @@ -76,35 +76,6 @@ export type JSONValue = | bigint export type JSONObjectValue = { [key: string]: JSONValue } -export function getStaticJSONValue( - node: JSONUnaryExpression | JSONNumberIdentifier | JSONNumberLiteral, -): number -export function getStaticJSONValue(node: JSONUndefinedIdentifier): undefined -export function getStaticJSONValue(node: JSONTemplateLiteral): string -export function getStaticJSONValue(node: JSONTemplateElement): string -export function getStaticJSONValue(node: JSONStringLiteral): string -export function getStaticJSONValue(node: JSONNumberLiteral): number -export function getStaticJSONValue(node: JSONKeywordLiteral): boolean | null -export function getStaticJSONValue(node: JSONRegExpLiteral): RegExp -export function getStaticJSONValue(node: JSONBigIntLiteral): bigint -export function getStaticJSONValue( - node: JSONLiteral, -): string | number | boolean | RegExp | bigint | null - -export function getStaticJSONValue(node: JSONObjectExpression): JSONObjectValue -export function getStaticJSONValue(node: JSONArrayExpression): JSONValue[] -export function getStaticJSONValue( - node: JSONExpression | JSONExpressionStatement, -): JSONValue -export function getStaticJSONValue(node: JSONProgram): JSONValue -export function getStaticJSONValue(node: JSONNode): JSONValue -/** - * Gets the static value for the given node. - */ -export function getStaticJSONValue(node: JSONNode): JSONValue { - return resolver[node.type](node) -} - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- any const resolver: { [key in JSONNode["type"]]: (node: any) => JSONValue } = { Program(node: JSONProgram) { @@ -185,3 +156,30 @@ const resolver: { [key in JSONNode["type"]]: (node: any) => JSONValue } = { return node.value.cooked }, } + +export function getStaticJSONValue( + node: JSONUnaryExpression | JSONNumberIdentifier | JSONNumberLiteral, +): number +export function getStaticJSONValue(node: JSONUndefinedIdentifier): undefined +export function getStaticJSONValue( + node: JSONTemplateLiteral | JSONTemplateElement | JSONStringLiteral, +): string +export function getStaticJSONValue(node: JSONKeywordLiteral): boolean | null +export function getStaticJSONValue(node: JSONRegExpLiteral): RegExp +export function getStaticJSONValue(node: JSONBigIntLiteral): bigint +export function getStaticJSONValue( + node: JSONLiteral, +): string | number | boolean | RegExp | bigint | null + +export function getStaticJSONValue(node: JSONObjectExpression): JSONObjectValue +export function getStaticJSONValue(node: JSONArrayExpression): JSONValue[] +export function getStaticJSONValue( + node: JSONExpression | JSONExpressionStatement | JSONProgram | JSONNode, +): JSONValue + +/** + * Gets the static value for the given node. + */ +export function getStaticJSONValue(node: JSONNode): JSONValue { + return resolver[node.type](node) +} diff --git a/tests/src/utils/ast.ts b/tests/src/utils/ast.ts index 9fd4407..7961940 100644 --- a/tests/src/utils/ast.ts +++ b/tests/src/utils/ast.ts @@ -129,6 +129,7 @@ describe("getStaticJSONValue", () => { assert.strictEqual( // eslint-disable-next-line @typescript-eslint/no-explicit-any -- any stringify(getStaticJSONValue(ast as any)), + // eslint-disable-next-line no-eval -- for test stringify(eval(`(${code})`)), ) })