Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | exports.__esModule = true; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 3 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 4 | const moduleRequire = require('./module-require').default; |
| 5 | const extname = require('path').extname; |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 6 | const fs = require('fs'); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 7 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 8 | const log = require('debug')('eslint-plugin-import:parse'); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 9 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame] | 10 | function getBabelEslintVisitorKeys(parserPath) { |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 11 | if (parserPath.endsWith('index.js')) { |
| 12 | const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js'); |
| 13 | if (fs.existsSync(hypotheticalLocation)) { |
| 14 | const keys = moduleRequire(hypotheticalLocation); |
| 15 | return keys.default || keys; |
| 16 | } |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 17 | } |
| 18 | return null; |
| 19 | } |
| 20 | |
| 21 | function keysFromParser(parserPath, parserInstance, parsedResult) { |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame] | 22 | // Exposed by @typescript-eslint/parser and @babel/eslint-parser |
| 23 | if (parsedResult && parsedResult.visitorKeys) { |
| 24 | return parsedResult.visitorKeys; |
| 25 | } |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 26 | if (/.*espree.*/.test(parserPath)) { |
| 27 | return parserInstance.VisitorKeys; |
| 28 | } |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame] | 29 | if (/.*babel-eslint.*/.test(parserPath)) { |
| 30 | return getBabelEslintVisitorKeys(parserPath); |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 31 | } |
| 32 | return null; |
| 33 | } |
| 34 | |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 35 | exports.default = function parse(path, content, context) { |
| 36 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 37 | if (context == null) throw new Error('need context to parse properly'); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 38 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 39 | let parserOptions = context.parserOptions; |
| 40 | const parserPath = getParserPath(path, context); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 41 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 42 | if (!parserPath) throw new Error('parserPath is required!'); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 43 | |
| 44 | // hack: espree blows up with frozen options |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 45 | parserOptions = Object.assign({}, parserOptions); |
| 46 | parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 47 | |
| 48 | // always include comments and tokens (for doc parsing) |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 49 | parserOptions.comment = true; |
| 50 | parserOptions.attachComment = true; // keeping this for backward-compat with older parsers |
| 51 | parserOptions.tokens = true; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 52 | |
| 53 | // attach node locations |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 54 | parserOptions.loc = true; |
| 55 | parserOptions.range = true; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 56 | |
| 57 | // provide the `filePath` like eslint itself does, in `parserOptions` |
| 58 | // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637 |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 59 | parserOptions.filePath = path; |
| 60 | |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 61 | // @typescript-eslint/parser will parse the entire project with typechecking if you provide |
| 62 | // "project" or "projects" in parserOptions. Removing these options means the parser will |
| 63 | // only parse one file in isolate mode, which is much, much faster. |
Tim van der Lippe | a661941 | 2021-09-13 14:28:55 +0200 | [diff] [blame] | 64 | // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962 |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 65 | delete parserOptions.project; |
| 66 | delete parserOptions.projects; |
| 67 | |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 68 | // require the parser relative to the main module (i.e., ESLint) |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 69 | const parser = moduleRequire(parserPath); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 70 | |
| 71 | if (typeof parser.parseForESLint === 'function') { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 72 | let ast; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 73 | try { |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 74 | const parserRaw = parser.parseForESLint(content, parserOptions); |
| 75 | ast = parserRaw.ast; |
| 76 | return { |
| 77 | ast, |
| 78 | visitorKeys: keysFromParser(parserPath, parser, parserRaw), |
| 79 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 80 | } catch (e) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 81 | console.warn(); |
| 82 | console.warn('Error while parsing ' + parserOptions.filePath); |
| 83 | console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 84 | } |
| 85 | if (!ast || typeof ast !== 'object') { |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 86 | console.warn( |
| 87 | '`parseForESLint` from parser `' + |
| 88 | parserPath + |
| 89 | '` is invalid and will just be ignored' |
| 90 | ); |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 91 | } else { |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 92 | return { |
| 93 | ast, |
| 94 | visitorKeys: keysFromParser(parserPath, parser, undefined), |
| 95 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Tim van der Lippe | bc3a0b7 | 2021-11-08 15:22:37 +0000 | [diff] [blame] | 99 | const keys = keysFromParser(parserPath, parser, undefined); |
| 100 | return { |
| 101 | ast: parser.parse(content, parserOptions), |
| 102 | visitorKeys: keys, |
| 103 | }; |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 104 | }; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 105 | |
| 106 | function getParserPath(path, context) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 107 | const parsers = context.settings['import/parsers']; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 108 | if (parsers != null) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 109 | const extension = extname(path); |
| 110 | for (const parserPath in parsers) { |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 111 | if (parsers[parserPath].indexOf(extension) > -1) { |
| 112 | // use this alternate parser |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 113 | log('using alt parser:', parserPath); |
| 114 | return parserPath; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | // default to use ESLint parser |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 119 | return context.parserPath; |
Tim van der Lippe | fdbd42e | 2020-04-07 15:14:36 +0100 | [diff] [blame] | 120 | } |