UNPKG

16.3 kBSource Map (JSON)View Raw
1{"version":3,"names":["_decorators","require","FEATURES","exports","Object","freeze","fields","privateMethods","decorators","privateIn","staticBlocks","featuresSameLoose","Map","featuresKey","looseKey","looseLowPriorityKey","canIgnoreLoose","file","feature","get","enableFeature","loose","hasFeature","set","setLoose","resolvedLoose","mask","name","isLoose","Error","getBabelShowConfigForHint","higherPriorityPluginName","undefined","console","warn","filename","opts","shouldTransform","path","decoratorPath","publicFieldPath","privateFieldPath","privateMethodPath","staticBlockPath","hasOwnDecorators","node","el","isClassProperty","isClassPrivateProperty","isClassPrivateMethod","isStaticBlock","buildCodeFrameError"],"sources":["../src/features.ts"],"sourcesContent":["import type { File, types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/core\";\nimport { hasOwnDecorators } from \"./decorators-2018-09.ts\";\n\nexport const FEATURES = Object.freeze({\n //classes: 1 << 0,\n fields: 1 << 1,\n privateMethods: 1 << 2,\n decorators: 1 << 3,\n privateIn: 1 << 4,\n staticBlocks: 1 << 5,\n});\n\nconst featuresSameLoose = new Map([\n [FEATURES.fields, \"@babel/plugin-transform-class-properties\"],\n [FEATURES.privateMethods, \"@babel/plugin-transform-private-methods\"],\n [FEATURES.privateIn, \"@babel/plugin-transform-private-property-in-object\"],\n]);\n\n// We can't use a symbol because this needs to always be the same, even if\n// this package isn't deduped by npm. e.g.\n// - node_modules/\n// - @babel/plugin-class-features\n// - @babel/plugin-proposal-decorators\n// - node_modules\n// - @babel-plugin-class-features\nconst featuresKey = \"@babel/plugin-class-features/featuresKey\";\nconst looseKey = \"@babel/plugin-class-features/looseKey\";\n\nif (!process.env.BABEL_8_BREAKING) {\n // See https://github.com/babel/babel/issues/11622.\n // Since preset-env sets loose for the fields and private methods plugins, it can\n // cause conflicts with the loose mode set by an explicit plugin in the config.\n // To solve this problem, we ignore preset-env's loose mode if another plugin\n // explicitly sets it\n // The code to handle this logic doesn't check that \"low priority loose\" is always\n // the same. However, it is only set by the preset and not directly by users:\n // unless someone _wants_ to break it, it shouldn't be a problem.\n // eslint-disable-next-line no-var\n var looseLowPriorityKey =\n \"@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing\";\n}\n\nif (!process.env.BABEL_8_BREAKING) {\n // eslint-disable-next-line no-var\n var canIgnoreLoose = function (file: File, feature: number) {\n return !!(file.get(looseLowPriorityKey) & feature);\n };\n}\n\nexport function enableFeature(file: File, feature: number, loose: boolean) {\n // We can't blindly enable the feature because, if it was already set,\n // \"loose\" can't be changed, so that\n // @babel/plugin-class-properties { loose: true }\n // @babel/plugin-class-properties { loose: false }\n // is transformed in loose mode.\n // We only enabled the feature if it was previously disabled.\n if (process.env.BABEL_8_BREAKING) {\n if (!hasFeature(file, feature)) {\n file.set(featuresKey, file.get(featuresKey) | feature);\n setLoose(file, feature, loose);\n }\n } else if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {\n file.set(featuresKey, file.get(featuresKey) | feature);\n if (\n // @ts-expect-error comparing loose with internal private magic string\n loose ===\n \"#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error\"\n ) {\n setLoose(file, feature, true);\n file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);\n } else if (\n // @ts-expect-error comparing loose with internal private magic string\n loose ===\n \"#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error\"\n ) {\n setLoose(file, feature, false);\n file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);\n } else {\n setLoose(file, feature, loose);\n }\n }\n\n let resolvedLoose: boolean | undefined;\n for (const [mask, name] of featuresSameLoose) {\n if (!hasFeature(file, mask)) continue;\n if (!process.env.BABEL_8_BREAKING) {\n if (canIgnoreLoose(file, mask)) continue;\n }\n\n const loose = isLoose(file, mask);\n\n if (resolvedLoose === !loose) {\n throw new Error(\n \"'loose' mode configuration must be the same for @babel/plugin-transform-class-properties, \" +\n \"@babel/plugin-transform-private-methods and \" +\n \"@babel/plugin-transform-private-property-in-object (when they are enabled).\" +\n \"\\n\\n\" +\n getBabelShowConfigForHint(file),\n );\n } else {\n resolvedLoose = loose;\n\n if (!process.env.BABEL_8_BREAKING) {\n // eslint-disable-next-line no-var\n var higherPriorityPluginName = name;\n }\n }\n }\n\n if (!process.env.BABEL_8_BREAKING && resolvedLoose !== undefined) {\n for (const [mask, name] of featuresSameLoose) {\n if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {\n setLoose(file, mask, resolvedLoose);\n console.warn(\n `Though the \"loose\" option was set to \"${!resolvedLoose}\" in your @babel/preset-env ` +\n `config, it will not be used for ${name} since the \"loose\" mode option was set to ` +\n `\"${resolvedLoose}\" for ${higherPriorityPluginName}.\\nThe \"loose\" option must be the ` +\n `same for @babel/plugin-transform-class-properties, @babel/plugin-transform-private-methods ` +\n `and @babel/plugin-transform-private-property-in-object (when they are enabled): you can ` +\n `silence this warning by explicitly adding\\n` +\n `\\t[\"${name}\", { \"loose\": ${resolvedLoose} }]\\n` +\n `to the \"plugins\" section of your Babel config.` +\n \"\\n\\n\" +\n getBabelShowConfigForHint(file),\n );\n }\n }\n }\n}\n\nfunction getBabelShowConfigForHint(file: File) {\n let { filename } = file.opts;\n if (!filename || filename === \"unknown\") {\n filename = \"[name of the input file]\";\n }\n return `\\\nIf you already set the same 'loose' mode for these plugins in your config, it's possible that they \\\nare enabled multiple times with different options.\nYou can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded \\\nconfiguration:\n\\tnpx cross-env BABEL_SHOW_CONFIG_FOR=${filename} <your build command>\nSee https://babeljs.io/docs/configuration#print-effective-configs for more info.`;\n}\n\nfunction hasFeature(file: File, feature: number) {\n return !!(file.get(featuresKey) & feature);\n}\n\nexport function isLoose(file: File, feature: number) {\n return !!(file.get(looseKey) & feature);\n}\n\nfunction setLoose(file: File, feature: number, loose: boolean) {\n if (loose) file.set(looseKey, file.get(looseKey) | feature);\n else file.set(looseKey, file.get(looseKey) & ~feature);\n\n if (!process.env.BABEL_8_BREAKING) {\n file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);\n }\n}\n\nexport function shouldTransform(path: NodePath<t.Class>, file: File): boolean {\n let decoratorPath: NodePath<t.Decorator> | null = null;\n let publicFieldPath: NodePath<t.ClassProperty> | null = null;\n let privateFieldPath: NodePath<t.ClassPrivateProperty> | null = null;\n let privateMethodPath: NodePath<t.ClassPrivateMethod> | null = null;\n let staticBlockPath: NodePath<t.StaticBlock> | null = null;\n\n if (hasOwnDecorators(path.node)) {\n decoratorPath = path.get(\"decorators.0\");\n }\n for (const el of path.get(\"body.body\")) {\n if (!decoratorPath && hasOwnDecorators(el.node)) {\n decoratorPath = el.get(\"decorators.0\");\n }\n if (!publicFieldPath && el.isClassProperty()) {\n publicFieldPath = el;\n }\n if (!privateFieldPath && el.isClassPrivateProperty()) {\n privateFieldPath = el;\n }\n // NOTE: path.isClassPrivateMethod() it isn't supported in <7.2.0\n if (!privateMethodPath && el.isClassPrivateMethod?.()) {\n privateMethodPath = el;\n }\n if (!staticBlockPath && el.isStaticBlock?.()) {\n staticBlockPath = el;\n }\n }\n\n if (decoratorPath && privateFieldPath) {\n throw privateFieldPath.buildCodeFrameError(\n \"Private fields in decorated classes are not supported yet.\",\n );\n }\n if (decoratorPath && privateMethodPath) {\n throw privateMethodPath.buildCodeFrameError(\n \"Private methods in decorated classes are not supported yet.\",\n );\n }\n\n if (decoratorPath && !hasFeature(file, FEATURES.decorators)) {\n throw path.buildCodeFrameError(\n \"Decorators are not enabled.\" +\n \"\\nIf you are using \" +\n '[\"@babel/plugin-proposal-decorators\", { \"version\": \"legacy\" }], ' +\n 'make sure it comes *before* \"@babel/plugin-transform-class-properties\" ' +\n \"and enable loose mode, like so:\\n\" +\n '\\t[\"@babel/plugin-proposal-decorators\", { \"version\": \"legacy\" }]\\n' +\n '\\t[\"@babel/plugin-transform-class-properties\", { \"loose\": true }]',\n );\n }\n\n if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {\n throw privateMethodPath.buildCodeFrameError(\n \"Class private methods are not enabled. \" +\n \"Please add `@babel/plugin-transform-private-methods` to your configuration.\",\n );\n }\n\n if (\n (publicFieldPath || privateFieldPath) &&\n !hasFeature(file, FEATURES.fields) &&\n // We want to allow enabling the private-methods plugin even without enabling\n // the class-properties plugin. Class fields will still be compiled in classes\n // that contain private methods.\n // This is already allowed with the other various class features plugins, but\n // it's because they can fallback to a transform separated from this helper.\n !hasFeature(file, FEATURES.privateMethods)\n ) {\n throw path.buildCodeFrameError(\n \"Class fields are not enabled. \" +\n \"Please add `@babel/plugin-transform-class-properties` to your configuration.\",\n );\n }\n\n if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {\n throw path.buildCodeFrameError(\n \"Static class blocks are not enabled. \" +\n \"Please add `@babel/plugin-transform-class-static-block` to your configuration.\",\n );\n }\n\n if (decoratorPath || privateMethodPath || staticBlockPath) {\n // If one of those feature is used we know that its transform is\n // enabled, otherwise the previous checks throw.\n return true;\n }\n if (\n (publicFieldPath || privateFieldPath) &&\n hasFeature(file, FEATURES.fields)\n ) {\n return true;\n }\n\n return false;\n}\n"],"mappings":";;;;;;;;;AAEA,IAAAA,WAAA,GAAAC,OAAA;AAEO,MAAMC,QAAQ,GAAAC,OAAA,CAAAD,QAAA,GAAGE,MAAM,CAACC,MAAM,CAAC;EAEpCC,MAAM,EAAE,CAAC,IAAI,CAAC;EACdC,cAAc,EAAE,CAAC,IAAI,CAAC;EACtBC,UAAU,EAAE,CAAC,IAAI,CAAC;EAClBC,SAAS,EAAE,CAAC,IAAI,CAAC;EACjBC,YAAY,EAAE,CAAC,IAAI;AACrB,CAAC,CAAC;AAEF,MAAMC,iBAAiB,GAAG,IAAIC,GAAG,CAAC,CAChC,CAACV,QAAQ,CAACI,MAAM,EAAE,0CAA0C,CAAC,EAC7D,CAACJ,QAAQ,CAACK,cAAc,EAAE,yCAAyC,CAAC,EACpE,CAACL,QAAQ,CAACO,SAAS,EAAE,oDAA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