Skip to content
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
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
},
},
],
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/parser/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
2 changes: 2 additions & 0 deletions src/parser/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import type { Comment } from "../types"
*/
export class ParseError extends SyntaxError {
public index: number

public lineNumber: number

public column: number

/**
Expand Down
5 changes: 3 additions & 2 deletions src/parser/espree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ export function getEspree(): ESPree {
}
}
}
// eslint-disable-next-line @typescript-eslint/no-require-imports -- special require
return espreeCache || (espreeCache = require("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
Expand Down
7 changes: 6 additions & 1 deletion src/parser/token-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)]
}
Expand All @@ -30,13 +32,15 @@ export class TokenStore {
public getFirstToken(nodeOrToken: MaybeNodeOrToken): AST.Token {
return this.findTokenByOffset(nodeOrToken.range![0])!
}

/**
* Get the last token representing the given node.
*
*/
public getLastToken(nodeOrToken: MaybeNodeOrToken): AST.Token {
return this.findTokenByOffset(nodeOrToken.range![1] - 1)!
}

/**
* Get the first token before the given node or token.
*/
Expand All @@ -54,6 +58,7 @@ export class TokenStore {
}
return null
}

/**
* Get the first token after the given node or token.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/parser/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
56 changes: 27 additions & 29 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions tests/src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`)),
)
})
Expand Down