Skip to content

Change to accept parens when syntax is not specified. #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2021
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"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/transpile-only --inspect \"tests/src/**/*.ts\" --reporter dot",
"test:debug": "mocha --require ts-node/register/transpile-only \"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 .",
Expand Down
42 changes: 11 additions & 31 deletions src/parser/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,19 @@ import type { Token as AcornToken, tokTypes as AcornTokTypes } from "acorn"
import { isStaticValueIdentifier } from "./validate"
import { throwUnexpectedNodeError, throwUnexpectedTokenError } from "./errors"
import { getAcorn } from "./modules/acorn"

export type JSONSyntaxContext = {
trailingCommas: boolean
comments: boolean
// invalid JSON numbers
plusSigns: boolean
spacedSigns: boolean
leadingOrTrailingDecimalPoints: boolean
infinities: boolean
nans: boolean
numericSeparators: boolean
binaryNumericLiterals: boolean
octalNumericLiterals: boolean
legacyOctalNumericLiterals: boolean
invalidJsonNumbers: boolean
// statics
multilineStrings: boolean
unquoteProperties: boolean
singleQuotes: boolean
numberProperties: boolean
undefinedKeywords: boolean
sparseArrays: boolean
regExpLiterals: boolean
templateLiterals: boolean
bigintLiterals: boolean
unicodeCodepointEscapes: boolean
escapeSequenceInIdentifier: boolean
// JS-likes
// staticExpression: boolean
}
import type { JSONSyntaxContext } from "./syntax-context"

export class TokenConvertor {
private readonly ctx: JSONSyntaxContext

private readonly code: string

private readonly templateBuffer: AcornToken[] = []

private readonly tokTypes: typeof AcornTokTypes

public constructor(code: string) {
public constructor(ctx: JSONSyntaxContext, code: string) {
this.ctx = ctx
this.code = code
this.tokTypes = getAcorn().tokTypes
}
Expand Down Expand Up @@ -125,6 +99,12 @@ export class TokenConvertor {
pattern: reValue.pattern,
}
value = `/${reValue.pattern}/${reValue.flags}`
} else if (
this.ctx.parentheses &&
(token.type === tokTypes.parenL || token.type === tokTypes.parenR)
) {
type = "Punctuator"
value = this.code.slice(...token.range!)
} else {
// const key = Object.keys(tokTypes).find(
// (k) => tokTypes[k] === token.type,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/extend-parser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { TokenStore } from "./token-store"
import type { JSONSyntaxContext } from "./validate"
import { validateNode } from "./validate"
import type { Parser, Options } from "acorn"
import type { Comment } from "../types"
import type { Node } from "estree"
import { getAcorn } from "./modules/acorn"
import { ParseError, throwUnexpectedCommentError } from "./errors"
import { TokenConvertor } from "./convert"
import type { JSONSyntaxContext } from "./syntax-context"

let parserCache: typeof Parser | undefined

Expand Down Expand Up @@ -39,7 +39,7 @@ export function getParser(): typeof Parser {
) {
super(
((): Options => {
const tokenConvertor = new TokenConvertor(code)
const tokenConvertor = new TokenConvertor(options.ctx, code)
return {
// do not use spread, because we don't want to pass any unknown options to acorn
ecmaVersion: options.ecmaVersion,
Expand Down
6 changes: 5 additions & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { AST, SourceCode } from "eslint"
import type { ESPree } from "./modules/espree"
import { getEspree } from "./modules/espree"
import { getVisitorKeys } from "./visitor-keys"
import type { JSONSyntaxContext } from "./convert"
import { convertProgramNode } from "./convert"
import { TokenStore } from "./token-store"
import type { JSONProgram } from "./ast"
import { lte } from "semver"
import { getParser } from "./extend-parser"
import type { JSONSyntaxContext } from "./syntax-context"

const DEFAULT_ECMA_VERSION = 2019

Expand Down Expand Up @@ -99,6 +99,7 @@ function getJSONSyntaxContext(str?: string | null): JSONSyntaxContext {
bigintLiterals: false,
unicodeCodepointEscapes: false,
escapeSequenceInIdentifier: false,
parentheses: false,
}
}
if (upperCase === "JSONC") {
Expand Down Expand Up @@ -126,6 +127,7 @@ function getJSONSyntaxContext(str?: string | null): JSONSyntaxContext {
bigintLiterals: false,
unicodeCodepointEscapes: false,
escapeSequenceInIdentifier: false,
parentheses: false,
}
}
if (upperCase === "JSON5") {
Expand Down Expand Up @@ -153,6 +155,7 @@ function getJSONSyntaxContext(str?: string | null): JSONSyntaxContext {
bigintLiterals: false,
unicodeCodepointEscapes: false,
escapeSequenceInIdentifier: false,
parentheses: false,
}
}
return {
Expand All @@ -179,6 +182,7 @@ function getJSONSyntaxContext(str?: string | null): JSONSyntaxContext {
bigintLiterals: true,
unicodeCodepointEscapes: true,
escapeSequenceInIdentifier: true,
parentheses: true,
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/parser/syntax-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export type JSONSyntaxContext = {
trailingCommas: boolean
comments: boolean
// invalid JSON numbers
plusSigns: boolean
spacedSigns: boolean
leadingOrTrailingDecimalPoints: boolean
infinities: boolean
nans: boolean
numericSeparators: boolean
binaryNumericLiterals: boolean
octalNumericLiterals: boolean
legacyOctalNumericLiterals: boolean
invalidJsonNumbers: boolean
// statics
multilineStrings: boolean
unquoteProperties: boolean
singleQuotes: boolean
numberProperties: boolean
undefinedKeywords: boolean
sparseArrays: boolean
regExpLiterals: boolean
templateLiterals: boolean
bigintLiterals: boolean
unicodeCodepointEscapes: boolean
escapeSequenceInIdentifier: boolean
// JS-likes
parentheses: boolean
// staticExpression: boolean
}
31 changes: 1 addition & 30 deletions src/parser/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
requireFromCwd,
requireFromLinter,
} from "./modules/require-utils"
import type { JSONSyntaxContext } from "./syntax-context"

const lineBreakPattern = /\r\n|[\n\r\u2028\u2029]/u
const octalNumericLiteralPattern = /^0[Oo]/u
Expand Down Expand Up @@ -73,36 +74,6 @@ function getCodePointEscapeMatcher(): eslintUtils.PatternMatcher {
return cacheCodePointEscapeMatcher
}

export type JSONSyntaxContext = {
trailingCommas: boolean
comments: boolean
// invalid JSON numbers
plusSigns: boolean
spacedSigns: boolean
leadingOrTrailingDecimalPoints: boolean
infinities: boolean
nans: boolean
numericSeparators: boolean
binaryNumericLiterals: boolean
octalNumericLiterals: boolean
legacyOctalNumericLiterals: boolean
invalidJsonNumbers: boolean
// statics
multilineStrings: boolean
unquoteProperties: boolean
singleQuotes: boolean
numberProperties: boolean
undefinedKeywords: boolean
sparseArrays: boolean
regExpLiterals: boolean
templateLiterals: boolean
bigintLiterals: boolean
unicodeCodepointEscapes: boolean
escapeSequenceInIdentifier: boolean
// JS-likes
// staticExpression: boolean
}

/**
* Validate ES node
*/
Expand Down
50 changes: 20 additions & 30 deletions tests/src/parser/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ describe("Check that parsing error is correct.", () => {
code: `
{method(){}}
`,
message: "Unexpected token '('.",
message: "Unexpected token '{'.",
lineNumber: 2,
column: 8,
index: 8,
char: "(",
column: 10,
index: 10,
char: "{",
},
{
code: `
Expand All @@ -145,31 +145,21 @@ describe("Check that parsing error is correct.", () => {
code: `
{get foo(){}}
`,
message: "Unexpected token '('.",
message: "Unexpected token '{'.",
lineNumber: 2,
column: 9,
index: 9,
char: "(",
column: 11,
index: 11,
char: "{",
},
{
code: `
{set foo(p){}}
`,
message: "Unexpected token '('.",
lineNumber: 2,
column: 9,
index: 9,
char: "(",
},
{
code: `
[('a')]
`,
message: "Unexpected token '('.",
message: "Unexpected token '{'.",
lineNumber: 2,
column: 2,
index: 2,
char: "(",
column: 12,
index: 12,
char: "{",
},
{
code: `
Expand All @@ -185,11 +175,11 @@ describe("Check that parsing error is correct.", () => {
code: `
[call()]
`,
message: "Unexpected token '('.",
message: "Unexpected call expression.",
lineNumber: 2,
column: 6,
index: 6,
char: "(",
column: 2,
index: 2,
char: "c",
},
{
code: `
Expand Down Expand Up @@ -255,11 +245,11 @@ typeof 123
code: `
+(+1)
`,
message: "Unexpected token '('.",
message: "Unexpected unary expression.",
lineNumber: 2,
column: 2,
index: 2,
char: "(",
column: 3,
index: 3,
char: "+",
},
{
code: `
Expand Down
16 changes: 16 additions & 0 deletions tests/src/parser/syntaxes/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ describe("Check that parsing error is correct for JSON.", () => {
index: 0,
char: "0",
},
{
code: "(42)",
message: "Unexpected token '('.",
lineNumber: 1,
column: 1,
index: 0,
char: "(",
},
{
code: "[('a')]",
message: "Unexpected token '('.",
lineNumber: 1,
column: 2,
index: 1,
char: "(",
},
]) {
it(`JSON parseForESLint error on ${JSON.stringify(code)}`, () => {
const e = getParseError(code)
Expand Down
16 changes: 16 additions & 0 deletions tests/src/parser/syntaxes/json5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ describe("Check that parsing error is correct for JSON5.", () => {
index: 0,
char: "0",
},
{
code: "(42)",
message: "Unexpected token '('.",
lineNumber: 1,
column: 1,
index: 0,
char: "(",
},
{
code: "[('a')]",
message: "Unexpected token '('.",
lineNumber: 1,
column: 2,
index: 1,
char: "(",
},
]) {
it(`JSON5 parseForESLint error on ${JSON.stringify(code)}`, () => {
const e = getParseError(code)
Expand Down
16 changes: 16 additions & 0 deletions tests/src/parser/syntaxes/jsonc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ describe("Check that parsing error is correct for JSONC.", () => {
index: 0,
char: "0",
},
{
code: "(42)",
message: "Unexpected token '('.",
lineNumber: 1,
column: 1,
index: 0,
char: "(",
},
{
code: "[('a')]",
message: "Unexpected token '('.",
lineNumber: 1,
column: 2,
index: 1,
char: "(",
},
]) {
it(`JSONC parseForESLint error on ${JSON.stringify(code)}`, () => {
const e = getParseError(code)
Expand Down