Skip to content

Commit b4cbf01

Browse files
committed
more type annotations and cleanups
1 parent 87b8b26 commit b4cbf01

3 files changed

Lines changed: 62 additions & 46 deletions

File tree

src/index.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
import path from 'path';
22
import Macros from './utils/macros';
3-
import { normalizeOptions } from './utils/normalize-options';
3+
import { UserOptions, NormalizedOptions, normalizeOptions } from './utils/normalize-options';
4+
import * as Babel from '@babel/core';
5+
import type { types as t } from '@babel/core';
46

5-
export default function macros(babel) {
7+
interface State {
8+
opts: NormalizedOptions;
9+
macroBuilder: Macros;
10+
}
11+
12+
export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
613
let t = babel.types;
714

8-
function buildIdentifier(value, name) {
15+
function buildIdentifier(value: boolean, name: string) {
916
let replacement = t.booleanLiteral(value);
10-
11-
// when we only support babel@7 we should change this
12-
// to `path.addComment` or `t.addComment`
13-
let comment = {
14-
type: 'CommentBlock',
15-
value: ` ${name} `,
16-
leading: false,
17-
trailing: true,
18-
};
19-
replacement.trailingComments = [comment];
20-
17+
t.addComment(replacement, 'trailing', ` ${name} `);
2118
return replacement;
2219
}
2320

2421
return {
2522
name: 'babel-feature-flags-and-debug-macros',
2623
visitor: {
2724
ImportSpecifier(path, state) {
28-
let importPath = path.parent.source.value;
25+
let importPath = (path.parent as t.ImportDeclaration).source.value;
2926
let flagsForImport = state.opts.flags[importPath];
3027

3128
if (flagsForImport) {
32-
let flagName = path.node.imported.name;
29+
let flagName = t.isIdentifier(path.node.imported)
30+
? path.node.imported.name
31+
: path.node.imported.value;
3332
let localBindingName = path.node.local.name;
3433

3534
if (!(flagName in flagsForImport)) {
@@ -43,7 +42,7 @@ export default function macros(babel) {
4342
return;
4443
}
4544

46-
let binding = path.scope.getBinding(localBindingName);
45+
let binding = path.scope.getBinding(localBindingName)!;
4746

4847
binding.referencePaths.forEach((p) => {
4948
p.replaceWith(buildIdentifier(flagValue, flagName));
@@ -68,7 +67,9 @@ export default function macros(babel) {
6867

6968
Program: {
7069
enter(path, state) {
71-
state.opts = normalizeOptions(state.opts);
70+
// most of our plugin declares state.opts as already being normalized.
71+
// This is the spot where we force it become so.
72+
state.opts = normalizeOptions(state.opts as unknown as UserOptions);
7273
this.macroBuilder = new Macros(babel, state.opts);
7374

7475
let body = path.get('body');
@@ -105,5 +106,3 @@ export default function macros(babel) {
105106
macros.baseDir = function () {
106107
return path.resolve(__dirname, '..', '..');
107108
};
108-
109-

src/utils/macros.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Builder from './builder';
2+
import type * as Babel from '@babel/core';
3+
import type { NormalizedOptions } from './normalize-options';
24

35
const SUPPORTED_MACROS = ['assert', 'deprecate', 'warn', 'log'];
46

57
export default class Macros {
6-
constructor(babel, options) {
7-
this.babel = babel;
8+
constructor(babel: typeof Babel, options: NormalizedOptions) {
89
this.localDebugBindings = [];
910

1011
this.debugHelpers = options.externalizeHelpers || {};

src/utils/normalize-options.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { gt } from 'semver';
22

3-
function parseDebugTools(options: any): NormalizedOptions["debugTools"] {
3+
function parseDebugTools(options: UserOptions): {
4+
isDebug: boolean;
5+
debugToolsImport: string;
6+
assertPredicateIndex: number | undefined;
7+
} {
48
let debugTools = options.debugTools || {
59
isDebug: false,
610
source: '',
@@ -11,21 +15,22 @@ function parseDebugTools(options: any): NormalizedOptions["debugTools"] {
1115
let debugToolsImport = debugTools.source;
1216
let assertPredicateIndex = debugTools.assertPredicateIndex;
1317

14-
if (options.envFlags && isDebug === undefined) {
15-
isDebug = options.envFlags.flags.DEBUG;
16-
}
17-
1818
return {
1919
isDebug,
2020
debugToolsImport,
2121
assertPredicateIndex,
2222
};
2323
}
2424

25-
function evaluateFlagValue(options, name, flagName, flagValue) {
25+
function evaluateFlagValue(
26+
options: UserOptions,
27+
name: string | undefined,
28+
flagName: string,
29+
flagValue: string | boolean | null
30+
): boolean | null {
2631
let svelte = options.svelte;
2732

28-
if (typeof flagValue === 'string') {
33+
if (typeof flagValue === 'string' && name) {
2934
if (svelte && svelte[name]) {
3035
return gt(flagValue, svelte[name]);
3136
} else {
@@ -38,10 +43,10 @@ function evaluateFlagValue(options, name, flagName, flagValue) {
3843
}
3944
}
4045

41-
function parseFlags(options) {
46+
function parseFlags(options: UserOptions): Record<string, Record<string, boolean | null>> {
4247
let flagsProvided = options.flags || [];
4348

44-
let combinedFlags = {};
49+
let combinedFlags: Record<string, Record<string, boolean | null>> = {};
4550
flagsProvided.forEach((flagsDefinition) => {
4651
let source = flagsDefinition.source;
4752
let flagsForSource = (combinedFlags[source] = combinedFlags[source] || {});
@@ -62,27 +67,38 @@ function parseFlags(options) {
6267
}
6368

6469
export interface NormalizedOptions {
65-
externalizeHelpers: unknown;
66-
flags: unknown;
70+
externalizeHelpers: {
71+
module?: boolean;
72+
global?: string;
73+
};
74+
flags: Record<string, Record<string, boolean | null>>;
6775
svelte: unknown;
68-
debugTools: unknown;
76+
debugTools: {
77+
isDebug: boolean;
78+
debugToolsImport: string;
79+
assertPredicateIndex: number | undefined;
80+
};
6981
}
7082

71-
export function normalizeOptions(options: any): NormalizedOptions {
72-
let features = options.features || [];
73-
let externalizeHelpers = options.externalizeHelpers;
74-
let svelte = options.svelte;
75-
76-
if (!Array.isArray(features)) {
77-
features = [features];
78-
}
83+
export interface UserOptions {
84+
externalizeHelpers?: {
85+
module?: boolean;
86+
global?: string;
87+
};
88+
svelte?: Record<string, string>;
89+
flags?: { source: string; name?: string; flags: Record<string, boolean | string | null> }[];
90+
debugTools?: {
91+
isDebug: boolean;
92+
source: string;
93+
assertPredicateIndex?: number;
94+
};
95+
}
7996

97+
export function normalizeOptions(options: UserOptions): NormalizedOptions {
8098
return {
81-
externalizeHelpers,
99+
externalizeHelpers: options.externalizeHelpers ?? {},
82100
flags: parseFlags(options),
83-
svelte,
101+
svelte: options.svelte,
84102
debugTools: parseDebugTools(options),
85103
};
86104
}
87-
88-

0 commit comments

Comments
 (0)