Skip to content

Commit 30f61e0

Browse files
authored
Add index.d.ts (#14)
* Add index.d.ts * fix
1 parent de9dfc2 commit 30f61e0

File tree

8 files changed

+86
-36
lines changed

8 files changed

+86
-36
lines changed

.github/workflows/NodeCI.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@ jobs:
1010
lint:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v1
13+
- uses: actions/checkout@v2
1414
- uses: actions/setup-node@v1
1515
with:
1616
node-version: 14
1717
- name: Install Packages
1818
run: npm install
1919
- name: Lint
2020
run: npm run lint
21+
- name: Build
22+
run: npm run build
2123
test:
2224
runs-on: ubuntu-latest
2325
strategy:
2426
matrix:
2527
node-version: [10.13.x, 12.x, 13.x, 14.x]
2628
steps:
27-
- uses: actions/checkout@v1
29+
- uses: actions/checkout@v2
2830
- name: Use Node.js ${{ matrix.node-version }}
2931
uses: actions/setup-node@v1
3032
with:
@@ -36,7 +38,7 @@ jobs:
3638
test-with-eslint6:
3739
runs-on: ubuntu-latest
3840
steps:
39-
- uses: actions/checkout@v1
41+
- uses: actions/checkout@v2
4042
- uses: actions/setup-node@v1
4143
with:
4244
node-version: 8.10.x
@@ -50,12 +52,14 @@ jobs:
5052
test-and-coverage:
5153
runs-on: ubuntu-latest
5254
steps:
53-
- uses: actions/checkout@v1
55+
- uses: actions/checkout@v2
5456
- uses: actions/setup-node@v1
5557
with:
5658
node-version: 10.x
5759
- name: Install Packages
5860
run: npm install
61+
- name: Setup
62+
run: npm run setup-types
5963
- name: Test
6064
run: npm run test:nyc
6165
- name: Coveralls GitHub Action

.github/workflows/NpmPublish.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: checkout
12-
uses: actions/checkout@v1
12+
uses: actions/checkout@v2
1313
- name: setup Node
1414
uses: actions/setup-node@v1
1515
with:
@@ -18,6 +18,8 @@ jobs:
1818
run: npm install
1919
- name: test
2020
run: npm run test
21+
- name: Build
22+
run: npm run build
2123
- name: check can npm-publish
2224
run: npx can-npm-publish
2325
- name: release

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ dist
104104
.tern-port
105105

106106
/typings/eslint/lib
107+
/dist-ts
108+
/index.d.ts
107109

108110
# docs
109111
/404.html

lib/index.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { parseForESLint } from "./parser/json-eslint-parser"
1010
import { traverseNodes } from "./parser/traverse"
1111
import { getStaticJSONValue } from "./utils/ast"
1212

13+
import type * as AST from "./parser/ast"
14+
1315
const configs = {
1416
base,
1517
"auto-config": autoConfig,
@@ -23,15 +25,48 @@ const rules = ruleList.reduce((obj, r) => {
2325
return obj
2426
}, {} as { [key: string]: RuleModule })
2527

26-
export = {
28+
export default {
2729
configs,
2830
rules,
2931
processors,
30-
3132
// as parser
3233
parseForESLint,
33-
3434
// tools
35+
parseJSON,
3536
traverseNodes,
3637
getStaticJSONValue,
3738
}
39+
export {
40+
configs,
41+
rules,
42+
processors,
43+
// as parser
44+
parseForESLint,
45+
// tools
46+
parseJSON,
47+
traverseNodes,
48+
getStaticJSONValue,
49+
// types
50+
AST,
51+
}
52+
53+
/**
54+
* Parse JSON source code
55+
*/
56+
function parseJSON(code: string, options?: any): AST.JSONProgram {
57+
const parserOptions = Object.assign(
58+
{ filePath: "<input>", ecmaVersion: 2019 },
59+
options || {},
60+
{
61+
loc: true,
62+
range: true,
63+
raw: true,
64+
tokens: true,
65+
comment: true,
66+
eslintVisitorKeys: true,
67+
eslintScopeManager: true,
68+
},
69+
)
70+
71+
return parseForESLint(code, parserOptions).ast as never
72+
}

lib/parser/ast.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ export interface JSONProgram extends BaseJSONNode {
3535
body: [JSONExpressionStatement]
3636
comments: Comment[]
3737
tokens: AST.Token[]
38-
parent?: null
38+
parent: null
3939
}
4040

4141
export interface JSONExpressionStatement extends BaseJSONNode {
4242
type: "JSONExpressionStatement"
4343
expression: JSONExpression
44-
parent?: JSONProgram
44+
parent: JSONProgram
4545
}
4646

4747
export type JSONExpression =
@@ -56,13 +56,13 @@ export type JSONExpression =
5656
export interface JSONArrayExpression extends BaseJSONNode {
5757
type: "JSONArrayExpression"
5858
elements: (JSONExpression | null)[]
59-
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
59+
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
6060
}
6161

6262
export interface JSONObjectExpression extends BaseJSONNode {
6363
type: "JSONObjectExpression"
6464
properties: JSONProperty[]
65-
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
65+
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
6666
}
6767

6868
export interface JSONProperty extends BaseJSONNode {
@@ -73,7 +73,7 @@ export interface JSONProperty extends BaseJSONNode {
7373
method: false
7474
shorthand: false
7575
computed: false
76-
parent?: JSONObjectExpression
76+
parent: JSONObjectExpression
7777
}
7878

7979
export interface JSONIdentifier extends BaseJSONNode {
@@ -144,14 +144,14 @@ export interface JSONUnaryExpression extends BaseJSONNode {
144144
operator: "-" | "+"
145145
prefix: true
146146
argument: JSONNumberLiteral | JSONNumberIdentifier
147-
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
147+
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
148148
}
149149

150150
export interface JSONTemplateLiteral extends BaseJSONNode {
151151
type: "JSONTemplateLiteral"
152152
quasis: [JSONTemplateElement]
153153
expressions: []
154-
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
154+
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
155155
}
156156

157157
export interface JSONTemplateElement extends BaseJSONNode {
@@ -161,5 +161,5 @@ export interface JSONTemplateElement extends BaseJSONNode {
161161
cooked: string
162162
raw: string
163163
}
164-
parent?: JSONTemplateLiteral
164+
parent: JSONTemplateLiteral
165165
}

lib/parser/convert.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function convertProgramNode(node: Program, tokens: AST.Token[]): JSONProgram {
136136
type: "JSONExpressionStatement",
137137
expression: convertNode(expression, tokens),
138138
...getFixLocation(bodyNode),
139+
parent: null as never,
139140
}
140141

141142
const nn: JSONProgram = {
@@ -144,6 +145,7 @@ function convertProgramNode(node: Program, tokens: AST.Token[]): JSONProgram {
144145
comments: [],
145146
tokens: [],
146147
...getFixLocation(node),
148+
parent: null as never,
147149
}
148150
return nn
149151
}
@@ -166,6 +168,7 @@ function convertObjectExpressionNode(
166168
convertPropertyNode(p as Property, tokens),
167169
),
168170
...getFixLocation(node),
171+
parent: null as never,
169172
}
170173
checkUnexpectedKeys(node, tokens, nn)
171174
return nn
@@ -219,6 +222,7 @@ function convertPropertyNode(
219222
method: node.method,
220223
shorthand: node.shorthand,
221224
...getFixLocation(node),
225+
parent: null as never,
222226
}
223227
checkUnexpectedKeys(node, tokens, nn)
224228
return nn
@@ -250,6 +254,7 @@ function convertArrayExpressionNode(
250254
type: "JSONArrayExpression",
251255
elements,
252256
...getFixLocation(node),
257+
parent: null as never,
253258
}
254259
checkUnexpectedKeys(node, tokens, nn)
255260
return nn
@@ -338,6 +343,7 @@ function convertUnaryExpressionNode(
338343
| JSONNumberLiteral
339344
| JSONNumberIdentifier,
340345
...getFixLocation(node),
346+
parent: null as never,
341347
}
342348
checkUnexpectedKeys(node, tokens, nn)
343349
return nn
@@ -401,6 +407,7 @@ function convertTemplateLiteralNode(
401407
quasis,
402408
expressions: [],
403409
...getFixLocation(node),
410+
parent: null as never,
404411
}
405412
checkUnexpectedKeys(node, tokens, nn)
406413
return nn
@@ -423,6 +430,7 @@ function convertTemplateElementNode(
423430
tail: node.tail,
424431
value: node.value,
425432
...getFixLocation(node),
433+
parent: null as never,
426434
}
427435
checkUnexpectedKeys(node, tokens, nn)
428436
return nn

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
{
22
"name": "eslint-plugin-jsonc",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "ESLint plugin for JSON, JSONC and JSON5 files.",
55
"main": "dist/index.js",
6+
"typescript": {
7+
"definition": "index.d.ts"
8+
},
9+
"types": "index.d.ts",
610
"files": [
7-
"dist"
11+
"dist",
12+
"index.d.ts"
813
],
914
"scripts": {
1015
"prebuild": "npm run -s clean && npm run setup-types",
11-
"build": "tsc --project ./tsconfig.build.json",
16+
"build": "npm run build:ts && npm run build:dts",
17+
"build:ts": "tsc --project ./tsconfig.build.json",
18+
"build:dts": "npm run build:dts-step1 && npm run build:dts-step2",
19+
"build:dts-step1": "tsc --declaration --outDir dist-ts --project ./tsconfig.build.json",
20+
"build:dts-step2": "dts-bundle --name eslint-plugin-jsonc --main ./dist-ts/index.d.ts --out ../index.d.ts",
1221
"clean": "rimraf .nyc_output dist coverage",
1322
"lint": "eslint \"tests\" \"lib\" \"docs/.vuepress\" --ext .js,.vue,.ts",
1423
"eslint-fix": "eslint \"tests\" \"lib\" \"docs/.vuepress\" --ext .js,.vue,.ts --fix",
15-
"pretest": "npm run build",
24+
"pretest": "npm run setup-types",
1625
"test:base": "mocha --require ts-node/register \"tests/lib/**/*.ts\" --reporter dot --timeout 60000",
1726
"test": "npm run test:base",
18-
"pretest:nyc": "npm run build",
1927
"test:nyc": "nyc --reporter=lcov npm run test:base",
2028
"test:debug": "mocha --require ts-node/register --inspect-brk \"tests/lib/**/*.ts\" --reporter dot",
2129
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
2230
"new": "ts-node ./tools/new-rule.ts",
23-
"predocs:watch": "npm run build",
31+
"predocs:watch": "npm run build:ts",
2432
"docs:watch": "vuepress dev --debug docs",
25-
"docs:build": "npm run build && vuepress build docs --no-cache",
33+
"docs:build": "npm run build:ts && vuepress build docs --no-cache",
2634
"docs-deploysetup": "npm run docs:build && npm run docs-deploysetup:clean && npm run docs-deploysetup:copy",
2735
"docs-deploysetup:clean": "rimraf assets",
2836
"docs-deploysetup:copy": "npx cpx \"docs/\\.vuepress/dist/**\" . -u",
@@ -60,6 +68,7 @@
6068
"@types/node": "^14.0.13",
6169
"@types/semver": "^7.3.1",
6270
"babel-eslint": "^10.1.0",
71+
"dts-bundle": "^0.7.3",
6372
"eslint": "^7.3.0",
6473
"eslint4b": "^7.3.1",
6574
"espree": "^7.1.0",

tests/lib/parser/json-eslint-parser.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "assert"
22
import path from "path"
33
import fs from "fs"
44

5-
import { parseForESLint } from "../../../lib/parser/json-eslint-parser"
5+
import { parseJSON } from "../../../lib/index"
66

77
const FIXTURE_ROOT = path.resolve(__dirname, "../../fixtures/parser/ast")
88

@@ -24,17 +24,7 @@ function replacer(key: string, value: any) {
2424
}
2525

2626
function parse(code: string) {
27-
return parseForESLint(code, {
28-
comment: true,
29-
ecmaVersion: 2020,
30-
eslintScopeManager: true,
31-
eslintVisitorKeys: true,
32-
filePath: "test.json",
33-
loc: true,
34-
range: true,
35-
raw: true,
36-
tokens: true,
37-
})
27+
return parseJSON(code, { ecmaVersion: 2020 })
3828
}
3929

4030
describe("Check for AST.", () => {
@@ -54,7 +44,7 @@ describe("Check for AST.", () => {
5444
)
5545

5646
const input = fs.readFileSync(inputFileName, "utf8")
57-
const ast = JSON.stringify(parse(input).ast, replacer, 2)
47+
const ast = JSON.stringify(parse(input), replacer, 2)
5848
const output = fs.readFileSync(outputFileName, "utf8")
5949
assert.strictEqual(ast, output)
6050
})

0 commit comments

Comments
 (0)