-
-
Notifications
You must be signed in to change notification settings - Fork 723
feat(linter/plugins): add SourceCode API
#14281
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
Merged
graphite-app
merged 1 commit into
main
from
10-01-feat_linter_plugins_add_sourcecode_api
Oct 1, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import type { Node } from './types.ts'; | ||
|
|
||
| const { max } = Math; | ||
|
|
||
| /** | ||
| * `SourceCode` class. | ||
| * | ||
| * Each rule has its own `SourceCode` object. It is stored in `Context` for that rule. | ||
| * | ||
| * A new `SourceCode` instance is NOT generated for each file. | ||
| * The `SourceCode` instance for the rule is updated for each file. | ||
| */ | ||
| export class SourceCode { | ||
| // Source text. | ||
| // Initially `null`, but set to source text string before linting each file. | ||
| text: string = null as unknown as string; | ||
overlookmotel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| getText( | ||
| node?: Node | null | undefined, | ||
| beforeCount?: number | null | undefined, | ||
| afterCount?: number | null | undefined, | ||
| ): string { | ||
| // ESLint treats all falsy values for `node` as undefined | ||
| if (!node) return this.text; | ||
|
|
||
| // ESLint ignores falsy values for `beforeCount` and `afterCount` | ||
| let { start, end } = node; | ||
| if (beforeCount) start = max(start - beforeCount, 0); | ||
| if (afterCount) end += afterCount; | ||
| return this.text.slice(start, end); | ||
| } | ||
|
|
||
| // TODO: Add more methods | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "jsPlugins": ["./plugin.ts"], | ||
| "categories": { "correctness": "off" }, | ||
| "rules": { | ||
| "source-code-plugin/create": "error", | ||
| "source-code-plugin/create-once": "error" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let foo, bar; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let qux; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| # Exit code | ||
| 1 | ||
|
|
||
| # stdout | ||
| ``` | ||
| x source-code-plugin(create): create: | ||
| | sourceCode.text: "let foo, bar;\n" | ||
| | sourceCode.getText(): "let foo, bar;\n" | ||
| ,-[files/1.js:1:1] | ||
| 1 | let foo, bar; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): after: | ||
| | source: "let foo, bar;\n" | ||
| ,-[files/1.js:1:1] | ||
| 1 | let foo, bar; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): before: | ||
| | sourceCode.text: "let foo, bar;\n" | ||
| | sourceCode.getText(): "let foo, bar;\n" | ||
| ,-[files/1.js:1:1] | ||
| 1 | let foo, bar; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): var decl: | ||
| | source: "let foo, bar;" | ||
| ,-[files/1.js:1:1] | ||
| 1 | let foo, bar; | ||
| : ^^^^^^^^^^^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): var decl: | ||
| | source: "let foo, bar;" | ||
| ,-[files/1.js:1:1] | ||
| 1 | let foo, bar; | ||
| : ^^^^^^^^^^^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): ident "foo": | ||
| | source: "foo" | ||
| | source with before: "t foo" | ||
| | source with after: "foo," | ||
| | source with both: "t foo," | ||
| ,-[files/1.js:1:5] | ||
| 1 | let foo, bar; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): ident "foo": | ||
| | source: "foo" | ||
| | source with before: "t foo" | ||
| | source with after: "foo," | ||
| | source with both: "t foo," | ||
| ,-[files/1.js:1:5] | ||
| 1 | let foo, bar; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): ident "bar": | ||
| | source: "bar" | ||
| | source with before: ", bar" | ||
| | source with after: "bar;" | ||
| | source with both: ", bar;" | ||
| ,-[files/1.js:1:10] | ||
| 1 | let foo, bar; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): ident "bar": | ||
| | source: "bar" | ||
| | source with before: ", bar" | ||
| | source with after: "bar;" | ||
| | source with both: ", bar;" | ||
| ,-[files/1.js:1:10] | ||
| 1 | let foo, bar; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): create: | ||
| | sourceCode.text: "let qux;\n" | ||
| | sourceCode.getText(): "let qux;\n" | ||
| ,-[files/2.js:1:1] | ||
| 1 | let qux; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): after: | ||
| | source: "let qux;\n" | ||
| ,-[files/2.js:1:1] | ||
| 1 | let qux; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): before: | ||
| | sourceCode.text: "let qux;\n" | ||
| | sourceCode.getText(): "let qux;\n" | ||
| ,-[files/2.js:1:1] | ||
| 1 | let qux; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): var decl: | ||
| | source: "let qux;" | ||
| ,-[files/2.js:1:1] | ||
| 1 | let qux; | ||
| : ^^^^^^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): var decl: | ||
| | source: "let qux;" | ||
| ,-[files/2.js:1:1] | ||
| 1 | let qux; | ||
| : ^^^^^^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create): ident "qux": | ||
| | source: "qux" | ||
| | source with before: "t qux" | ||
| | source with after: "qux;" | ||
| | source with both: "t qux;" | ||
| ,-[files/2.js:1:5] | ||
| 1 | let qux; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| x source-code-plugin(create-once): ident "qux": | ||
| | source: "qux" | ||
| | source with before: "t qux" | ||
| | source with after: "qux;" | ||
| | source with both: "t qux;" | ||
| ,-[files/2.js:1:5] | ||
| 1 | let qux; | ||
| : ^^^ | ||
| `---- | ||
|
|
||
| Found 0 warnings and 16 errors. | ||
| Finished in Xms on 2 files using X threads. | ||
| ``` | ||
|
|
||
| # stderr | ||
| ``` | ||
| WARNING: JS plugins are experimental and not subject to semver. | ||
| Breaking changes are possible while JS plugins support is under development. | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.