Skip to content

Commit

Permalink
Add index.d.ts (#14)
Browse files Browse the repository at this point in the history
* Add index.d.ts

* fix
  • Loading branch information
ota-meshi authored Jul 24, 2020
1 parent de9dfc2 commit 30f61e0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 36 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/NodeCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- 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@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
Expand All @@ -36,7 +38,7 @@ jobs:
test-with-eslint6:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 8.10.x
Expand All @@ -50,12 +52,14 @@ jobs:
test-and-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 10.x
- name: Install Packages
run: npm install
- name: Setup
run: npm run setup-types
- name: Test
run: npm run test:nyc
- name: Coveralls GitHub Action
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/NpmPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v1
uses: actions/checkout@v2
- name: setup Node
uses: actions/setup-node@v1
with:
Expand All @@ -18,6 +18,8 @@ jobs:
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
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ dist
.tern-port

/typings/eslint/lib
/dist-ts
/index.d.ts

# docs
/404.html
Expand Down
41 changes: 38 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { parseForESLint } from "./parser/json-eslint-parser"
import { traverseNodes } from "./parser/traverse"
import { getStaticJSONValue } from "./utils/ast"

import type * as AST from "./parser/ast"

const configs = {
base,
"auto-config": autoConfig,
Expand All @@ -23,15 +25,48 @@ const rules = ruleList.reduce((obj, r) => {
return obj
}, {} as { [key: string]: RuleModule })

export = {
export default {
configs,
rules,
processors,

// as parser
parseForESLint,

// tools
parseJSON,
traverseNodes,
getStaticJSONValue,
}
export {
configs,
rules,
processors,
// as parser
parseForESLint,
// tools
parseJSON,
traverseNodes,
getStaticJSONValue,
// types
AST,
}

/**
* Parse JSON source code
*/
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
}
16 changes: 8 additions & 8 deletions lib/parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export interface JSONProgram extends BaseJSONNode {
body: [JSONExpressionStatement]
comments: Comment[]
tokens: AST.Token[]
parent?: null
parent: null
}

export interface JSONExpressionStatement extends BaseJSONNode {
type: "JSONExpressionStatement"
expression: JSONExpression
parent?: JSONProgram
parent: JSONProgram
}

export type JSONExpression =
Expand All @@ -56,13 +56,13 @@ export type JSONExpression =
export interface JSONArrayExpression extends BaseJSONNode {
type: "JSONArrayExpression"
elements: (JSONExpression | null)[]
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

export interface JSONObjectExpression extends BaseJSONNode {
type: "JSONObjectExpression"
properties: JSONProperty[]
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

export interface JSONProperty extends BaseJSONNode {
Expand All @@ -73,7 +73,7 @@ export interface JSONProperty extends BaseJSONNode {
method: false
shorthand: false
computed: false
parent?: JSONObjectExpression
parent: JSONObjectExpression
}

export interface JSONIdentifier extends BaseJSONNode {
Expand Down Expand Up @@ -144,14 +144,14 @@ export interface JSONUnaryExpression extends BaseJSONNode {
operator: "-" | "+"
prefix: true
argument: JSONNumberLiteral | JSONNumberIdentifier
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

export interface JSONTemplateLiteral extends BaseJSONNode {
type: "JSONTemplateLiteral"
quasis: [JSONTemplateElement]
expressions: []
parent?: JSONArrayExpression | JSONProperty | JSONExpressionStatement
parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

export interface JSONTemplateElement extends BaseJSONNode {
Expand All @@ -161,5 +161,5 @@ export interface JSONTemplateElement extends BaseJSONNode {
cooked: string
raw: string
}
parent?: JSONTemplateLiteral
parent: JSONTemplateLiteral
}
8 changes: 8 additions & 0 deletions lib/parser/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function convertProgramNode(node: Program, tokens: AST.Token[]): JSONProgram {
type: "JSONExpressionStatement",
expression: convertNode(expression, tokens),
...getFixLocation(bodyNode),
parent: null as never,
}

const nn: JSONProgram = {
Expand All @@ -144,6 +145,7 @@ function convertProgramNode(node: Program, tokens: AST.Token[]): JSONProgram {
comments: [],
tokens: [],
...getFixLocation(node),
parent: null as never,
}
return nn
}
Expand All @@ -166,6 +168,7 @@ function convertObjectExpressionNode(
convertPropertyNode(p as Property, tokens),
),
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand Down Expand Up @@ -219,6 +222,7 @@ function convertPropertyNode(
method: node.method,
shorthand: node.shorthand,
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand Down Expand Up @@ -250,6 +254,7 @@ function convertArrayExpressionNode(
type: "JSONArrayExpression",
elements,
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand Down Expand Up @@ -338,6 +343,7 @@ function convertUnaryExpressionNode(
| JSONNumberLiteral
| JSONNumberIdentifier,
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand Down Expand Up @@ -401,6 +407,7 @@ function convertTemplateLiteralNode(
quasis,
expressions: [],
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand All @@ -423,6 +430,7 @@ function convertTemplateElementNode(
tail: node.tail,
value: node.value,
...getFixLocation(node),
parent: null as never,
}
checkUnexpectedKeys(node, tokens, nn)
return nn
Expand Down
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
{
"name": "eslint-plugin-jsonc",
"version": "0.2.0",
"version": "0.3.0",
"description": "ESLint plugin for JSON, JSONC and JSON5 files.",
"main": "dist/index.js",
"typescript": {
"definition": "index.d.ts"
},
"types": "index.d.ts",
"files": [
"dist"
"dist",
"index.d.ts"
],
"scripts": {
"prebuild": "npm run -s clean && npm run setup-types",
"build": "tsc --project ./tsconfig.build.json",
"build": "npm run build:ts && npm run build:dts",
"build:ts": "tsc --project ./tsconfig.build.json",
"build:dts": "npm run build:dts-step1 && npm run build:dts-step2",
"build:dts-step1": "tsc --declaration --outDir dist-ts --project ./tsconfig.build.json",
"build:dts-step2": "dts-bundle --name eslint-plugin-jsonc --main ./dist-ts/index.d.ts --out ../index.d.ts",
"clean": "rimraf .nyc_output dist coverage",
"lint": "eslint \"tests\" \"lib\" \"docs/.vuepress\" --ext .js,.vue,.ts",
"eslint-fix": "eslint \"tests\" \"lib\" \"docs/.vuepress\" --ext .js,.vue,.ts --fix",
"pretest": "npm run build",
"pretest": "npm run setup-types",
"test:base": "mocha --require ts-node/register \"tests/lib/**/*.ts\" --reporter dot --timeout 60000",
"test": "npm run test:base",
"pretest:nyc": "npm run build",
"test:nyc": "nyc --reporter=lcov npm run test:base",
"test:debug": "mocha --require ts-node/register --inspect-brk \"tests/lib/**/*.ts\" --reporter dot",
"update": "ts-node ./tools/update.ts && npm run eslint-fix && npm run test:nyc",
"new": "ts-node ./tools/new-rule.ts",
"predocs:watch": "npm run build",
"predocs:watch": "npm run build:ts",
"docs:watch": "vuepress dev --debug docs",
"docs:build": "npm run build && vuepress build docs --no-cache",
"docs:build": "npm run build:ts && vuepress build docs --no-cache",
"docs-deploysetup": "npm run docs:build && npm run docs-deploysetup:clean && npm run docs-deploysetup:copy",
"docs-deploysetup:clean": "rimraf assets",
"docs-deploysetup:copy": "npx cpx \"docs/\\.vuepress/dist/**\" . -u",
Expand Down Expand Up @@ -60,6 +68,7 @@
"@types/node": "^14.0.13",
"@types/semver": "^7.3.1",
"babel-eslint": "^10.1.0",
"dts-bundle": "^0.7.3",
"eslint": "^7.3.0",
"eslint4b": "^7.3.1",
"espree": "^7.1.0",
Expand Down
16 changes: 3 additions & 13 deletions tests/lib/parser/json-eslint-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "assert"
import path from "path"
import fs from "fs"

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

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

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

function parse(code: string) {
return parseForESLint(code, {
comment: true,
ecmaVersion: 2020,
eslintScopeManager: true,
eslintVisitorKeys: true,
filePath: "test.json",
loc: true,
range: true,
raw: true,
tokens: true,
})
return parseJSON(code, { ecmaVersion: 2020 })
}

describe("Check for AST.", () => {
Expand All @@ -54,7 +44,7 @@ describe("Check for AST.", () => {
)

const input = fs.readFileSync(inputFileName, "utf8")
const ast = JSON.stringify(parse(input).ast, replacer, 2)
const ast = JSON.stringify(parse(input), replacer, 2)
const output = fs.readFileSync(outputFileName, "utf8")
assert.strictEqual(ast, output)
})
Expand Down

0 comments on commit 30f61e0

Please sign in to comment.