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
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.nyc_output
/coverage
/node_modules
/tests/fixtures/integrations
/assets
/lib
/tests/fixtures
56 changes: 56 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"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",
],
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",
},
},
],
}
52 changes: 52 additions & 0 deletions .github/workflows/NodeCI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- name: Install Packages
run: npm install
- name: Lint
run: npm run lint
- name: Build
run: npm run build
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.13.x, 12.x, 13.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install Packages
run: npm install
- name: Test
run: npm test
test-and-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 10.x
- name: Install Packages
run: npm install
- name: Test
run: npm run test:nyc
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v1.1.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/NpmPublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: publish
on:
push:
tags:
- "*"
jobs:
release:
name: check version, and release
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Node
uses: actions/setup-node@v1
with:
registry-url: 'https://registry.npmjs.org'
- name: Install Packages
run: npm install
- name: test
run: npm run test
- name: Build
run: npm run build
- name: check can npm-publish
run: npx can-npm-publish
- name: release
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/
# typings/

# TypeScript cache
*.tsbuildinfo
Expand Down Expand Up @@ -102,3 +102,7 @@ dist

# TernJS port file
.tern-port

/lib
/dist-ts
/index.d.ts
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"eslint.validate": [
"javascript",
"javascriptreact",
"vue",
"typescript",
"json",
"jsonc"
],
"typescript.validate.enable": true,
"javascript.validate.enable": false,
"css.validate": false,
"typescript.tsdk": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
}
}
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# jsonc-eslint-parser
JSON, JSONC and JSON5 parser for use with ESLint plugins.

## :name_badge: Introduction

[JSON], [JSONC] and [JSON5] parser for use with [ESLint] plugins.

This parser allows us to lint [JSON], [JSONC] and [JSON5] files.
This parser and the rules of [eslint-plugin-jsonc] would catch some of the mistakes and code style violations.

See [eslint-plugin-jsonc] for details.

## :cd: Installation

```bash
npm i --save-dev jsonc-eslint-parser
```

## :book: Usage

In your ESLint configuration file, set the `overrides` > `parser` property:

```json5
{
// ...
// Add the following settings.
"overrides": [
{
"files": ["*.json", "*.json5"], // Specify the extension or pattern you want to parse as JSON.
"parser": "jsonc-eslint-parser", // Set this parser.
},
],
}
```

## :gear: Configuration

The following additional configuration options are available by specifying them in [parserOptions](https://eslint.org/docs/user-guide/configuring#specifying-parser-options-1) in your ESLint configuration file.

```json5
{
// ...
"overrides": [
{
"files": ["*.json", "*.json5"],
"parser": "jsonc-eslint-parser",
// Additional configuration options
"parserOptions": {
"jsonSyntax": "JSON5"
}
},
],
}
```

### `parserOptions.jsonSyntax`

Set to `"JSON"`, `"JSONC"` or `"JSON5"`. Select the JSON syntax you are using.
If not specified, all syntaxes that express static values ​​are accepted. For example, template literals without interpolation.

**Note** : Recommended to loosen the syntax checking by the parser and use check rules of [eslint-plugin-jsonc] to automatically fix it.

## :lock: License

See the [LICENSE](LICENSE) file for license rights and limitations (MIT).

[JSON]: https://json.org/
[JSONC]: https://github.com/microsoft/node-jsonc-parser
[JSON5]: https://json5.org/
[ESLint]: https://eslint.org/
[eslint-plugin-jsonc]: https://www.npmjs.com/package/eslint-plugin-jsonc
63 changes: 63 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "jsonc-eslint-parser",
"version": "0.5.0",
"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"
}
}
49 changes: 49 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { parseForESLint } from "./parser/parser"
import { traverseNodes } from "./parser/traverse"
import {
getStaticJSONValue,
isExpression,
isNumberIdentifier,
isUndefinedIdentifier,
} from "./utils/ast"

import type * as AST from "./parser/ast"
import { KEYS } from "./parser/visitor-keys"

// parser
export { parseForESLint }
// Keys
export const VisitorKeys = KEYS

// tools
export {
traverseNodes,
getStaticJSONValue,
isExpression,
isNumberIdentifier,
isUndefinedIdentifier,
}

/**
* Parse JSON source code
*/
export function parseJSON(code: string, options?: any): AST.JSONProgram {
const parserOptions = Object.assign(
{ filePath: "<input>", ecmaVersion: 2019 },
options || {},
{
loc: true,
range: true,
raw: true,
tokens: true,
comment: true,
eslintVisitorKeys: true,
eslintScopeManager: true,
},
)

return parseForESLint(code, parserOptions).ast as never
}

// types
export { AST }
Loading