Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Apr 27, 2022
1 parent 1713327 commit 6163af9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
21 changes: 3 additions & 18 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -2,21 +2,6 @@ import type SourceMap from "./source-map";
import type * as t from "@babel/types";
import * as charcodes from "charcodes";

import type {
EncodedSourceMap,
DecodedSourceMap,
Mapping,
} from "@jridgewell/gen-mapping";

type Mutable<T> = { -readonly [P in keyof T]: T[P] };

type Result = {
code: string;
map: Mutable<EncodedSourceMap> | undefined | null;
decodedMap: DecodedSourceMap | undefined | null;
rawMappings: Mapping[] | null;
};

const SPACES_RE = /^[ \t]+$/;
export default class Buffer {
constructor(map?: SourceMap | null) {
Expand Down Expand Up @@ -53,11 +38,11 @@ export default class Buffer {
* Get the final string output from the buffer, along with the sourcemap if one exists.
*/

get(): Result {
get() {
this._flush();

const map = this._map;
const result: Result = {
const result = {
// Whatever trim is used here should not execute a regex against the
// source string since it may be arbitrarily large after all transformations
code: this._buf.trimRight(),
Expand All @@ -66,7 +51,7 @@ export default class Buffer {

// Encoding the sourcemap is moderately CPU expensive.
get map() {
return (result.map = map?.get());
return (result.map = map ? map.get() : null);
},
set map(value) {
Object.defineProperty(result, "map", { value, writable: true });
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-generator/src/index.ts
Expand Up @@ -3,6 +3,7 @@ import Printer from "./printer";
import type * as t from "@babel/types";

import type { Format } from "./printer";
import type { DecodedSourceMap, Mapping } from "@jridgewell/gen-mapping";

/**
* Babel's code generator, turns an ast into code, maintaining sourcemaps,
Expand Down Expand Up @@ -225,6 +226,8 @@ export interface GeneratorResult {
mappings: string;
file: string;
} | null;
decodedMap: DecodedSourceMap | undefined;
rawMappings: Mapping[] | undefined;
}

/**
Expand Down

0 comments on commit 6163af9

Please sign in to comment.