Skip to content

Commit d6e3bda

Browse files
authored
Implements (#1)
* Implements * fix * format
1 parent 3ac54a0 commit d6e3bda

File tree

85 files changed

+10657
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+10657
-2
lines changed

.eslintignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.nyc_output
2+
/coverage
3+
/node_modules
4+
/tests/fixtures/integrations
5+
/assets
6+
/lib
7+
/tests/fixtures

.eslintrc.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict"
2+
3+
// const version = require("./package.json").version
4+
5+
module.exports = {
6+
parserOptions: {
7+
sourceType: "script",
8+
ecmaVersion: 2020,
9+
},
10+
extends: [
11+
"plugin:@mysticatea/es2015",
12+
"plugin:@mysticatea/+node",
13+
"plugin:@mysticatea/+eslint-plugin",
14+
"plugin:jsonc/auto-config",
15+
"plugin:jsonc/recommended-with-jsonc",
16+
],
17+
rules: {
18+
"require-jsdoc": "error",
19+
"no-warning-comments": "warn",
20+
"no-lonely-if": "off",
21+
"@mysticatea/ts/ban-ts-ignore": "off",
22+
},
23+
overrides: [
24+
{
25+
files: ["*.ts"],
26+
rules: {
27+
// "@mysticatea/ts/no-require-imports": "off",
28+
// "@mysticatea/ts/no-var-requires": "off",
29+
"@mysticatea/node/no-missing-import": "off",
30+
"no-implicit-globals": "off",
31+
"@mysticatea/node/no-extraneous-import": "off",
32+
},
33+
parserOptions: {
34+
sourceType: "module",
35+
project: "./tsconfig.json",
36+
},
37+
},
38+
{
39+
files: ["src/rules/**"],
40+
rules: {
41+
"@mysticatea/eslint-plugin/report-message-format": [
42+
"error",
43+
"[^a-z].*\\.$",
44+
],
45+
"@mysticatea/eslint-plugin/require-meta-docs-url": "off",
46+
},
47+
},
48+
{
49+
files: ["scripts/**/*.ts", "tests/**/*.ts"],
50+
rules: {
51+
"require-jsdoc": "off",
52+
"no-console": "off",
53+
},
54+
},
55+
],
56+
}

.github/workflows/NodeCI.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: actions/setup-node@v1
15+
with:
16+
node-version: 14
17+
- name: Install Packages
18+
run: npm install
19+
- name: Lint
20+
run: npm run lint
21+
- name: Build
22+
run: npm run build
23+
test:
24+
runs-on: ubuntu-latest
25+
strategy:
26+
matrix:
27+
node-version: [10.13.x, 12.x, 13.x, 14.x]
28+
steps:
29+
- uses: actions/checkout@v2
30+
- name: Use Node.js ${{ matrix.node-version }}
31+
uses: actions/setup-node@v1
32+
with:
33+
node-version: ${{ matrix.node-version }}
34+
- name: Install Packages
35+
run: npm install
36+
- name: Test
37+
run: npm test
38+
test-and-coverage:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- uses: actions/checkout@v2
42+
- uses: actions/setup-node@v1
43+
with:
44+
node-version: 10.x
45+
- name: Install Packages
46+
run: npm install
47+
- name: Test
48+
run: npm run test:nyc
49+
- name: Coveralls GitHub Action
50+
uses: coverallsapp/[email protected]
51+
with:
52+
github-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/NpmPublish.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: publish
2+
on:
3+
push:
4+
tags:
5+
- "*"
6+
jobs:
7+
release:
8+
name: check version, and release
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: checkout
12+
uses: actions/checkout@v2
13+
- name: setup Node
14+
uses: actions/setup-node@v1
15+
with:
16+
registry-url: 'https://registry.npmjs.org'
17+
- name: Install Packages
18+
run: npm install
19+
- name: test
20+
run: npm run test
21+
- name: Build
22+
run: npm run build
23+
- name: check can npm-publish
24+
run: npx can-npm-publish
25+
- name: release
26+
run: npm publish
27+
env:
28+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ node_modules/
4242
jspm_packages/
4343

4444
# TypeScript v1 declaration files
45-
typings/
45+
# typings/
4646

4747
# TypeScript cache
4848
*.tsbuildinfo
@@ -102,3 +102,7 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
/lib
107+
/dist-ts
108+
/index.d.ts

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.vscode/settings.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"eslint.validate": [
3+
"javascript",
4+
"javascriptreact",
5+
"vue",
6+
"typescript",
7+
"json",
8+
"jsonc"
9+
],
10+
"typescript.validate.enable": true,
11+
"javascript.validate.enable": false,
12+
"css.validate": false,
13+
"typescript.tsdk": "node_modules/typescript/lib",
14+
"editor.codeActionsOnSave": {
15+
"source.fixAll.eslint": true,
16+
}
17+
}

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,70 @@
11
# jsonc-eslint-parser
2-
JSON, JSONC and JSON5 parser for use with ESLint plugins.
2+
3+
## :name_badge: Introduction
4+
5+
[JSON], [JSONC] and [JSON5] parser for use with [ESLint] plugins.
6+
7+
This parser allows us to lint [JSON], [JSONC] and [JSON5] files.
8+
This parser and the rules of [eslint-plugin-jsonc] would catch some of the mistakes and code style violations.
9+
10+
See [eslint-plugin-jsonc] for details.
11+
12+
## :cd: Installation
13+
14+
```bash
15+
npm i --save-dev jsonc-eslint-parser
16+
```
17+
18+
## :book: Usage
19+
20+
In your ESLint configuration file, set the `overrides` > `parser` property:
21+
22+
```json5
23+
{
24+
// ...
25+
// Add the following settings.
26+
"overrides": [
27+
{
28+
"files": ["*.json", "*.json5"], // Specify the extension or pattern you want to parse as JSON.
29+
"parser": "jsonc-eslint-parser", // Set this parser.
30+
},
31+
],
32+
}
33+
```
34+
35+
## :gear: Configuration
36+
37+
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.
38+
39+
```json5
40+
{
41+
// ...
42+
"overrides": [
43+
{
44+
"files": ["*.json", "*.json5"],
45+
"parser": "jsonc-eslint-parser",
46+
// Additional configuration options
47+
"parserOptions": {
48+
"jsonSyntax": "JSON5"
49+
}
50+
},
51+
],
52+
}
53+
```
54+
55+
### `parserOptions.jsonSyntax`
56+
57+
Set to `"JSON"`, `"JSONC"` or `"JSON5"`. Select the JSON syntax you are using.
58+
If not specified, all syntaxes that express static values ​​are accepted. For example, template literals without interpolation.
59+
60+
**Note** : Recommended to loosen the syntax checking by the parser and use check rules of [eslint-plugin-jsonc] to automatically fix it.
61+
62+
## :lock: License
63+
64+
See the [LICENSE](LICENSE) file for license rights and limitations (MIT).
65+
66+
[JSON]: https://json.org/
67+
[JSONC]: https://github.com/microsoft/node-jsonc-parser
68+
[JSON5]: https://json5.org/
69+
[ESLint]: https://eslint.org/
70+
[eslint-plugin-jsonc]: https://www.npmjs.com/package/eslint-plugin-jsonc

package.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "jsonc-eslint-parser",
3+
"version": "0.5.0",
4+
"description": "JSON, JSONC and JSON5 parser for use with ESLint plugins",
5+
"main": "lib/index.js",
6+
"files": [
7+
"lib"
8+
],
9+
"scripts": {
10+
"build": "npm run build:ts",
11+
"build:ts": "tsc --project ./tsconfig.build.json",
12+
"clean": "rimraf .nyc_output lib coverage",
13+
"lint": "eslint \"tests\" \"src\" --ext .js,.ts",
14+
"eslint-fix": "eslint \"tests\" \"src\" --ext .js,.ts --fix",
15+
"test:base": "mocha --require ts-node/register \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
16+
"test": "npm run test:base",
17+
"test:nyc": "nyc --reporter=lcov npm run test:base",
18+
"test:debug": "mocha --require ts-node/register --inspect \"tests/src/**/*.ts\" --reporter dot",
19+
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
20+
"preversion": "npm test && npm run update && git add .",
21+
"version": "npm run eslint-fix && git add .",
22+
"update-fixtures": "ts-node ./tools/update-fixtures.ts"
23+
},
24+
"repository": {
25+
"type": "git",
26+
"url": "git+https://github.com/ota-meshi/jsonc-eslint-parser.git"
27+
},
28+
"keywords": [
29+
"eslint",
30+
"json",
31+
"jsonc",
32+
"json5",
33+
"parser"
34+
],
35+
"author": "Yosuke Ota",
36+
"license": "MIT",
37+
"bugs": {
38+
"url": "https://github.com/ota-meshi/jsonc-eslint-parser/issues"
39+
},
40+
"homepage": "https://github.com/ota-meshi/jsonc-eslint-parser#readme",
41+
"devDependencies": {
42+
"@mysticatea/eslint-plugin": "^13.0.0",
43+
"@types/eslint": "^7.2.0",
44+
"@types/estree": "0.0.44",
45+
"@types/mocha": "^7.0.2",
46+
"@types/natural-compare": "^1.4.0",
47+
"@types/node": "^14.0.13",
48+
"@types/semver": "^7.3.1",
49+
"babel-eslint": "^10.1.0",
50+
"eslint": "^7.3.0",
51+
"eslint-plugin-jsonc": "^0.4.0",
52+
"mocha": "^7.0.0",
53+
"nyc": "^15.1.0",
54+
"prettier": "^2.0.5",
55+
"semver": "^7.3.2",
56+
"ts-node": "^8.10.2",
57+
"typescript": "^3.9.5"
58+
},
59+
"dependencies": {
60+
"eslint-visitor-keys": "^1.3.0",
61+
"espree": "^6.0.0 || ^7.2.0"
62+
}
63+
}

src/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { parseForESLint } from "./parser/parser"
2+
import { traverseNodes } from "./parser/traverse"
3+
import {
4+
getStaticJSONValue,
5+
isExpression,
6+
isNumberIdentifier,
7+
isUndefinedIdentifier,
8+
} from "./utils/ast"
9+
10+
import type * as AST from "./parser/ast"
11+
import { KEYS } from "./parser/visitor-keys"
12+
13+
// parser
14+
export { parseForESLint }
15+
// Keys
16+
export const VisitorKeys = KEYS
17+
18+
// tools
19+
export {
20+
traverseNodes,
21+
getStaticJSONValue,
22+
isExpression,
23+
isNumberIdentifier,
24+
isUndefinedIdentifier,
25+
}
26+
27+
/**
28+
* Parse JSON source code
29+
*/
30+
export function parseJSON(code: string, options?: any): AST.JSONProgram {
31+
const parserOptions = Object.assign(
32+
{ filePath: "<input>", ecmaVersion: 2019 },
33+
options || {},
34+
{
35+
loc: true,
36+
range: true,
37+
raw: true,
38+
tokens: true,
39+
comment: true,
40+
eslintVisitorKeys: true,
41+
eslintScopeManager: true,
42+
},
43+
)
44+
45+
return parseForESLint(code, parserOptions).ast as never
46+
}
47+
48+
// types
49+
export { AST }

0 commit comments

Comments
 (0)