|
3 | 3 | // CodeMirror, copyright (c) by Marijn Haverbeke and others |
4 | 4 | // Distributed under an MIT license: https://codemirror.net/5/LICENSE |
5 | 5 |
|
6 | | -( ( CodeMirror ) => { |
7 | | - 'use strict'; |
| 6 | +const CodeMirror = require( 'codemirror' ); |
8 | 7 |
|
9 | | - /** |
10 | | - * CodeMirror Lint Error. |
11 | | - * |
12 | | - * @see https://codemirror.net/5/doc/manual.html#addon_lint |
13 | | - * |
14 | | - * @typedef {Object} CodeMirrorLintError |
15 | | - * @property {string} message - Error message. |
16 | | - * @property {'error'} severity - Severity. |
17 | | - * @property {CodeMirror.Position} from - From position. |
18 | | - * @property {CodeMirror.Position} to - To position. |
19 | | - */ |
| 8 | +/** |
| 9 | + * CodeMirror Lint Error. |
| 10 | + * |
| 11 | + * @see https://codemirror.net/5/doc/manual.html#addon_lint |
| 12 | + * |
| 13 | + * @typedef {Object} CodeMirrorLintError |
| 14 | + * @property {string} message - Error message. |
| 15 | + * @property {'error'} severity - Severity. |
| 16 | + * @property {CodeMirror.Position} from - From position. |
| 17 | + * @property {CodeMirror.Position} to - To position. |
| 18 | + */ |
20 | 19 |
|
21 | | - /** |
22 | | - * JSHint options supported by Espree. |
23 | | - * |
24 | | - * @see https://jshint.com/docs/options/ |
25 | | - * @see https://www.npmjs.com/package/espree#options |
26 | | - * |
27 | | - * @typedef {Object} SupportedJSHintOptions |
28 | | - * @property {number} [esversion] - "This option is used to specify the ECMAScript version to which the code must adhere." |
29 | | - * @property {boolean} [es5] - "This option enables syntax first defined in the ECMAScript 5.1 specification. This includes allowing reserved keywords as object properties." |
30 | | - * @property {boolean} [es3] - "This option tells JSHint that your code needs to adhere to ECMAScript 3 specification. Use this option if you need your program to be executable in older browsers—such as Internet Explorer 6/7/8/9—and other legacy JavaScript environments." |
31 | | - * @property {boolean} [module] - "This option informs JSHint that the input code describes an ECMAScript 6 module. All module code is interpreted as strict mode code." |
32 | | - * @property {'implied'} [strict] - "This option requires the code to run in ECMAScript 5's strict mode." |
33 | | - */ |
| 20 | +/** |
| 21 | + * JSHint options supported by Espree. |
| 22 | + * |
| 23 | + * @see https://jshint.com/docs/options/ |
| 24 | + * @see https://www.npmjs.com/package/espree#options |
| 25 | + * |
| 26 | + * @typedef {Object} SupportedJSHintOptions |
| 27 | + * @property {number} [esversion] - "This option is used to specify the ECMAScript version to which the code must adhere." |
| 28 | + * @property {boolean} [es5] - "This option enables syntax first defined in the ECMAScript 5.1 specification. This includes allowing reserved keywords as object properties." |
| 29 | + * @property {boolean} [es3] - "This option tells JSHint that your code needs to adhere to ECMAScript 3 specification. Use this option if you need your program to be executable in older browsers—such as Internet Explorer 6/7/8/9—and other legacy JavaScript environments." |
| 30 | + * @property {boolean} [module] - "This option informs JSHint that the input code describes an ECMAScript 6 module. All module code is interpreted as strict mode code." |
| 31 | + * @property {'implied'} [strict] - "This option requires the code to run in ECMAScript 5's strict mode." |
| 32 | + */ |
34 | 33 |
|
35 | | - /** |
36 | | - * Validates JavaScript. |
37 | | - * |
38 | | - * @param {string} text - Source. |
39 | | - * @param {SupportedJSHintOptions} options - Linting options. |
40 | | - * @returns {Promise<CodeMirrorLintError[]>} |
41 | | - */ |
42 | | - async function validator( text, options ) { |
43 | | - const errors = /** @type {CodeMirrorLintError[]} */ []; |
44 | | - try { |
45 | | - const espree = await import( /* webpackIgnore: true */ 'espree' ); |
46 | | - espree.parse( text, { |
47 | | - ...getEspreeOptions( options ), |
48 | | - loc: true, |
| 34 | +/** |
| 35 | + * Validates JavaScript. |
| 36 | + * |
| 37 | + * @param {string} text - Source. |
| 38 | + * @param {SupportedJSHintOptions} options - Linting options. |
| 39 | + * @returns {Promise<CodeMirrorLintError[]>} |
| 40 | + */ |
| 41 | +async function validator( text, options ) { |
| 42 | + const errors = /** @type {CodeMirrorLintError[]} */ []; |
| 43 | + try { |
| 44 | + const espree = await import( /* webpackIgnore: true */ 'espree' ); |
| 45 | + espree.parse( text, { |
| 46 | + ...getEspreeOptions( options ), |
| 47 | + loc: true, |
| 48 | + } ); |
| 49 | + } catch ( error ) { |
| 50 | + if ( |
| 51 | + // This is an `EnhancedSyntaxError` in Espree: <https://github.com/brettz9/espree/blob/3c1120280b24f4a5e4c3125305b072fa0dfca22b/packages/espree/lib/espree.js#L48-L54>. |
| 52 | + error instanceof SyntaxError && |
| 53 | + typeof error.lineNumber === 'number' && |
| 54 | + typeof error.column === 'number' |
| 55 | + ) { |
| 56 | + const line = error.lineNumber - 1; |
| 57 | + errors.push( { |
| 58 | + message: error.message, |
| 59 | + severity: 'error', |
| 60 | + from: CodeMirror.Pos( line, error.column - 1 ), |
| 61 | + to: CodeMirror.Pos( line, error.column ), |
49 | 62 | } ); |
50 | | - } catch ( error ) { |
51 | | - if ( |
52 | | - // This is an `EnhancedSyntaxError` in Espree: <https://github.com/brettz9/espree/blob/3c1120280b24f4a5e4c3125305b072fa0dfca22b/packages/espree/lib/espree.js#L48-L54>. |
53 | | - error instanceof SyntaxError && |
54 | | - typeof error.lineNumber === 'number' && |
55 | | - typeof error.column === 'number' |
56 | | - ) { |
57 | | - const line = error.lineNumber - 1; |
58 | | - errors.push( { |
59 | | - message: error.message, |
60 | | - severity: 'error', |
61 | | - from: CodeMirror.Pos( line, error.column - 1 ), |
62 | | - to: CodeMirror.Pos( line, error.column ), |
63 | | - } ); |
64 | | - } else { |
65 | | - console.warn( '[CodeMirror] Unable to lint JavaScript:', error ); |
66 | | - } |
| 63 | + } else { |
| 64 | + console.warn( '[CodeMirror] Unable to lint JavaScript:', error ); |
67 | 65 | } |
68 | | - |
69 | | - return errors; |
70 | 66 | } |
71 | 67 |
|
72 | | - CodeMirror.registerHelper( 'lint', 'javascript', validator ); |
| 68 | + return errors; |
| 69 | +} |
73 | 70 |
|
74 | | - /** |
75 | | - * Gets the options for Espree from the supported JSHint options. |
76 | | - * |
77 | | - * @param {SupportedJSHintOptions} options - Linting options for JSHint. |
78 | | - * @return {{ |
79 | | - * ecmaVersion?: number|'latest', |
80 | | - * ecmaFeatures?: { |
81 | | - * impliedStrict?: true |
82 | | - * } |
83 | | - * }} |
84 | | - */ |
85 | | - function getEspreeOptions( options ) { |
86 | | - const ecmaFeatures = {}; |
87 | | - if ( options.strict === 'implied' ) { |
88 | | - ecmaFeatures.impliedStrict = true; |
89 | | - } |
| 71 | +CodeMirror.registerHelper( 'lint', 'javascript', validator ); |
90 | 72 |
|
91 | | - return { |
92 | | - ecmaVersion: getEcmaVersion( options ), |
93 | | - sourceType: options.module ? 'module' : 'script', |
94 | | - ecmaFeatures, |
95 | | - }; |
| 73 | +/** |
| 74 | + * Gets the options for Espree from the supported JSHint options. |
| 75 | + * |
| 76 | + * @param {SupportedJSHintOptions} options - Linting options for JSHint. |
| 77 | + * @return {{ |
| 78 | + * ecmaVersion?: number|'latest', |
| 79 | + * ecmaFeatures?: { |
| 80 | + * impliedStrict?: true |
| 81 | + * } |
| 82 | + * }} |
| 83 | + */ |
| 84 | +function getEspreeOptions( options ) { |
| 85 | + const ecmaFeatures = {}; |
| 86 | + if ( options.strict === 'implied' ) { |
| 87 | + ecmaFeatures.impliedStrict = true; |
96 | 88 | } |
97 | 89 |
|
98 | | - /** |
99 | | - * Gets the ECMAScript version. |
100 | | - * |
101 | | - * @param {SupportedJSHintOptions} options - Options. |
102 | | - * @return {number|'latest'} ECMAScript version. |
103 | | - */ |
104 | | - function getEcmaVersion( options ) { |
105 | | - if ( typeof options.esversion === 'number' ) { |
106 | | - return options.esversion; |
107 | | - } |
108 | | - if ( options.es5 ) { |
109 | | - return 5; |
110 | | - } |
111 | | - if ( options.es3 ) { |
112 | | - return 3; |
113 | | - } |
114 | | - return 'latest'; |
| 90 | + return { |
| 91 | + ecmaVersion: getEcmaVersion( options ), |
| 92 | + sourceType: options.module ? 'module' : 'script', |
| 93 | + ecmaFeatures, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Gets the ECMAScript version. |
| 99 | + * |
| 100 | + * @param {SupportedJSHintOptions} options - Options. |
| 101 | + * @return {number|'latest'} ECMAScript version. |
| 102 | + */ |
| 103 | +function getEcmaVersion( options ) { |
| 104 | + if ( typeof options.esversion === 'number' ) { |
| 105 | + return options.esversion; |
| 106 | + } |
| 107 | + if ( options.es5 ) { |
| 108 | + return 5; |
| 109 | + } |
| 110 | + if ( options.es3 ) { |
| 111 | + return 3; |
115 | 112 | } |
116 | | -} )( require( 'codemirror' ) ); |
| 113 | + return 'latest'; |
| 114 | +} |
0 commit comments