Skip to content

Commit

Permalink
review and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored and nicolo-ribaudo committed Feb 18, 2023
1 parent 56e5d0c commit 8a27df1
Show file tree
Hide file tree
Showing 24 changed files with 171 additions and 79 deletions.
8 changes: 3 additions & 5 deletions packages/babel-core/src/transformation/file/generate.ts
Expand Up @@ -16,6 +16,8 @@ export default function generateCode(
const { opts, ast, code, inputMap } = file;
const { generatorOpts } = opts;

generatorOpts.inputSourceMap = inputMap?.toObject();

const results = [];
for (const plugins of pluginPasses) {
for (const plugin of plugins) {
Expand All @@ -30,11 +32,7 @@ export default function generateCode(

let result;
if (results.length === 0) {
result = generate(
ast,
{ ...generatorOpts, inputSourceMap: inputMap?.toObject() },
code,
);
result = generate(ast, generatorOpts, code);
} else if (results.length === 1) {
result = results[0];

Expand Down
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
"var t = x => x * x;"
],
"mappings": "AAAA,IAAIA,CAAC,GAAG,SAAJA,CAAC,CAAGC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAC;AAAA"
"mappings": "AAAA,IAAIA,CAAC,GAAG,SAAJA,CAACA,CAAGC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAC;AAAA"
}
Expand Up @@ -2,6 +2,11 @@
"version": 3,
"names": [
"Test",
"babelHelpers",
"classCallCheck",
"createClass",
"key",
"get",
"Error",
"test",
"bar"
Expand All @@ -12,5 +17,5 @@
"sourcesContent": [
"class Test {\n get bar() {\n throw new Error(\"wow\");\n }\n}\n\nvar test = new Test;\ntest.bar;"
],
"mappings": "IAAMA,IAAI;EAAA;;EAAA;IAAA;EAAA;EAAA;IAAA;IAAA,KACR,YAAU;MACR,MAAM,IAAIC,KAAK,CAAC,KAAK,CAAC;IACxB;EAAC;EAAA;AAAA;AAGH,IAAIC,IAAI,GAAG,IAAIF,IAAI;AACnBE,IAAI,CAACC,GAAG"
"mappings": "IAAMA,IAAI;EAAA;;EAAA,SAAAA,KAAA;IAAAC,YAAA,CAAAC,cAAA,OAAAF,IAAA;EAAA;EAAAC,YAAA,CAAAE,WAAA,CAAAH,IAAA;IAAAI,GAAA;IAAAC,GAAA,EACR,SAAAA,CAAA,EAAU;MACR,MAAM,IAAIC,KAAK,CAAC,KAAK,CAAC;IACxB;EAAC;EAAA,OAAAN,IAAA;AAAA;AAGH,IAAIO,IAAI,GAAG,IAAIP,IAAI;AACnBO,IAAI,CAACC,GAAG"
}
@@ -1,16 +1,15 @@
{
"version": 3,
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAsFA;EACAA;EACAC;IACA;MACAC;IACA;EACA;AACA",
"names": [
"name",
"data",
"msg"
],
"sourceRoot": "src/components",
"sources": [
"HelloWorld.vue"
"src/components/HelloWorld.vue"
],
"sourcesContent": [
"<template>\n <div class=\"hello\">\n <h1>{{ msg }}</h1>\n <h2>Essential Links</h2>\n <ul>\n <li>\n <a\n href=\"https://vuejs.org\"\n target=\"_blank\"\n >\n Core Docs\n </a>\n </li>\n <li>\n <a\n href=\"https://forum.vuejs.org\"\n target=\"_blank\"\n >\n Forum\n </a>\n </li>\n <li>\n <a\n href=\"https://chat.vuejs.org\"\n target=\"_blank\"\n >\n Community Chat\n </a>\n </li>\n <li>\n <a\n href=\"https://twitter.com/vuejs\"\n target=\"_blank\"\n >\n Twitter\n </a>\n </li>\n <br>\n <li>\n <a\n href=\"http://vuejs-templates.github.io/webpack/\"\n target=\"_blank\"\n >\n Docs for This Template\n </a>\n </li>\n </ul>\n <h2>Ecosystem</h2>\n <ul>\n <li>\n <a\n href=\"http://router.vuejs.org/\"\n target=\"_blank\"\n >\n vue-router\n </a>\n </li>\n <li>\n <a\n href=\"http://vuex.vuejs.org/\"\n target=\"_blank\"\n >\n vuex\n </a>\n </li>\n <li>\n <a\n href=\"http://vue-loader.vuejs.org/\"\n target=\"_blank\"\n >\n vue-loader\n </a>\n </li>\n <li>\n <a\n href=\"https://github.com/vuejs/awesome-vue\"\n target=\"_blank\"\n >\n awesome-vue\n </a>\n </li>\n </ul>\n </div>\n</template>\n\n<script>\nexport default {\n name: 'HelloWorld',\n data () {\n return {\n msg: 'Welcome to Your Vue.js App'\n }\n }\n}\n</script>\n\n<!-- Add \"scoped\" attribute to limit CSS to this component only -->\n<style scoped>\nh1, h2 {\n font-weight: normal;\n}\nul {\n list-style-type: none;\n padding: 0;\n}\nli {\n display: inline-block;\n margin: 0 10px;\n}\na {\n color: #42b983;\n}\n</style>\n"
]
],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eAsFA;EACAA,IAAA;EACAC,KAAA;IACA;MACAC,GAAA;IACA;EACA;AACA,G"
}
@@ -1,11 +1,13 @@
{
"version": 3,
"mappings": "AAAA,UAAU,Y;SAAM,C;CAAC",
"names": [],
"names": [
"foo"
],
"sources": [
"original.js"
],
"sourcesContent": [
"var foo = () => 4;"
]
],
"mappings": "AAAA,IAAAA,GAAA,GAAU,SAAAA,CAAA,E;SAAM,C;CAAC"
}
@@ -1,13 +1,17 @@
{
"version": 3,
"mappings": "AAAC,KAAG;ACAJ,KAAG",
"names": [],
"sources": [
"input.tsx",
"source-maps/input-source-map-multiple-output-sources-complete-replace/input.js",
"bar.js",
"baz.js"
],
"sourcesContent": [
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}",
"foo(1);\nfunction foo(bar) {\n throw new Error('Intentional.');\n}\n//# sourceMappingURL=input.js.map",
"<bar />",
"baz();"
]
],
"mappings": "AAAA,KAAI;AAAJ,KAAG"
}
@@ -1,17 +1,19 @@
{
"version": 3,
"mappings": "AAAC,KAAG;ACCJ,SAASA,GAAG,CAACC,GAAW;EACpB,MAAM,IAAIC,KAAK,CAAC,cAAc,CAAC;AACnC",
"names": [
"foo",
"bar",
"Error"
],
"sources": [
"test.js",
"input.tsx"
"input.tsx",
"source-maps/input-source-map-multiple-output-sources/input.js",
"test.js"
],
"sourcesContent": [
"<bar />",
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}"
]
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}",
"foo(1);\nfunction foo(bar) {\n throw new Error('Intentional.');\n}\n//# sourceMappingURL=input.js.map",
"<bar />"
],
"mappings": "AAAA,KAAI;AACJ,SAASA,GAAGA,CAACC,GAAW;EACpB,MAAM,IAAIC,KAAK,CAAC,cAAc,CAAC;AACnC"
}
@@ -1,11 +1,13 @@
{
"version": 3,
"mappings": "AAAA,UAAU,Y;SAAM,C;CAAC",
"names": [],
"names": [
"foo"
],
"sources": [
"source-maps/input-source-map-same-location/input.js"
],
"sourcesContent": [
"var foo = () => 4;"
]
],
"mappings": "AAAA,IAAAA,GAAA,GAAU,SAAAA,CAAA,E;SAAM,C;CAAC"
}
@@ -1,11 +1,15 @@
{
"version": 3,
"mappings": "AAAC,KAAG",
"names": [],
"sources": [
"input.tsx",
"source-maps/input-source-map-sources-complete-replace/input.js",
"test.js"
],
"sourcesContent": [
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}",
"foo(1);\nfunction foo(bar) {\n throw new Error('Intentional.');\n}\n//# sourceMappingURL=input.js.map",
"<bar />"
]
],
"mappings": "AAAA,KAAI"
}
@@ -1,11 +1,13 @@
{
"version": 3,
"mappings": "AAAA,UAAU,Y;SAAM,C;CAAC",
"names": [],
"names": [
"foo"
],
"sources": [
"original.js"
],
"sourcesContent": [
"var foo = () => 4;"
]
],
"mappings": "AAAA,IAAAA,GAAA,GAAU,SAAAA,CAAA,E;SAAM,C;CAAC"
}
25 changes: 21 additions & 4 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -42,6 +42,7 @@ export default class Buffer {
_last = 0;
_queue: QueueItem[] = [];
_queueCursor = 0;
_canMarkIdName = true;

_position = {
line: 1,
Expand Down Expand Up @@ -226,8 +227,10 @@ export default class Buffer {
this._position.column = 0;
}

sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
if (this._canMarkIdName) {
sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
}
}

_append(
Expand Down Expand Up @@ -257,7 +260,10 @@ export default class Buffer {
const { column, identifierName, identifierNamePos, filename } = sourcePos;
let line = sourcePos.line;

if (identifierName != null || identifierNamePos != null) {
if (
(identifierName != null || identifierNamePos != null) &&
this._canMarkIdName
) {
sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
}
Expand Down Expand Up @@ -407,9 +413,20 @@ export default class Buffer {
if (!this._map) return cb();

this.source("start", loc);

// @ts-expect-error identifierName is not defined
const identifierName = loc.identifierName;
const sourcePos = this._sourcePosition;
if (identifierName) {
this._canMarkIdName = false;
sourcePos.identifierName = identifierName;
}
cb();

if (identifierName) {
this._canMarkIdName = true;
sourcePos.identifierName = undefined;
sourcePos.identifierNamePos = undefined;
}
this.source("end", loc);
}

Expand Down
74 changes: 48 additions & 26 deletions packages/babel-generator/src/generators/methods.ts
@@ -1,12 +1,15 @@
import type Printer from "../printer";
import type * as t from "@babel/types";
import { isIdentifier } from "@babel/types";
import type { NodePath } from "@babel/traverse";

export function _params(
this: Printer,
node: t.Function | t.TSDeclareMethod | t.TSDeclareFunction,
idNode: t.Node,
parentNode: t.Node,
idNode: t.Expression | t.PrivateName,
parentNode: NodePath<
t.Function | t.TSDeclareMethod | t.TSDeclareFunction
>["parent"],
) {
this.print(node.typeParameters, node);

Expand Down Expand Up @@ -141,7 +144,9 @@ export function _predicate(
export function _functionHead(
this: Printer,
node: t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction,
parent: t.Node,
parent: NodePath<
t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction
>["parent"],
) {
if (node.async) {
this.word("async");
Expand Down Expand Up @@ -174,7 +179,7 @@ export function _functionHead(
export function FunctionExpression(
this: Printer,
node: t.FunctionExpression,
parent: t.Node,
parent: NodePath<t.FunctionExpression>["parent"],
) {
this._functionHead(node, parent);
this.space();
Expand All @@ -186,7 +191,7 @@ export { FunctionExpression as FunctionDeclaration };
export function ArrowFunctionExpression(
this: Printer,
node: t.ArrowFunctionExpression,
parent: t.Node,
parent: NodePath<t.ArrowFunctionExpression>["parent"],
) {
if (node.async) {
this.word("async", true);
Expand Down Expand Up @@ -236,44 +241,61 @@ function hasTypesOrComments(
);
}

function _getFuncIdName(this: Printer, idNode: t.Node, parent: t.Node) {
let id = idNode;
function _getFuncIdName(
this: Printer,
idNode: t.Expression | t.PrivateName,
parent: NodePath<
t.Function | t.TSDeclareMethod | t.TSDeclareFunction
>["parent"],
) {
let id: t.Expression | t.PrivateName | t.LVal = idNode;

if (!id && parent) {
const parentType = parent.type;

if (parentType === "VariableDeclarator") {
id = parent.id;
} else if (
parentType === "AssignmentExpression" ||
parentType === "AssignmentPattern"
) {
id = parent.left;
} else if (
parentType === "ObjectProperty" ||
parentType === "ClassProperty"
) {
if (!parent.computed || parent.key.type === "StringLiteral") {
id = parent.key;
}
} else if (
parentType === "ClassPrivateProperty" ||
parentType === "ClassAccessorProperty"
) {
id = parent.key;
}
}

if (!id) return;

let nameInfo;
if (id) {
if (id.type === "Identifier") {
nameInfo = {
pos: id.loc?.start,
name:
// @ts-expect-error Undocumented property identifierName
id.loc?.identifierName || id.name,
};
} else if (id.type === "PrivateName") {
nameInfo = {
pos: id.id.loc?.start,
name: id.id.name,
};
} else if (id.type === "StringLiteral") {
nameInfo = {
pos: id.loc?.start,
name: id.value,
};
}

if (id.type === "Identifier") {
nameInfo = {
pos: id.loc?.start,
name:
// @ts-expect-error Undocumented property identifierName
id.loc?.identifierName || id.name,
};
} else if (id.type === "PrivateName") {
nameInfo = {
pos: id.loc?.start,
name: "#" + id.id.name,
};
} else if (id.type === "StringLiteral") {
nameInfo = {
pos: id.loc?.start,
name: id.value,
};
}

return nameInfo;
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-generator/src/generators/typescript.ts
@@ -1,5 +1,6 @@
import type Printer from "../printer";
import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";

export function TSTypeAnnotation(this: Printer, node: t.TSTypeAnnotation) {
this.token(":");
Expand Down Expand Up @@ -76,7 +77,7 @@ export function TSParameterProperty(
export function TSDeclareFunction(
this: Printer,
node: t.TSDeclareFunction,
parent: t.Node,
parent: NodePath<t.TSDeclareFunction>["parent"],
) {
if (node.declare) {
this.word("declare");
Expand Down

0 comments on commit 8a27df1

Please sign in to comment.