Skip to content
Open
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
80 changes: 80 additions & 0 deletions language/syntaxes/lockfile.tmGrammar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"scopeName": "source.github-actions-lockfile.pins",
"injectionSelector": "L:source.github-actions-lockfile -comment",
"patterns": [
{
"include": "#commit"
},
{
"include": "#pin"
}
],
"repository": {
"commit": {
"match": "(?<![\\w-])(sha1|sha256)(-)([0-9a-fA-F]+)(?![\\w-])",
"captures": {
"1": {
"name": "storage.type.hash-algorithm.github-actions-lockfile"
},
"2": {
"name": "punctuation.separator.hash.github-actions-lockfile"
},
"3": {
"name": "constant.numeric.hash.github-actions-lockfile"
}
}
},
"pin": {
"patterns": [
{
"include": "#pin-quoted"
},
{
"include": "#pin-plain"
}
]
},
"pin-quoted": {
"match": "(?<=['\"])([^\\s/@:#'\"\\[\\],]+)(/)([^\\s/@:#'\"\\[\\],]+)(@)([^\\s:'\"]+)",
"name": "meta.pin.github-actions-lockfile",
"captures": {
"1": {
"name": "entity.name.function.action.owner.github-actions-lockfile"
},
"2": {
"name": "punctuation.separator.slash.github-actions-lockfile"
},
"3": {
"name": "entity.name.function.action.repo.github-actions-lockfile"
},
"4": {
"name": "keyword.operator.ref.github-actions-lockfile"
},
"5": {
"name": "variable.other.constant.ref.github-actions-lockfile"
}
}
},
"pin-plain": {
"match": "(?<![^\\s\\[,])([^\\s/@:#'\"\\[\\],]+)(/)([^\\s/@:#'\"\\[\\],]+)(@)([^\\s:'\"\\[\\],]+)",
"name": "meta.pin.github-actions-lockfile",
"captures": {
"1": {
"name": "entity.name.function.action.owner.github-actions-lockfile"
},
"2": {
"name": "punctuation.separator.slash.github-actions-lockfile"
},
"3": {
"name": "entity.name.function.action.repo.github-actions-lockfile"
},
"4": {
"name": "keyword.operator.ref.github-actions-lockfile"
},
"5": {
"name": "variable.other.constant.ref.github-actions-lockfile"
}
}
}
}
}
9 changes: 9 additions & 0 deletions language/syntaxes/lockfile.tmLanguage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "GitHub Actions Lockfile",
"scopeName": "source.github-actions-lockfile",
"patterns": [
{
"include": "source.github-actions-workflow"
}
]
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
"dark": "resources/icons/dark/lang.svg",
"light": "resources/icons/light/lang.svg"
}
},
{
"id": "github-actions-lockfile",
"aliases": [
"GitHub Actions Lockfile"
],
"filenamePatterns": [
"**/.github/workflows/actions.lock"
],
"configuration": "./language/language-configuration.json",
"icon": {
"dark": "resources/icons/dark/lang.svg",
"light": "resources/icons/light/lang.svg"
}
}
],
"grammars": [
Expand All @@ -63,6 +77,18 @@
"injectTo": [
"source.github-actions-workflow"
]
},
{
"language": "github-actions-lockfile",
"scopeName": "source.github-actions-lockfile",
"path": "./language/syntaxes/lockfile.tmLanguage.json"
},
{
"scopeName": "source.github-actions-lockfile.pins",
"path": "./language/syntaxes/lockfile.tmGrammar.json",
"injectTo": [
"source.github-actions-lockfile"
]
}
],
"configuration": {
Expand Down
46 changes: 46 additions & 0 deletions src/lockfileGrammar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from "fs";
import path from "path";

type Rule = {match?: string; patterns?: {include: string}[]};

const grammar = JSON.parse(
fs.readFileSync(path.join(process.cwd(), "language/syntaxes/lockfile.tmGrammar.json"), "utf8")
) as {repository: Record<string, Rule>};

const pinRules = (grammar.repository.pin.patterns ?? []).map(
p => new RegExp(grammar.repository[p.include.slice(1)].match ?? "", "g")
);

// Mirrors TextMate scanning: at each position the earliest match wins, ties go to the first rule.
function refs(line: string): string[] {
const found: string[] = [];
let pos = 0;
for (;;) {
let best: RegExpExecArray | null = null;
for (const re of pinRules) {
re.lastIndex = pos;
const m = re.exec(line);
if (m && (!best || m.index < best.index)) best = m;
}
if (!best) return found;
found.push(best[5]);
pos = best.index + best[0].length;
}
}

describe("lockfile pin grammar", () => {
it("captures the full ref in quoted entries, including # and ,", () => {
expect(refs(" - 'actions/checkout@release#1'")).toEqual(["release#1"]);
expect(refs(' - "actions/checkout@release,1"')).toEqual(["release,1"]);
expect(refs(" 'actions/checkout@feature/x#2,3':")).toEqual(["feature/x#2,3"]);
});

it("keeps # in plain refs but stops at comments and flow separators", () => {
expect(refs(" - actions/checkout@release#1 # note")).toEqual(["release#1"]);
expect(refs(" - [actions/checkout@v1, actions/setup-node@v2]")).toEqual(["v1", "v2"]);
});

it("does not run past the closing quote", () => {
expect(refs(" 'actions/checkout@v4':")).toEqual(["v4"]);
});
});
Loading