es-check 9.4.2 → 9.4.3
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/constants.js +6 -0
- package/package.json +1 -1
- package/utils.js +24 -2
package/constants.js
CHANGED
|
@@ -716,6 +716,11 @@ const JS_VERSIONS = [
|
|
|
716
716
|
'es13', 'es2022', 'es14', 'es2023', 'es15', 'es2024', 'es16', 'es2025', 'checkBrowser'
|
|
717
717
|
];
|
|
718
718
|
|
|
719
|
+
const VERSION_ORDER = [
|
|
720
|
+
'es5', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019',
|
|
721
|
+
'es2020', 'es2021', 'es2022', 'es2023', 'es2024', 'es2025'
|
|
722
|
+
];
|
|
723
|
+
|
|
719
724
|
/**
|
|
720
725
|
* Maps feature names from ES_FEATURES to their polyfill patterns.
|
|
721
726
|
* This version uses standardized keys and robust regex for both manual and module polyfills.
|
|
@@ -798,4 +803,5 @@ module.exports = {
|
|
|
798
803
|
BROWSER_TO_ES_VERSION,
|
|
799
804
|
FEATURE_TO_POLYFILL_MAP,
|
|
800
805
|
JS_VERSIONS,
|
|
806
|
+
VERSION_ORDER,
|
|
801
807
|
};
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -496,10 +496,26 @@ function getTargetVersion(ecmaVersion) {
|
|
|
496
496
|
*/
|
|
497
497
|
const { fastBrakeSync } = require('fast-brake/sync');
|
|
498
498
|
const esversionPlugin = require('fast-brake/plugins/esversion');
|
|
499
|
+
const { VERSION_ORDER } = require('./constants');
|
|
499
500
|
|
|
500
501
|
const fastbrake = fastBrakeSync({ plugins: [esversionPlugin.default] });
|
|
501
502
|
const parseCache = new Map();
|
|
502
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
|
+
|
|
503
519
|
function parseCode(code, acornOpts, acorn, file, needsFeatures = false) {
|
|
504
520
|
const cacheKey = `${file}:${acornOpts.ecmaVersion}:${acornOpts.sourceType}:${needsFeatures}:${code.length}`;
|
|
505
521
|
|
|
@@ -515,9 +531,14 @@ function parseCode(code, acornOpts, acorn, file, needsFeatures = false) {
|
|
|
515
531
|
const codeToCheck = acornOpts.allowHashBang && code.startsWith('#!')
|
|
516
532
|
? code.slice(code.indexOf('\n') + 1)
|
|
517
533
|
: code;
|
|
518
|
-
|
|
519
|
-
const options = { target: targetVersion, sourceType };
|
|
520
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 };
|
|
521
542
|
const isCompatible = fastbrake.check(codeToCheck, options);
|
|
522
543
|
if (!isCompatible) {
|
|
523
544
|
throw new Error(`Code contains features incompatible with ${targetVersion}`);
|
|
@@ -654,6 +675,7 @@ module.exports = {
|
|
|
654
675
|
readFileAsync,
|
|
655
676
|
clearFileCache,
|
|
656
677
|
getFileCacheStats,
|
|
678
|
+
checkModuleCompatibility,
|
|
657
679
|
parseCode,
|
|
658
680
|
parseLightMode,
|
|
659
681
|
determineInvocationType,
|