sucrase
Version:
Super-fast alternative to Babel for when you can target modern JS runtimes
219 lines (200 loc) • 8.82 kB
JavaScript
"use strict"; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }Object.defineProperty(exports, "__esModule", {value: true});
var _keywords = require('../parser/tokenizer/keywords');
var _types = require('../parser/tokenizer/types');
var _getNonTypeIdentifiers = require('../util/getNonTypeIdentifiers');
var _Transformer = require('./Transformer'); var _Transformer2 = _interopRequireDefault(_Transformer);
/**
* Class for editing import statements when we are keeping the code as ESM. We still need to remove
* type-only imports in TypeScript and Flow.
*/
class ESMImportTransformer extends _Transformer2.default {
constructor(
tokens,
nameManager,
reactHotLoaderTransformer,
isTypeScriptTransformEnabled,
options,
) {
super();this.tokens = tokens;this.nameManager = nameManager;this.reactHotLoaderTransformer = reactHotLoaderTransformer;this.isTypeScriptTransformEnabled = isTypeScriptTransformEnabled;;
this.nonTypeIdentifiers = isTypeScriptTransformEnabled
? _getNonTypeIdentifiers.getNonTypeIdentifiers.call(void 0, tokens, options)
: new Set();
}
process() {
// TypeScript `import foo = require('foo');` should always just be translated to plain require.
if (this.tokens.matches3(_types.TokenType._import, _types.TokenType.name, _types.TokenType.eq)) {
this.tokens.replaceToken("const");
return true;
}
if (this.tokens.matches2(_types.TokenType._export, _types.TokenType.eq)) {
this.tokens.replaceToken("module.exports");
return true;
}
if (this.tokens.matches1(_types.TokenType._import)) {
return this.processImport();
}
if (this.tokens.matches2(_types.TokenType._export, _types.TokenType._default)) {
return this.processExportDefault();
}
return false;
}
processImport() {
if (this.tokens.matches2(_types.TokenType._import, _types.TokenType.parenL)) {
// Dynamic imports don't need to be transformed.
return false;
}
const snapshot = this.tokens.snapshot();
const allImportsRemoved = this.removeTypeBindings();
if (allImportsRemoved) {
this.tokens.restoreToSnapshot(snapshot);
while (!this.tokens.matches1(_types.TokenType.string)) {
this.tokens.removeToken();
}
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.semi)) {
this.tokens.removeToken();
}
}
return true;
}
/**
* Remove type bindings from this import, leaving the rest of the import intact.
*
* Return true if this import was ONLY types, and thus is eligible for removal. This will bail out
* of the replacement operation, so we can return early here.
*/
removeTypeBindings() {
this.tokens.copyExpectedToken(_types.TokenType._import);
if (
this.tokens.matchesContextual(_keywords.ContextualKeyword._type) &&
!this.tokens.matches1AtIndex(this.tokens.currentIndex() + 1, _types.TokenType.comma) &&
!this.tokens.matchesContextualAtIndex(this.tokens.currentIndex() + 1, _keywords.ContextualKeyword._from)
) {
// This is an "import type" statement, so exit early.
return true;
}
if (this.tokens.matches1(_types.TokenType.string)) {
// This is a bare import, so we should proceed with the import.
this.tokens.copyToken();
return false;
}
let foundNonTypeImport = false;
if (this.tokens.matches1(_types.TokenType.name)) {
if (this.isTypeName(this.tokens.identifierName())) {
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.removeToken();
}
} else {
foundNonTypeImport = true;
this.tokens.copyToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.copyToken();
}
}
}
if (this.tokens.matches1(_types.TokenType.star)) {
if (this.isTypeName(this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 2))) {
this.tokens.removeToken();
this.tokens.removeToken();
this.tokens.removeToken();
} else {
foundNonTypeImport = true;
this.tokens.copyExpectedToken(_types.TokenType.star);
this.tokens.copyExpectedToken(_types.TokenType.name);
this.tokens.copyExpectedToken(_types.TokenType.name);
}
} else if (this.tokens.matches1(_types.TokenType.braceL)) {
this.tokens.copyToken();
while (!this.tokens.matches1(_types.TokenType.braceR)) {
if (
this.tokens.matches3(_types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
this.tokens.matches3(_types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
) {
// type foo
this.tokens.removeToken();
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.removeToken();
}
} else if (
this.tokens.matches5(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
this.tokens.matches5(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
) {
// type foo as bar
this.tokens.removeToken();
this.tokens.removeToken();
this.tokens.removeToken();
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.removeToken();
}
} else if (
this.tokens.matches2(_types.TokenType.name, _types.TokenType.comma) ||
this.tokens.matches2(_types.TokenType.name, _types.TokenType.braceR)
) {
// foo
if (this.isTypeName(this.tokens.identifierName())) {
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.removeToken();
}
} else {
foundNonTypeImport = true;
this.tokens.copyToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.copyToken();
}
}
} else if (
this.tokens.matches4(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.comma) ||
this.tokens.matches4(_types.TokenType.name, _types.TokenType.name, _types.TokenType.name, _types.TokenType.braceR)
) {
// foo as bar
if (this.isTypeName(this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 2))) {
this.tokens.removeToken();
this.tokens.removeToken();
this.tokens.removeToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.removeToken();
}
} else {
foundNonTypeImport = true;
this.tokens.copyToken();
this.tokens.copyToken();
this.tokens.copyToken();
if (this.tokens.matches1(_types.TokenType.comma)) {
this.tokens.copyToken();
}
}
} else {
throw new Error("Unexpected import form.");
}
}
this.tokens.copyExpectedToken(_types.TokenType.braceR);
}
return !foundNonTypeImport;
}
isTypeName(name) {
return this.isTypeScriptTransformEnabled && !this.nonTypeIdentifiers.has(name);
}
processExportDefault() {
const alreadyHasName =
this.tokens.matches4(_types.TokenType._export, _types.TokenType._default, _types.TokenType._function, _types.TokenType.name) ||
// export default async function
this.tokens.matches5(_types.TokenType._export, _types.TokenType._default, _types.TokenType.name, _types.TokenType._function, _types.TokenType.name) ||
this.tokens.matches4(_types.TokenType._export, _types.TokenType._default, _types.TokenType._class, _types.TokenType.name) ||
this.tokens.matches5(_types.TokenType._export, _types.TokenType._default, _types.TokenType._abstract, _types.TokenType._class, _types.TokenType.name);
if (!alreadyHasName && this.reactHotLoaderTransformer) {
// This is a plain "export default E" statement and we need to assign E to a variable.
// Change "export default E" to "let _default; export default _default = E"
const defaultVarName = this.nameManager.claimFreeName("_default");
this.tokens.replaceToken(`let ${defaultVarName}; export`);
this.tokens.copyToken();
this.tokens.appendCode(` ${defaultVarName} =`);
this.reactHotLoaderTransformer.setExtractedDefaultExportName(defaultVarName);
return true;
}
return false;
}
} exports.default = ESMImportTransformer;