Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for import attributes #174

Merged
merged 2 commits into from
Jun 25, 2024
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
25 changes: 25 additions & 0 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ import z from "z";
`,
);
});

test('handles import attributes and assertions, converting to attributes when necessary', async () => {
const code = `import z from 'z';
import g from 'g' with { type: 'json' };
import c from 'c' assert { type: 'json' };
`;
const importNodes = getImportNodes(code, {
plugins: [['importAttributes', { deprecatedAssertSyntax: true }]],
});
const sortedNodes = getSortedNodes(importNodes, {
importOrder: defaultImportOrder,
importOrderCombineTypeAndValueImports: true,
});
const formatted = getCodeFromAst({
nodesToOutput: sortedNodes,
originalCode: code,
directives: [],
});
expect(await format(formatted, { parser: 'babel' })).toEqual(
`import c from "c" with { type: "json" };
import g from "g" with { type: "json" };
import z from "z";
`,
);
});
2 changes: 1 addition & 1 deletion src/utils/get-code-from-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const getCodeFromAst = ({
},
});

const { code } = generate(newAST);
const { code } = generate(newAST, { importAttributesKeyword: 'with' });

const replacedCode = code.replace(injectNewlinesRegex, newLineCharacters);

Expand Down
13 changes: 13 additions & 0 deletions tests/Babel/__snapshots__/ppsi.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`imports-with-assertions.ts - babel-verify > imports-with-assertions.ts 1`] = `
import z from 'z-assert' assert { type: 'json' };
import x from 'x-with' with { type: 'json' };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import x from "x-with" with { type: "json" };
import z from "z-assert" with { type: "json" };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported

`;

exports[`imports-with-comments.js - babel-verify > imports-with-comments.js 1`] = `
// I am top level comment in this file.

Expand Down
4 changes: 4 additions & 0 deletions tests/Babel/imports-with-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import z from 'z-assert' assert { type: 'json' };
import x from 'x-with' with { type: 'json' };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported
1 change: 1 addition & 0 deletions tests/Babel/ppsi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import {run_spec} from '../../test-setup/run_spec';

run_spec(__dirname, ["babel"], {
importOrder: [ "<THIRD_PARTY_MODULES>", '^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'],
importOrderParserPlugins : ['[\"importAttributes\", {\"deprecatedAssertSyntax\": true}]'],
});
13 changes: 13 additions & 0 deletions tests/Typescript/__snapshots__/ppsi.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ export class AppComponent extends BaseComponent {

`;

exports[`imports-with-assertions.ts - typescript-verify > imports-with-assertions.ts 1`] = `
import z from 'z-assert' assert { type: 'json' };
import x from 'x-with' with { type: 'json' };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import x from "x-with" with { type: "json" };
import z from "z-assert" with { type: "json" };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported

`;

exports[`imports-with-comments.ts - typescript-verify > imports-with-comments.ts 1`] = `
import z from 'z';
import { isEmpty } from "lodash-es";
Expand Down
4 changes: 4 additions & 0 deletions tests/Typescript/imports-with-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import z from 'z-assert' assert { type: 'json' };
import x from 'x-with' with { type: 'json' };

// import y from 'y-legacy' with type: "json" // <-- this format is from a very old proposal, and is not supported
2 changes: 1 addition & 1 deletion tests/Typescript/ppsi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import {run_spec} from '../../test-setup/run_spec';

run_spec(__dirname, ["typescript"], {
importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'],
importOrderParserPlugins : ['typescript', 'jsx', 'decorators-legacy', 'classProperties'],
importOrderParserPlugins : ['typescript', 'jsx', 'decorators-legacy', 'classProperties', '[\"importAttributes\", {\"deprecatedAssertSyntax\": true}]'],
});
Loading