es-check 9.4.3 → 9.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -2
- package/package.json +1 -1
- package/utils.js +16 -67
package/index.js
CHANGED
|
@@ -492,8 +492,8 @@ async function runChecks(configs, loggerOrOptions) {
|
|
|
492
492
|
|
|
493
493
|
const needsFullAST = checkFeatures;
|
|
494
494
|
const parserOptions = needsFullAST ? acornOpts : { ...acornOpts, locations: false, ranges: false, onComment: null };
|
|
495
|
-
|
|
496
|
-
const { ast, error: parseError } = parseCode(code, parserOptions, acorn, file
|
|
495
|
+
|
|
496
|
+
const { ast, error: parseError } = parseCode(code, parserOptions, acorn, file);
|
|
497
497
|
if (parseError) {
|
|
498
498
|
if (isDebug) {
|
|
499
499
|
logger.debug(`ES-Check: failed to parse file: ${file} \n - error: ${parseError.err}`)
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const winston = require('winston');
|
|
3
3
|
const supportsColor = require('supports-color');
|
|
4
|
+
const { fastBrakeSync } = require('fast-brake/sync');
|
|
5
|
+
const esversionPlugin = require('fast-brake/plugins/esversion');
|
|
6
|
+
const fastbrake = fastBrakeSync({ plugins: [esversionPlugin.default] });
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* Parse ignore list from options
|
|
@@ -487,80 +490,26 @@ function getTargetVersion(ecmaVersion) {
|
|
|
487
490
|
}
|
|
488
491
|
|
|
489
492
|
/**
|
|
490
|
-
* Parse code with
|
|
493
|
+
* Parse code with Acorn and handle errors
|
|
491
494
|
* @param {string} code - Code to parse
|
|
492
|
-
* @param {Object} acornOpts - Parsing options
|
|
493
|
-
* @param {Object} acorn -
|
|
495
|
+
* @param {Object} acornOpts - Parsing options
|
|
496
|
+
* @param {Object} acorn - Acorn parser instance
|
|
494
497
|
* @param {string} file - File path for error reporting
|
|
495
498
|
* @returns {{ast: Object, error: null} | {ast: null, error: Object}}
|
|
496
499
|
*/
|
|
497
|
-
|
|
498
|
-
const esversionPlugin = require('fast-brake/plugins/esversion');
|
|
499
|
-
const { VERSION_ORDER } = require('./constants');
|
|
500
|
-
|
|
501
|
-
const fastbrake = fastBrakeSync({ plugins: [esversionPlugin.default] });
|
|
502
|
-
const parseCache = new Map();
|
|
503
|
-
|
|
504
|
-
function checkModuleCompatibility(code, targetVersion, versionOrder, needsFeatures) {
|
|
505
|
-
const allFeatures = fastbrake.detect(code);
|
|
506
|
-
const nonModuleFeatures = allFeatures.filter(f => f.name !== 'import' && f.name !== 'export');
|
|
507
|
-
|
|
508
|
-
const targetIdx = versionOrder.indexOf(targetVersion);
|
|
509
|
-
const hasIncompat = nonModuleFeatures.some(f => versionOrder.indexOf(f.version) > targetIdx);
|
|
510
|
-
|
|
511
|
-
if (hasIncompat) {
|
|
512
|
-
throw new Error(`Code contains features incompatible with ${targetVersion}`);
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
const detectedFeatures = needsFeatures ? allFeatures : [];
|
|
516
|
-
return { ast: { type: 'Program', features: detectedFeatures }, error: null };
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
function parseCode(code, acornOpts, acorn, file, needsFeatures = false) {
|
|
520
|
-
const cacheKey = `${file}:${acornOpts.ecmaVersion}:${acornOpts.sourceType}:${needsFeatures}:${code.length}`;
|
|
521
|
-
|
|
522
|
-
if (parseCache.has(cacheKey)) {
|
|
523
|
-
return parseCache.get(cacheKey);
|
|
524
|
-
}
|
|
525
|
-
|
|
500
|
+
function parseCode(code, acornOpts, acorn, file) {
|
|
526
501
|
try {
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
const sourceType = acornOpts.sourceType || 'script';
|
|
530
|
-
|
|
531
|
-
const codeToCheck = acornOpts.allowHashBang && code.startsWith('#!')
|
|
532
|
-
? code.slice(code.indexOf('\n') + 1)
|
|
533
|
-
: code;
|
|
534
|
-
|
|
535
|
-
if (sourceType === 'module') {
|
|
536
|
-
const result = checkModuleCompatibility(codeToCheck, targetVersion, VERSION_ORDER, needsFeatures);
|
|
537
|
-
parseCache.set(cacheKey, result);
|
|
538
|
-
return result;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
const options = { target: targetVersion, sourceType };
|
|
542
|
-
const isCompatible = fastbrake.check(codeToCheck, options);
|
|
543
|
-
if (!isCompatible) {
|
|
544
|
-
throw new Error(`Code contains features incompatible with ${targetVersion}`);
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
const detectedFeatures = needsFeatures
|
|
548
|
-
? fastbrake.detect(codeToCheck)
|
|
549
|
-
: [];
|
|
550
|
-
|
|
551
|
-
const result = {
|
|
552
|
-
ast: { type: 'Program', features: detectedFeatures },
|
|
553
|
-
error: null
|
|
554
|
-
};
|
|
555
|
-
parseCache.set(cacheKey, result);
|
|
556
|
-
return result;
|
|
502
|
+
const ast = acorn.parse(code, acornOpts);
|
|
503
|
+
return { ast, error: null };
|
|
557
504
|
} catch (err) {
|
|
558
|
-
|
|
505
|
+
return {
|
|
559
506
|
ast: null,
|
|
560
|
-
error: {
|
|
507
|
+
error: {
|
|
508
|
+
err,
|
|
509
|
+
stack: err.stack,
|
|
510
|
+
file
|
|
511
|
+
}
|
|
561
512
|
};
|
|
562
|
-
parseCache.set(cacheKey, result);
|
|
563
|
-
return result;
|
|
564
513
|
}
|
|
565
514
|
}
|
|
566
515
|
|
|
@@ -626,6 +575,7 @@ function handleESVersionError(options) {
|
|
|
626
575
|
}
|
|
627
576
|
}
|
|
628
577
|
|
|
578
|
+
|
|
629
579
|
function parseLightMode(code, ecmaVersion, isModule, allowHashBang, file) {
|
|
630
580
|
const targetVersion = getTargetVersion(ecmaVersion);
|
|
631
581
|
|
|
@@ -675,7 +625,6 @@ module.exports = {
|
|
|
675
625
|
readFileAsync,
|
|
676
626
|
clearFileCache,
|
|
677
627
|
getFileCacheStats,
|
|
678
|
-
checkModuleCompatibility,
|
|
679
628
|
parseCode,
|
|
680
629
|
parseLightMode,
|
|
681
630
|
determineInvocationType,
|