Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cse-machine/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as ast from '../utils/ast/astCreator';
import {
filterImportDeclarations,
getSourceVariableDeclaration,
getSpecifierName,
hasNoDeclarations,
hasNoImportDeclarations,
} from '../utils/ast/helpers';
Expand Down Expand Up @@ -237,7 +238,7 @@ function evaluateImports(program: es.Program, context: Context) {

switch (spec.type) {
case 'ImportSpecifier': {
obj = functions[spec.imported.name];
obj = functions[getSpecifierName(spec.imported)];
break;
}
case 'ImportDefaultSpecifier': {
Expand Down
3 changes: 2 additions & 1 deletion src/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
} from 'estree';

import type { Context, Node } from './types';
import { getSpecifierName } from './utils/ast/helpers';
import {
ancestor,
base,
Expand Down Expand Up @@ -90,7 +91,7 @@ export function findDeclarationNode(program: Node, identifier: Identifier): Node
}
},
ImportSpecifier(node: ImportSpecifier, _state: any, _callback: WalkerCallback<any>) {
if (node.imported.name === identifier.name) {
if (getSpecifierName(node.imported) === identifier.name) {
declarations.push(node.imported);
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/modules/preprocessor/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getIdsFromDeclaration,
getImportedName,
getModuleDeclarationSource,
getSpecifierName,
} from '../../utils/ast/helpers';
import { isModuleDeclaration, isNamespaceSpecifier } from '../../utils/ast/typeGuards';
import Dict, { ArrayMap } from '../../utils/dict';
Expand Down Expand Up @@ -87,7 +88,7 @@ export default function analyzeImportsAndExports(
}

for (const spec of node.specifiers) {
moduleDocs[sourceModule].add(spec.exported.name);
moduleDocs[sourceModule].add(getSpecifierName(spec.exported));
}

if (!node.source) continue;
Expand All @@ -103,7 +104,7 @@ export default function analyzeImportsAndExports(
if (dstModuleDocs.size === 0) throw new UndefinedNamespaceImportError(dstModule, node);

if (node.exported) {
moduleDocs[sourceModule].add(node.exported.name);
moduleDocs[sourceModule].add(getSpecifierName(node.exported));
} else {
for (const each of dstModuleDocs) {
if (each === 'default') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type es from 'estree';

import { accessExportFunctionName } from '../../../stdlib/localImport.prelude';
import * as create from '../../../utils/ast/astCreator';
import { getSpecifierName } from '../../../utils/ast/helpers';

/**
* Constructs a call to the `pair` function.
Expand Down Expand Up @@ -78,7 +79,7 @@ export const cloneAndStripImportSpecifier = (
return {
type: 'ImportSpecifier',
local: create.identifier(importSpecifier.local.name),
imported: create.identifier(importSpecifier.imported.name),
imported: create.identifier(getSpecifierName(importSpecifier.imported)),
};
case 'ImportDefaultSpecifier':
return {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/preprocessor/transformers/hoistAndMergeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type es from 'estree';
import { partition } from 'lodash';

import * as create from '../../../utils/ast/astCreator';
import { getModuleDeclarationSource } from '../../../utils/ast/helpers';
import { getModuleDeclarationSource, getSpecifierName } from '../../../utils/ast/helpers';
import { isImportDeclaration } from '../../../utils/ast/typeGuards';
import Dict from '../../../utils/dict';
import { isSourceModule } from '../../utils';
Expand Down Expand Up @@ -47,7 +47,7 @@ export default function hoistAndMergeImports(program: es.Program) {
break;
}
case 'ImportSpecifier': {
const importedName = spec.imported.name;
const importedName = getSpecifierName(spec.imported);
regularSpecifiers.setdefault(importedName, new Set()).add(declaredName);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type es from 'estree';
import { defaultExportLookupName } from '../../../stdlib/localImport.prelude';
import assert from '../../../utils/assert';
import * as create from '../../../utils/ast/astCreator';
import { getModuleDeclarationSource } from '../../../utils/ast/helpers';
import { getModuleDeclarationSource, getSpecifierName } from '../../../utils/ast/helpers';
import {
isDeclaration,
isDirective,
Expand Down Expand Up @@ -130,8 +130,10 @@ const getExportedNameToIdentifierMap = (
// Exported names can be renamed using the 'as' keyword. As such, the
// exported names and their corresponding identifiers might be different.
node.specifiers.forEach((node: es.ExportSpecifier): void => {
const exportedName = node.exported.name;
const identifier = node.local;
const exportedName = getSpecifierName(node.exported);
// Source only supports identifier locals (string-literal re-exports are
// not part of the language), so this is always an Identifier.
const identifier = node.local as es.Identifier;
exportedNameToIdentifierMap[exportedName] = identifier;
});
}
Expand Down Expand Up @@ -200,7 +202,7 @@ export const createAccessImportStatements = (
importDeclaration = createImportedNameDeclaration(
invokedFunctionResultVariableName,
importSpecifier.local,
importSpecifier.imported.name,
getSpecifierName(importSpecifier.imported),
);
break;
case 'ImportDefaultSpecifier':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ExportNamedDeclaration } from 'estree';
import { defaultExportLookupName } from '../../../stdlib/localImport.prelude';
import { getSpecifierName } from '../../../utils/ast/helpers';
import { mapAndFilter } from '../../../utils/misc';
import { RuleError } from '../../errors';
import { defineRule } from '../../types';
Expand All @@ -20,7 +21,7 @@ export default defineRule(
{
ExportNamedDeclaration(node) {
return mapAndFilter(node.specifiers, specifier =>
specifier.exported.name === defaultExportLookupName
getSpecifierName(specifier.exported) === defaultExportLookupName
? new NoExportNamedDeclarationWithDefaultError(node)
: undefined,
);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/source/rules/noExportNamedDeclarationWithSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ExportNamedDeclaration } from 'estree';
import { specifierToString } from '../../../utils/ast/helpers';
import { getSpecifierName, specifierToString } from '../../../utils/ast/helpers';
import { RuleError } from '../../errors';
import { defineRule } from '../../types';

Expand All @@ -11,7 +11,7 @@ export class NoExportNamedDeclarationWithSourceError extends RuleError<ExportNam
public override elaborate() {
const [imports, exps] = this.node.specifiers.reduce(
([ins, outs], spec) => [
[...ins, spec.local.name],
[...ins, getSpecifierName(spec.local)],
[...outs, specifierToString(spec)],
],
[[], []] as [string[], string[]],
Expand Down
3 changes: 2 additions & 1 deletion src/parser/source/rules/noImportSpecifierWithDefault.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ImportSpecifier } from 'estree';
import { defaultExportLookupName } from '../../../stdlib/localImport.prelude';
import { getSpecifierName } from '../../../utils/ast/helpers';
import { RuleError } from '../../errors';
import { defineRule } from '../../types';
import syntaxBlacklist from '../syntax';
Expand All @@ -18,7 +19,7 @@ export default defineRule(
'no-import-with-default',
{
ImportSpecifier(node) {
if (node.imported.name === defaultExportLookupName) {
if (getSpecifierName(node.imported) === defaultExportLookupName) {
return [new NoImportSpecifierWithDefaultError(node)];
}
return [];
Expand Down
6 changes: 3 additions & 3 deletions src/stdlib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse as sourceParse } from '../parser/parser';
import { SourceParser } from '../parser/source';
import { libraryParserLanguage } from '../parser/source/syntax';
import type { Context, ContiguousArrayElements, Node, NodeTypeToNode, Value } from '../types';
import { getSourceVariableDeclaration } from '../utils/ast/helpers';
import { getSourceVariableDeclaration, getSpecifierName } from '../utils/ast/helpers';
import { isDeclaration } from '../utils/ast/typeGuards';
import { oneLine } from '../utils/formatters';
import { RuntimeSourceError } from '../errors/base';
Expand Down Expand Up @@ -135,7 +135,7 @@ const transformers: ASTTransformers = {
declaration ? this.transform(declaration) : specifiers.map(this.transform),
]);
},
ExportSpecifier: node => vector_to_list(['name', node.exported.name]),
ExportSpecifier: node => vector_to_list(['name', getSpecifierName(node.exported)]),
ExpressionStatement({ expression }) {
return this.transform(expression);
},
Expand Down Expand Up @@ -180,7 +180,7 @@ const transformers: ASTTransformers = {
]);
},
ImportDefaultSpecifier: () => vector_to_list(['default']),
ImportSpecifier: node => vector_to_list(['name', node.imported.name]),
ImportSpecifier: node => vector_to_list(['name', getSpecifierName(node.imported)]),
Literal: ({ value }) => vector_to_list(['literal', value]),
LogicalExpression(node) {
return vector_to_list([
Expand Down
6 changes: 4 additions & 2 deletions src/stepper/nodes/Expression/BinaryExpression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BinaryExpression, BinaryOperator, Comment, SourceLocation } from 'estree';
import type { BinaryExpression, BinaryOperator, Comment, Expression, SourceLocation } from 'estree';
import type { StepperExpression, StepperPattern } from '..';
import { convert } from '../../generator';
import { StepperBaseNode } from '../../interface';
Expand Down Expand Up @@ -28,7 +28,9 @@ export class StepperBinaryExpression
static create(node: BinaryExpression) {
return new StepperBinaryExpression(
node.operator,
convert(node.left),
// `left` is `Expression | PrivateIdentifier`; private-in expressions are
// not valid in Source, so the operand is always an Expression.
convert(node.left as Expression),
convert(node.right),
node.leadingComments,
node.trailingComments,
Expand Down
4 changes: 3 additions & 1 deletion src/transpiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ function transformUnaryAndBinaryOperationsToFunctionCalls(
create.mutateToCallExpression(node, globalIds.binaryOp, [
create.literal(operator),
create.literal(chapter),
left,
// `left` is `Expression | PrivateIdentifier`; private-in expressions are
// not valid in Source, so the operand is always an Expression.
left as es.Expression,
right,
create.literal(line),
create.literal(column),
Expand Down
1 change: 1 addition & 0 deletions src/utils/ast/astCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const importDeclaration = (
type: 'ImportDeclaration',
source: literal(source),
specifiers,
attributes: [],
loc,
});

Expand Down
24 changes: 16 additions & 8 deletions src/utils/ast/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,24 @@ export function getSourceVariableDeclaration(decl: es.VariableDeclaration) {
};
}

/**
* Get the name of an import/export specifier endpoint. ES2022 allows these to be
* string literals (e.g. `export { x as "y" }`); Source only supports identifier
* names, so fall back to the literal's stringified value.
*/
export const getSpecifierName = (node: es.Identifier | es.Literal): string =>
node.type === 'Identifier' ? node.name : `${node.value}`;

export const getImportedName = (
spec: es.ImportSpecifier | es.ImportDefaultSpecifier | es.ExportSpecifier,
) => {
switch (spec.type) {
case 'ImportDefaultSpecifier':
return 'default';
case 'ImportSpecifier':
return spec.imported.name;
return getSpecifierName(spec.imported);
case 'ExportSpecifier':
return spec.local.name;
return getSpecifierName(spec.local);
}
};

Expand All @@ -119,18 +127,18 @@ export const specifierToString = (
) => {
switch (spec.type) {
case 'ImportSpecifier': {
if (spec.imported.name === spec.local.name) {
return spec.imported.name;
if (getSpecifierName(spec.imported) === spec.local.name) {
return getSpecifierName(spec.imported);
}
return `${spec.imported.name} as ${spec.local.name}`;
return `${getSpecifierName(spec.imported)} as ${spec.local.name}`;
}
case 'ImportDefaultSpecifier':
return `default as ${spec.local.name}`;
case 'ExportSpecifier': {
if (spec.local.name === spec.exported.name) {
return spec.local.name;
if (getSpecifierName(spec.local) === getSpecifierName(spec.exported)) {
return getSpecifierName(spec.local);
}
return `${spec.local.name} as ${spec.exported.name}`;
return `${getSpecifierName(spec.local)} as ${getSpecifierName(spec.exported)}`;
}
}
};
Expand Down
15 changes: 4 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -722,17 +722,10 @@ __metadata:
languageName: node
linkType: hard

"@types/estree@npm:^1.0.0, @types/estree@npm:^1.0.6":
version: 1.0.8
resolution: "@types/estree@npm:1.0.8"
checksum: 10c0/39d34d1afaa338ab9763f37ad6066e3f349444f9052b9676a7cc0252ef9485a41c6d81c9c4e0d26e9077993354edf25efc853f3224dd4b447175ef62bdcc86a5
languageName: node
linkType: hard

"@types/estree@npm:^1.0.5":
version: 1.0.5
resolution: "@types/estree@npm:1.0.5"
checksum: 10c0/b3b0e334288ddb407c7b3357ca67dbee75ee22db242ca7c56fe27db4e1a31989cb8af48a84dd401deb787fe10cc6b2ab1ee82dc4783be87ededbe3d53c79c70d
"@types/estree@npm:^1.0.0, @types/estree@npm:^1.0.5, @types/estree@npm:^1.0.6":
version: 1.0.9
resolution: "@types/estree@npm:1.0.9"
checksum: 10c0/3ad3286ca2988cd550dafb8f2ad599c8474868e954fa601a36655bdfefd8039f7c714b8c1c7f2ae219ffbd58bd4660e66fa7479a0120fc02d4777057d4865387
languageName: node
linkType: hard

Expand Down
Loading