File size: 6,152 Bytes
7d73cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseForESLint = void 0;
const espree_1 = require("./modules/espree");
const visitor_keys_1 = require("./visitor-keys");
const convert_1 = require("./convert");
const token_store_1 = require("./token-store");
const semver_1 = require("semver");
const extend_parser_1 = require("./extend-parser");
const DEFAULT_ECMA_VERSION = 2019;
function parseForESLint(code, options) {
    const parserOptions = Object.assign({ filePath: "<input>", ecmaVersion: DEFAULT_ECMA_VERSION }, options || {}, {
        loc: true,
        range: true,
        raw: true,
        tokens: true,
        comment: true,
        eslintVisitorKeys: true,
        eslintScopeManager: true,
    });
    parserOptions.ecmaVersion = normalizeEcmaVersion(parserOptions.ecmaVersion);
    const ctx = getJSONSyntaxContext(parserOptions.jsonSyntax);
    const tokens = [];
    const comments = [];
    const tokenStore = new token_store_1.TokenStore(tokens);
    const nodes = [];
    parserOptions.ctx = ctx;
    parserOptions.tokenStore = tokenStore;
    parserOptions.comments = comments;
    parserOptions.nodes = nodes;
    const baseAst = (0, extend_parser_1.getParser)().parseExpressionAt(code, 0, parserOptions);
    for (const node of nodes) {
        ;
        node.type = `JSON${node.type}`;
    }
    const ast = (0, convert_1.convertProgramNode)(baseAst, tokenStore, ctx, code);
    ast.tokens = tokens;
    ast.comments = comments;
    return {
        ast,
        visitorKeys: (0, visitor_keys_1.getVisitorKeys)(),
        services: {
            isJSON: true,
        },
    };
}
exports.parseForESLint = parseForESLint;
function getJSONSyntaxContext(str) {
    const upperCase = str === null || str === void 0 ? void 0 : str.toUpperCase();
    if (upperCase === "JSON") {
        return {
            trailingCommas: false,
            comments: false,
            plusSigns: false,
            spacedSigns: false,
            leadingOrTrailingDecimalPoints: false,
            infinities: false,
            nans: false,
            numericSeparators: false,
            binaryNumericLiterals: false,
            octalNumericLiterals: false,
            legacyOctalNumericLiterals: false,
            invalidJsonNumbers: false,
            multilineStrings: false,
            unquoteProperties: false,
            singleQuotes: false,
            numberProperties: false,
            undefinedKeywords: false,
            sparseArrays: false,
            regExpLiterals: false,
            templateLiterals: false,
            bigintLiterals: false,
            unicodeCodepointEscapes: false,
            escapeSequenceInIdentifier: false,
        };
    }
    if (upperCase === "JSONC") {
        return {
            trailingCommas: true,
            comments: true,
            plusSigns: false,
            spacedSigns: false,
            leadingOrTrailingDecimalPoints: false,
            infinities: false,
            nans: false,
            numericSeparators: false,
            binaryNumericLiterals: false,
            octalNumericLiterals: false,
            legacyOctalNumericLiterals: false,
            invalidJsonNumbers: false,
            multilineStrings: false,
            unquoteProperties: false,
            singleQuotes: false,
            numberProperties: false,
            undefinedKeywords: false,
            sparseArrays: false,
            regExpLiterals: false,
            templateLiterals: false,
            bigintLiterals: false,
            unicodeCodepointEscapes: false,
            escapeSequenceInIdentifier: false,
        };
    }
    if (upperCase === "JSON5") {
        return {
            trailingCommas: true,
            comments: true,
            plusSigns: true,
            spacedSigns: true,
            leadingOrTrailingDecimalPoints: true,
            infinities: true,
            nans: true,
            numericSeparators: false,
            binaryNumericLiterals: false,
            octalNumericLiterals: false,
            legacyOctalNumericLiterals: false,
            invalidJsonNumbers: true,
            multilineStrings: true,
            unquoteProperties: true,
            singleQuotes: true,
            numberProperties: false,
            undefinedKeywords: false,
            sparseArrays: false,
            regExpLiterals: false,
            templateLiterals: false,
            bigintLiterals: false,
            unicodeCodepointEscapes: false,
            escapeSequenceInIdentifier: false,
        };
    }
    return {
        trailingCommas: true,
        comments: true,
        plusSigns: true,
        spacedSigns: true,
        leadingOrTrailingDecimalPoints: true,
        infinities: true,
        nans: true,
        numericSeparators: true,
        binaryNumericLiterals: true,
        octalNumericLiterals: true,
        legacyOctalNumericLiterals: true,
        invalidJsonNumbers: true,
        multilineStrings: true,
        unquoteProperties: true,
        singleQuotes: true,
        numberProperties: true,
        undefinedKeywords: true,
        sparseArrays: true,
        regExpLiterals: true,
        templateLiterals: true,
        bigintLiterals: true,
        unicodeCodepointEscapes: true,
        escapeSequenceInIdentifier: true,
    };
}
function normalizeEcmaVersion(version) {
    const espree = (0, espree_1.getEspree)();
    const latestEcmaVersion = getLatestEcmaVersion(espree);
    if (version == null || version === "latest") {
        return latestEcmaVersion;
    }
    return Math.min(getEcmaVersionYear(version), latestEcmaVersion);
}
function getLatestEcmaVersion(espree) {
    if (espree.latestEcmaVersion == null) {
        for (const { v, latest } of [
            { v: "6.1.0", latest: 2020 },
            { v: "4.0.0", latest: 2019 },
        ]) {
            if ((0, semver_1.lte)(v, espree.version)) {
                return latest;
            }
        }
        return 2018;
    }
    return getEcmaVersionYear(espree.latestEcmaVersion);
}
function getEcmaVersionYear(version) {
    return version > 5 && version < 2015 ? version + 2009 : version;
}