• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unexpectedjs / unexpected-snapshot / 4281364852

pending completion
4281364852

push

github

GitHub
Update eslint to version 8.35.0

124 of 169 branches covered (73.37%)

Branch coverage included in aggregate %.

235 of 297 relevant lines covered (79.12%)

36.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.36
/lib/getSourceCode.js
1
const eslint = require('eslint');
36✔
2
const fs = require('fs');
36✔
3

4
function getSourceCode(fileName) {
5
  const { SourceCode } = eslint;
36✔
6
  /**
7
   * Strips Unicode BOM from a given text.
8
   *
9
   * @param {string} text - A text to strip.
10
   * @returns {string} The stripped text.
11
   */
12
  function stripUnicodeBOM(text) {
13
    /*
14
     * Check Unicode BOM.
15
     * In JavaScript, string data is stored as UTF-16, so BOM is 0xFEFF.
16
     * http://www.ecma-international.org/ecma-262/6.0/#sec-unicode-format-control-characters
17
     */
18
    if (text.charCodeAt(0) === 0xfeff) {
36!
19
      return text.slice(1);
×
20
    }
21
    return text;
36✔
22
  }
23

24
  function parse(text, config, filePath) {
25
    let parser;
26

27
    let parserOptions = {
36✔
28
      loc: true,
29
      range: true,
30
      raw: true,
31
      tokens: true,
32
      comment: true,
33
      attachComment: true,
34
      filePath,
35
      ecmaVersion: 9,
36
      ecmaFeatures: {
37
        jsx: true,
38
        globalReturn: true,
39
        experimentalObjectRestSpread: true,
40
      },
41
    };
42

43
    try {
36✔
44
      parser = require(config.parser);
36✔
45
    } catch (ex) {
46
      console.error({
×
47
        ruleId: null,
48
        fatal: true,
49
        severity: 2,
50
        source: null,
51
        message: ex.message,
52
        line: 0,
53
        column: 0,
54
      });
55

56
      return null;
×
57
    }
58

59
    // merge in any additional parser options
60
    if (config.parserOptions) {
36!
61
      parserOptions = Object.assign({}, config.parserOptions, parserOptions);
×
62
    }
63

64
    /*
65
     * Check for parsing errors first. If there's a parsing error, nothing
66
     * else can happen. However, a parsing error does not throw an error
67
     * from this method - it's just considered a fatal error message, a
68
     * problem that ESLint identified just like any other.
69
     */
70
    try {
36✔
71
      if (typeof parser.parseForESLint === 'function') {
36!
72
        return parser.parseForESLint(text, parserOptions);
×
73
      }
74
      return parser.parse(text, parserOptions);
36✔
75
    } catch (ex) {
76
      // If the message includes a leading line number, strip it:
77
      const message = ex.message.replace(/^line \d+:/i, '').trim();
×
78
      const source = ex.lineNumber
×
79
        ? SourceCode.splitLines(text)[ex.lineNumber - 1]
80
        : null;
81

82
      console.error({
×
83
        ruleId: null,
84
        fatal: true,
85
        severity: 2,
86
        source,
87
        message: `Parsing error: ${message}`,
88

89
        line: ex.lineNumber,
90
        column: ex.column,
91
      });
92
      return null;
×
93
    }
94
  }
95

96
  const text = fs.readFileSync(fileName, 'utf-8');
36✔
97
  let parseResult = parse(
36✔
98
    stripUnicodeBOM(text).replace(/^#!([^\r\n]+)/, (match, captured) => {
99
      return `//${captured}`;
×
100
    }),
101
    { parser: 'espree' },
102
    fileName
103
  );
104

105
  let ast;
106
  if (parseResult && parseResult.ast) {
36!
107
    ast = parseResult.ast;
×
108
  } else {
109
    ast = parseResult;
36✔
110
    parseResult = null;
36✔
111
  }
112

113
  return ast && new SourceCode(text, ast);
36✔
114
}
115

116
module.exports = getSourceCode;
36✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc