-
Notifications
You must be signed in to change notification settings - Fork 39
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
Use broader template-lint range #418
Changes from all commits
035923e
514ae2f
1859807
2dd183c
827faef
10a09db
e2804b1
5e964ca
372197b
95e33c8
8b30ec5
e44d324
45158a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ import Server from './server'; | |
import { Project } from './project'; | ||
import { getRequireSupport } from './utils/layout-helpers'; | ||
import { getFileRanges, RangeWalker } from './utils/glimmer-script'; | ||
import * as semver from 'semver'; | ||
import { type SemVer } from 'semver'; | ||
|
||
type FindUp = (name: string, opts: { cwd: string; type: string }) => Promise<string | undefined>; | ||
type LinterVerifyArgs = { source: string; moduleId: string; filePath: string }; | ||
|
@@ -88,7 +90,7 @@ export default class TemplateLinter { | |
return this.server.projectRoots.projectForUri(textDocument.uri); | ||
} | ||
|
||
private sourcesForDocument(textDocument: TextDocument, templateLintVersion: string): string[] { | ||
private sourcesForDocument(textDocument: TextDocument, templateLintVersion: SemVer | null): string[] { | ||
const ext = getExtension(textDocument); | ||
|
||
if (ext !== null && !extensionsToLint.includes(ext)) { | ||
|
@@ -98,8 +100,11 @@ export default class TemplateLinter { | |
const documentContent = textDocument.getText(); | ||
|
||
// we assume that ember-template-lint v5 could handle js/ts/gts/gjs files | ||
if (!templateLintVersion) { | ||
return [documentContent]; | ||
} | ||
|
||
if (templateLintVersion === '5') { | ||
if (semver.gte(templateLintVersion, '5.0.0')) { | ||
return [documentContent]; | ||
} | ||
|
||
|
@@ -139,28 +144,46 @@ export default class TemplateLinter { | |
}); | ||
} | ||
} | ||
getSourcesForDocument(textDocument: TextDocument, project: Project): string[] { | ||
const linterMeta = project.dependencyMap.get('ember-template-lint'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likely we need to add integration test for it, or test locally :) |
||
|
||
if (!linterMeta) { | ||
return []; | ||
} | ||
|
||
let sources = []; | ||
|
||
try { | ||
/** | ||
* Semver parsing can throw errors, if the version is invalid, | ||
* we want behave as if there was no version specified. | ||
* | ||
* (same as when errors are thrown from sourcesForDocument) | ||
*/ | ||
const version = linterMeta?.package.version; | ||
const linterVersion = version ? semver.parse(version) : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated logic on how we resolve template-lint version, now it should be taken from deps, need to test it... |
||
|
||
sources = this.sourcesForDocument(textDocument, linterVersion); | ||
} catch (e) { | ||
return []; | ||
} | ||
|
||
return sources; | ||
} | ||
async lint(textDocument: TextDocument): Promise<Diagnostic[] | undefined> { | ||
if (this._isEnabled === false) { | ||
return; | ||
return []; | ||
} | ||
|
||
const cwd = process.cwd(); | ||
|
||
const project = this.getProjectForDocument(textDocument); | ||
|
||
if (!project) { | ||
return; | ||
} | ||
|
||
const linterMeta = project.dependenciesMeta.find((dep) => dep.name === 'ember-template-lint'); | ||
const linterVersion = linterMeta?.version.split('.')[0] || 'unknown'; | ||
|
||
let sources = []; | ||
|
||
try { | ||
sources = this.sourcesForDocument(textDocument, linterVersion); | ||
} catch (e) { | ||
return; | ||
} | ||
const sources = this.getSourcesForDocument(textDocument, project); | ||
|
||
if (!sources.length) { | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import TemplateLinter from '../src/template-linter'; | ||
import { type Project, type Server } from '../src'; | ||
import { type TextDocument } from 'vscode-languageserver-textdocument'; | ||
|
||
function getLinterInstance(depName?: string, depVersion?: string): [TemplateLinter, Project] { | ||
const linter = new TemplateLinter({ | ||
projectRoots: { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
projectForUri(_url: string) { | ||
return { | ||
dependencyMap: new Map(depName ? [[depName, { package: { name: depName, version: depVersion } }]] : []), | ||
} as Project; | ||
}, | ||
}, | ||
options: { | ||
type: 'node', | ||
}, | ||
} as Server); | ||
|
||
return [linter, linter['server'].projectRoots.projectForUri('') as Project]; | ||
} | ||
|
||
describe('template-linter', function () { | ||
describe('sourcesForDocument', function () { | ||
it('supports empty template-lint version', function () { | ||
const [linter, project] = getLinterInstance(); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([]); | ||
}); | ||
it('supports incorrect template-lint version [foo-bar]', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', 'foo-bar'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]); | ||
}); | ||
it('supports incorrect template-lint version [*]', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '*'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]); | ||
}); | ||
it('process gjs for template-lint v2 with', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '2.0.0'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']); | ||
}); | ||
it('process gjs for template-lint v3 with', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '3.3.1'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']); | ||
}); | ||
it('process gjs for template-lint v4 with', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '4.3.1'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']); | ||
}); | ||
it('skip gjs processing for template-lint v5', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '5.0.0'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]); | ||
}); | ||
it('skip gjs processing for template-lint v6', function () { | ||
const [linter, project] = getLinterInstance('ember-template-lint', '6.0.0'); | ||
|
||
const doc: TextDocument = { | ||
uri: 'test.gjs', | ||
getText() { | ||
return 'let a = 12;<template>1</template>'; | ||
}, | ||
} as TextDocument; | ||
|
||
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work sadly.
Something like the following is technically correct: