Skip to content

Commit

Permalink
Allow stimulus package name for controller definition (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoroth authored Jul 19, 2024
1 parent 4f1acd7 commit 5736f57
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/source_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class SourceFile {
const originalName = (original === "default") ? undefined : original
const localName = specifier.local.name
const source = ast.extractLiteral(node.source) as string
const isStimulusImport = (originalName === "Controller" && source === "@hotwired/stimulus")
const isStimulusImport = (originalName === "Controller" && ["@hotwired/stimulus", "stimulus"].includes(source))

let type: ImportDeclarationType = "default"

Expand Down
20 changes: 20 additions & 0 deletions test/source_file/class_declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ describe("SourceFile", () => {
expect(something.superClass).toBeInstanceOf(StimulusControllerClassDeclaration)
})

test("named class with superclass from Stimulus Controller import and old package name", async () => {
const code = dedent`
import { Controller } from "stimulus"
class Something extends Controller {}
`

const sourceFile = new SourceFile(project, "abc.js", code)
project.projectFiles.push(sourceFile)

await project.analyze()

const something = sourceFile.findClass("Something")

expect(something).toBeDefined()
expect(something.isStimulusDescendant).toBeTruthy()
expect(something.superClass).toBeDefined()
expect(something.superClass.isStimulusDescendant).toBeTruthy()
expect(something.superClass).toBeInstanceOf(StimulusControllerClassDeclaration)
})

test("anonymous class assigned to variable from Stimulus Controller import", async () => {
const code = dedent`
import { Controller } from "@hotwired/stimulus"
Expand Down
40 changes: 40 additions & 0 deletions test/source_file/import_declarations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ describe("SourceFile", async () => {
expect(controller.type).toEqual("named")
})

test("stimulus controller import with old package name", async () => {
const code = dedent`
import { Controller } from "stimulus"
`

const sourceFile = new SourceFile(project, "abc.js", code)
project.projectFiles.push(sourceFile)

await project.analyze()

const controller = sourceFile.findImport("Controller")

expect(controller.isRenamedImport).toEqual(false)
expect(controller.isStimulusImport).toEqual(true)
expect(controller.localName).toEqual("Controller")
expect(controller.originalName).toEqual("Controller")
expect(controller.source).toEqual("stimulus")
expect(controller.type).toEqual("named")
})

test("stimulus controller import with alias", async () => {
const code = dedent`
import { Controller as StimulusController } from "@hotwired/stimulus"
Expand All @@ -221,5 +241,25 @@ describe("SourceFile", async () => {
expect(controller.source).toEqual("@hotwired/stimulus")
expect(controller.type).toEqual("named")
})

test("stimulus controller import with alias and old package name", async () => {
const code = dedent`
import { Controller as StimulusController } from "stimulus"
`

const sourceFile = new SourceFile(project, "abc.js", code)
project.projectFiles.push(sourceFile)

await project.analyze()

const controller = sourceFile.findImport("StimulusController")

expect(controller.isRenamedImport).toEqual(true)
expect(controller.isStimulusImport).toEqual(true)
expect(controller.localName).toEqual("StimulusController")
expect(controller.originalName).toEqual("Controller")
expect(controller.source).toEqual("stimulus")
expect(controller.type).toEqual("named")
})
})
})

0 comments on commit 5736f57

Please sign in to comment.