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
45 changes: 6 additions & 39 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,22 @@
"use strict"

// const version = require("./package.json").version

module.exports = {
parserOptions: {
sourceType: "script",
ecmaVersion: 2020,
},
extends: [
"plugin:@mysticatea/es2015",
"plugin:@mysticatea/+node",
"plugin:@mysticatea/+eslint-plugin",
"plugin:jsonc/auto-config",
"plugin:jsonc/recommended-with-jsonc",
"plugin:@ota-meshi/recommended",
"plugin:@ota-meshi/+typescript",
"plugin:@ota-meshi/+json",
"plugin:@ota-meshi/+node",
"plugin:@ota-meshi/+prettier",
],
rules: {
"require-jsdoc": "error",
"no-warning-comments": "warn",
"no-lonely-if": "off",
"@mysticatea/ts/ban-ts-ignore": "off",
},
overrides: [
{
files: ["*.ts"],
rules: {
// "@mysticatea/ts/no-require-imports": "off",
// "@mysticatea/ts/no-var-requires": "off",
"@mysticatea/node/no-missing-import": "off",
"no-implicit-globals": "off",
"@mysticatea/node/no-extraneous-import": "off",
},
parserOptions: {
sourceType: "module",
project: "./tsconfig.json",
},
},
{
files: ["src/rules/**"],
rules: {
"@mysticatea/eslint-plugin/report-message-format": [
"error",
"[^a-z].*\\.$",
],
"@mysticatea/eslint-plugin/require-meta-docs-url": "off",
},
},
{
files: ["scripts/**/*.ts", "tests/**/*.ts"],
rules: {
"require-jsdoc": "off",
"no-console": "off",
"@typescript-eslint/no-non-null-assertion": "off",
},
},
],
Expand Down
128 changes: 67 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,63 +1,69 @@
{
"name": "jsonc-eslint-parser",
"version": "0.5.2",
"description": "JSON, JSONC and JSON5 parser for use with ESLint plugins",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "npm run build:ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"clean": "rimraf .nyc_output lib coverage",
"lint": "eslint \"tests\" \"src\" --ext .js,.ts",
"eslint-fix": "eslint \"tests\" \"src\" --ext .js,.ts --fix",
"test:base": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"test": "npm run test:base",
"test:nyc": "nyc --reporter=lcov npm run test:base",
"test:debug": "mocha --require ts-node/register --inspect \"tests/src/**/*.ts\" --reporter dot",
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
"preversion": "npm test && npm run update && git add .",
"version": "npm run eslint-fix && git add .",
"update-fixtures": "ts-node ./tools/update-fixtures.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ota-meshi/jsonc-eslint-parser.git"
},
"keywords": [
"eslint",
"json",
"jsonc",
"json5",
"parser"
],
"author": "Yosuke Ota",
"license": "MIT",
"bugs": {
"url": "https://github.com/ota-meshi/jsonc-eslint-parser/issues"
},
"homepage": "https://github.com/ota-meshi/jsonc-eslint-parser#readme",
"devDependencies": {
"@mysticatea/eslint-plugin": "^13.0.0",
"@types/eslint": "^7.2.0",
"@types/estree": "0.0.44",
"@types/mocha": "^7.0.2",
"@types/natural-compare": "^1.4.0",
"@types/node": "^14.0.13",
"@types/semver": "^7.3.1",
"babel-eslint": "^10.1.0",
"eslint": "^7.3.0",
"eslint-plugin-jsonc": "^0.4.0",
"mocha": "^7.0.0",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"semver": "^7.3.2",
"ts-node": "^8.10.2",
"typescript": "^3.9.5"
},
"dependencies": {
"eslint-visitor-keys": "^1.3.0",
"espree": "^6.0.0 || ^7.2.0"
}
"name": "jsonc-eslint-parser",
"version": "0.5.2",
"description": "JSON, JSONC and JSON5 parser for use with ESLint plugins",
"main": "lib/index.js",
"files": [
"lib"
],
"scripts": {
"build": "npm run build:ts",
"build:ts": "tsc --project ./tsconfig.build.json",
"clean": "rimraf .nyc_output lib coverage",
"lint": "eslint \"tests\" \"src\" --ext .js,.ts",
"eslint-fix": "eslint \"tests\" \"src\" --ext .js,.ts --fix",
"test:base": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"test": "npm run test:base",
"test:nyc": "nyc --reporter=lcov npm run test:base",
"test:debug": "mocha --require ts-node/register --inspect \"tests/src/**/*.ts\" --reporter dot",
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
"preversion": "npm test && npm run update && git add .",
"version": "npm run eslint-fix && git add .",
"update-fixtures": "ts-node ./tools/update-fixtures.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ota-meshi/jsonc-eslint-parser.git"
},
"keywords": [
"eslint",
"json",
"jsonc",
"json5",
"parser"
],
"author": "Yosuke Ota",
"license": "MIT",
"bugs": {
"url": "https://github.com/ota-meshi/jsonc-eslint-parser/issues"
},
"homepage": "https://github.com/ota-meshi/jsonc-eslint-parser#readme",
"devDependencies": {
"@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/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",
"eslint": "^7.3.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-jsonc": "^0.5.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"mocha": "^7.0.0",
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"semver": "^7.3.2",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {
"eslint-visitor-keys": "^1.3.0",
"espree": "^6.0.0 || ^7.2.0"
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export {
/**
* Parse JSON source code
*/
export function parseJSON(code: string, options?: any): AST.JSONProgram {
export function parseJSON(
code: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
options?: any,
): AST.JSONProgram {
const parserOptions = Object.assign(
{ filePath: "<input>", ecmaVersion: 2019 },
options || {},
Expand Down
17 changes: 11 additions & 6 deletions src/parser/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
Expression,
} from "estree"
import type { AST } from "eslint"
import {
import type {
JSONNode,
JSONProgram,
JSONExpressionStatement,
Expand All @@ -35,16 +35,17 @@ import {
JSONStringLiteral,
} from "./ast"
import { getKeys, getNodes } from "./traverse"
import type { ParseError } from "./errors"
import {
ParseError,
throwUnexpectedNodeError,
throwExpectedTokenError,
throwUnexpectedTokenError,
throwInvalidNumberError,
throwUnexpectedSpaceError,
throwUnexpectedError,
} from "./errors"
import { TokenStore, isComma, MaybeNodeOrToken } from "./token-store"
import type { TokenStore, MaybeNodeOrToken } from "./token-store"
import { isComma } from "./token-store"

const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u

Expand Down Expand Up @@ -380,14 +381,18 @@ function convertLiteralNode(
regex: node.regex,
...getFixLocation(node),
} as JSONRegExpLiteral
} else if ((node as any).bigint) {
} else if (
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- bigint
(node as any).bigint
) {
if (!ctx.bigintLiterals) {
return throwUnexpectedNodeError(node, tokens)
}
nn = {
type: "JSONLiteral",
value: node.value,
raw: node.raw!,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- bigint
bigint: (node as any).bigint,
...getFixLocation(node),
} as JSONBigIntLiteral
Expand Down Expand Up @@ -653,7 +658,7 @@ function checkUnexpectedKeys(
* Fix the location information of the given node.
* @param node The node.
*/
export function fixLocation(node: MaybeNodeOrToken | AST.Token) {
export function fixLocation(node: MaybeNodeOrToken | AST.Token): void {
const locs = getFixLocation(node)
node.range = locs.range
node.loc = locs.loc
Expand All @@ -663,7 +668,7 @@ export function fixLocation(node: MaybeNodeOrToken | AST.Token) {
* Modify the location information of the given error with using the base offset and gaps of this calculator.
* @param error The error to modify their location.
*/
export function fixErrorLocation(error: ParseError) {
export function fixErrorLocation(error: ParseError): void {
error.index = Math.max(error.index - 2, 0)
if (error.lineNumber === 0) {
error.column = Math.max(error.column - 2, 0)
Expand Down
13 changes: 11 additions & 2 deletions src/parser/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export class ParseError extends SyntaxError {
* Normalize the error object.
* @param x The error object to normalize.
*/
public static normalize(x: any): ParseError | null {
public static normalize(
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
x: any,
): ParseError | null {
if (ParseError.isParseError(x)) {
return x
}
Expand Down Expand Up @@ -50,7 +53,10 @@ export class ParseError extends SyntaxError {
* @param x The value to check.
* @returns `true` if the value has `message`, `pos`, `loc` properties.
*/
public static isParseError(x: any): x is ParseError {
public static isParseError(
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
x: any,
): x is ParseError {
return (
x instanceof ParseError ||
(typeof x.message === "string" &&
Expand Down Expand Up @@ -191,6 +197,7 @@ export function throwUnexpectedNodeError(
throw err
}
if (node.type === "Literal") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- bigint
const type = (node as any).bigint
? "bigint"
: (node as RegExpLiteral).regex
Expand Down Expand Up @@ -249,6 +256,7 @@ export function throwUnexpectedNodeError(
* Throw syntax error of outside of code.
*/
export function throwErrorAsAdjustingOutsideOfCode(
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
err: any,
code: string,
): never {
Expand All @@ -268,6 +276,7 @@ export function throwErrorAsAdjustingOutsideOfCode(
* @returns `true` if the value has acorn style location information.
*/
function isAcornStyleParseError(
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
x: any,
): x is { message: string; pos: number; loc: Position } {
return (
Expand Down
14 changes: 10 additions & 4 deletions src/parser/espree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import path from "path"
* The interface of ESLint custom parsers.
*/
export interface ESPree {
parse(code: string, options: any): AST.Program
parse(
code: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
options: any,
): AST.Program
}

let espreeCache: ESPree | null = null
Expand Down Expand Up @@ -36,17 +40,19 @@ export function getEspree(): ESPree {
}
}
}
// eslint-disable-next-line @mysticatea/ts/no-require-imports
return espreeCache || (espreeCache = require("espree"))
}

/**
* createRequire
*/
function createRequire(filename: string) {
// eslint-disable-next-line @mysticatea/ts/no-require-imports, @mysticatea/ts/no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires -- special require
const Module = require("module")
const fn: (filename: string) => any =
const fn: (
filename: string,
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any -- any
any =
// Added in v12.2.0
Module.createRequire ||
// Added in v10.12.0, but deprecated in v12.2.0.
Expand Down
Loading