diff --git a/package.json b/package.json index 415f69f..9f02e67 100644 --- a/package.json +++ b/package.json @@ -33,20 +33,20 @@ }, "license": "Apache-2.0", "dependencies": { - "@babel/generator": "7.17.7", - "@babel/parser": "^7.20.5", - "@babel/traverse": "7.23.2", - "@babel/types": "7.17.0", + "@babel/generator": "7.24.4", + "@babel/parser": "^7.24.4", + "@babel/traverse": "7.24.1", + "@babel/types": "7.24.0", "javascript-natural-sort": "0.7.1", "lodash": "^4.17.21" }, "devDependencies": { - "@babel/core": "^7.20.7", + "@babel/core": "^7.24.4", "@types/chai": "4.2.15", "@types/jest": "26.0.20", "@types/lodash": "4.14.168", "@types/node": "20.8.6", - "@vue/compiler-sfc": "^3.2.41", + "@vue/compiler-sfc": "3.2.41", "jest": "26.6.3", "prettier": "2.8", "ts-jest": "26.5.3", diff --git a/src/utils/__tests__/get-code-from-ast.spec.ts b/src/utils/__tests__/get-code-from-ast.spec.ts index da58c04..e264e16 100644 --- a/src/utils/__tests__/get-code-from-ast.spec.ts +++ b/src/utils/__tests__/get-code-from-ast.spec.ts @@ -30,6 +30,7 @@ import a from 'a'; expect(format(formatted, { parser: 'babel' })).toEqual( `// first comment // second comment + import a from "a"; import c from "c"; import g from "g"; diff --git a/src/utils/extract-ast-nodes.ts b/src/utils/extract-ast-nodes.ts index 55ba311..5f085c5 100644 --- a/src/utils/extract-ast-nodes.ts +++ b/src/utils/extract-ast-nodes.ts @@ -29,7 +29,7 @@ export function extractASTNodes(ast: ParseResult) { ImportDeclaration(path: NodePath) { const tsModuleParent = path.findParent((p) => - isTSModuleDeclaration(p), + isTSModuleDeclaration(p.node), ); if (!tsModuleParent) { importNodes.push(path.node); diff --git a/src/utils/get-code-from-ast.ts b/src/utils/get-code-from-ast.ts index 2856266..ce4f8dd 100644 --- a/src/utils/get-code-from-ast.ts +++ b/src/utils/get-code-from-ast.ts @@ -36,19 +36,22 @@ export const getCodeFromAst = ( directives, sourceType: 'module', interpreter: interpreter, - sourceFile: '', leadingComments: [], innerComments: [], trailingComments: [], start: 0, end: 0, loc: { - start: { line: 0, column: 0 }, - end: { line: 0, column: 0 }, + filename: '', + identifierName: '', + start: { line: 0, column: 0, index: 0 }, + end: { line: 0, column: 0, index: 0 }, }, }); - const { code } = generate(newAST); + const { code } = generate(newAST, { + importAttributesKeyword: 'with', + }); return ( code.replace( diff --git a/src/utils/get-import-nodes.ts b/src/utils/get-import-nodes.ts index 5530a35..8848451 100644 --- a/src/utils/get-import-nodes.ts +++ b/src/utils/get-import-nodes.ts @@ -12,7 +12,7 @@ export const getImportNodes = (code: string, options?: ParserOptions) => { traverse(ast, { ImportDeclaration(path: NodePath) { const tsModuleParent = path.findParent((p) => - isTSModuleDeclaration(p), + isTSModuleDeclaration(p.node), ); if (!tsModuleParent) { importNodes.push(path.node); diff --git a/src/utils/is-sort-imports-ignored.ts b/src/utils/is-sort-imports-ignored.ts index 0606e86..62ec0cc 100644 --- a/src/utils/is-sort-imports-ignored.ts +++ b/src/utils/is-sort-imports-ignored.ts @@ -6,6 +6,6 @@ import { getAllCommentsFromNodes } from './get-all-comments-from-nodes'; export const isSortImportsIgnored = (nodes: Statement[]) => getAllCommentsFromNodes(nodes).some( (comment) => - comment.loc.start.line === 1 && + comment.loc?.start.line === 1 && comment.value.includes(sortImportsIgnoredComment), ); diff --git a/tests/Babel/__snapshots__/ppsi.spec.js.snap b/tests/Babel/__snapshots__/ppsi.spec.js.snap index 84c6285..fddaf99 100644 --- a/tests/Babel/__snapshots__/ppsi.spec.js.snap +++ b/tests/Babel/__snapshots__/ppsi.spec.js.snap @@ -20,6 +20,7 @@ function add(a,b) { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. + import thirdParty from "third-party"; import z from "z"; diff --git a/tests/Flow/__snapshots__/ppsi.spec.js.snap b/tests/Flow/__snapshots__/ppsi.spec.js.snap index 0703b7d..b5581e4 100644 --- a/tests/Flow/__snapshots__/ppsi.spec.js.snap +++ b/tests/Flow/__snapshots__/ppsi.spec.js.snap @@ -33,7 +33,9 @@ export function givesAFoo3Obj(): AliasFoo3 { /** * @flow */ + // I am top level comment in this file. + import thirdParty from "third-party"; import abc from "@core/abc"; diff --git a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap index 7956502..4dd3c46 100644 --- a/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparated/__snapshots__/ppsi.spec.js.snap @@ -96,6 +96,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import React from "react"; import "./commands"; @@ -131,6 +132,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import thirdParty from "third-party"; import z from "z"; import abc from "@core/abc"; @@ -245,6 +247,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //@ts-ignore // I am file top level comments + import a from "a"; import c from "c"; // I am stick to third party comment @@ -323,6 +326,7 @@ import fourLevelRelativePath from "../../../../fourLevelRelativePath"; import something from "@server/something"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment + import abc from "@core/abc"; import otherthing from "@core/otherthing"; import something from "@server/something"; @@ -378,6 +382,7 @@ import './commands'; // You can read more here: // https://on.cypress.io/configuration // *********************************************************** + // Import commands.js using ES2015 syntax: import "./commands"; diff --git a/tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap b/tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap index ecc8d51..e66cc50 100644 --- a/tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsNotSeparatedRest/__snapshots__/ppsi.spec.js.snap @@ -96,6 +96,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import React from "react"; import "./commands"; @@ -131,6 +132,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import abc from "@core/abc"; import otherthing from "@core/otherthing"; import qwerty from "@server/qwerty"; @@ -188,6 +190,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //@ts-ignore // I am file top level comments + import otherthing from "@core/otherthing"; import something from "@server/something"; import component from "@ui/hello"; @@ -266,6 +269,7 @@ import fourLevelRelativePath from "../../../../fourLevelRelativePath"; import something from "@server/something"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment + import abc from "@core/abc"; import otherthing from "@core/otherthing"; import something from "@server/something"; @@ -321,6 +325,7 @@ import './commands'; // You can read more here: // https://on.cypress.io/configuration // *********************************************************** + // Import commands.js using ES2015 syntax: import "./commands"; diff --git a/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap index 4dc5569..4819f7e 100644 --- a/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsSeparated/__snapshots__/ppsi.spec.js.snap @@ -100,6 +100,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import React from "react"; import "./commands"; @@ -136,6 +137,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import thirdParty from "third-party"; import z from "z"; @@ -196,6 +198,7 @@ const workletAdd = (a:number,b:number) => { "use client"; // comment after directives + import abc from "@core/abc"; import otherthing from "@core/otherthing"; @@ -262,6 +265,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //@ts-ignore // I am file top level comments + import a from "a"; import c from "c"; // I am stick to third party comment @@ -347,6 +351,7 @@ import fourLevelRelativePath from "../../../../fourLevelRelativePath"; import something from "@server/something"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment + import abc from "@core/abc"; import otherthing from "@core/otherthing"; @@ -405,6 +410,7 @@ import './commands'; // You can read more here: // https://on.cypress.io/configuration // *********************************************************** + // Import commands.js using ES2015 syntax: import "./commands"; diff --git a/tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap b/tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap index b9a6b68..efb75cd 100644 --- a/tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsSeparatedRest/__snapshots__/ppsi.spec.js.snap @@ -100,6 +100,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import React from "react"; import "./commands"; @@ -136,6 +137,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment in this file. // I am second line of top level comment in this file. + import abc from "@core/abc"; import otherthing from "@core/otherthing"; @@ -197,6 +199,7 @@ function add(a:number,b:number) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //@ts-ignore // I am file top level comments + import otherthing from "@core/otherthing"; import something from "@server/something"; @@ -282,6 +285,7 @@ import fourLevelRelativePath from "../../../../fourLevelRelativePath"; import something from "@server/something"; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // I am top level comment + import abc from "@core/abc"; import otherthing from "@core/otherthing"; @@ -340,6 +344,7 @@ import './commands'; // You can read more here: // https://on.cypress.io/configuration // *********************************************************** + // Import commands.js using ES2015 syntax: import "./commands"; diff --git a/tests/Vue/__snapshots__/ppsi.spec.js.snap b/tests/Vue/__snapshots__/ppsi.spec.js.snap index 065c444..25e027a 100644 --- a/tests/Vue/__snapshots__/ppsi.spec.js.snap +++ b/tests/Vue/__snapshots__/ppsi.spec.js.snap @@ -28,6 +28,7 @@ function add(a,b) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~