Skip to content
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

[Theme Checks] Ensuring importmap is not being used on script tags in theme app extensions #682

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions .changeset/yellow-insects-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-common': minor
---

This theme check will check for and suggest removing script tags with type importmap on theme app extensions
2 changes: 2 additions & 0 deletions packages/theme-check-common/src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { ValidSchemaName } from './valid-schema-name';
import { ValidStaticBlockType } from './valid-static-block-type';
import { VariableName } from './variable-name';
import { MissingSchema } from './missing-schema';
import { NoImportmap } from './no-importmap';

export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
AppBlockValidTags,
Expand Down Expand Up @@ -92,6 +93,7 @@ export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
ValidStaticBlockType,
VariableName,
ValidSchemaName,
NoImportmap,
];

/**
Expand Down
31 changes: 31 additions & 0 deletions packages/theme-check-common/src/checks/no-importmap/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest';
import { NoImportmap } from '.';
import { check as reportOffenses } from '../../test';

describe('Module: NoImportmap', () => {
it('should report offense when using <script type="importmap">', async () => {
const file = `<script type="importmap">
{
"imports": {
"a.js": "https://foo.bar/baz.js"
}
}
</script>`;
const startIndex = file.indexOf('<script');
const endIndex = file.indexOf('</script>') + '</script>'.length;

const offenses = await reportOffenses({ 'code.liquid': file }, [NoImportmap]);

expect(offenses).to.have.length(1);
const { message, start, end } = offenses[0];

expect(message).toEqual(
'Until browsers permit multiple importmap entries, only themes can have an importmap',
);
expect(start.index).toEqual(startIndex);
expect(end.index).toEqual(endIndex);

expect(offenses[0].suggest).to.have.length(1);
expect(offenses[0]!.suggest![0].message).to.equal("Remove the 'importmap' script tag");
});
});
54 changes: 54 additions & 0 deletions packages/theme-check-common/src/checks/no-importmap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ConfigTarget, LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';

import { hasAttributeValueOf, isAttr, isValuedHtmlAttribute } from '../utils';

export const NoImportmap: LiquidCheckDefinition = {
meta: {
code: 'NoImportmap',
name: 'Import map in theme app extensions',
docs: {
description:
'Report offenses associated with import maps on script tags in theme app extensions',
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/no-importmap',
},
type: SourceCodeType.LiquidHtml,
severity: Severity.ERROR,
schema: {},
targets: [ConfigTarget.ThemeAppExtension],
},

create(context) {
return {
async HtmlRawNode(node) {
if (node.name !== 'script') {
return;
}

const typeImportMap = node.attributes
.filter(isValuedHtmlAttribute)
.some((attr) => isAttr(attr, 'type') && hasAttributeValueOf(attr, 'importmap'));

const typeModule = node.attributes
.filter(isValuedHtmlAttribute)
.some((attr) => isAttr(attr, 'type') && hasAttributeValueOf(attr, 'importmap'));

if (!typeImportMap || !typeModule) {
return;
}

context.report({
message:
'Until browsers permit multiple importmap entries, only themes can have an importmap',
startIndex: node.position.start,
endIndex: node.position.end,
suggest: [
{
message: `Remove the 'importmap' script tag`,
fix: (corrector) => corrector.remove(node.position.start, node.position.end),
},
],
});
},
};
},
};
3 changes: 3 additions & 0 deletions packages/theme-check-node/configs/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ MissingTemplate:
enabled: true
severity: 0
ignoreMissing: []
NoImportmap:
enabled: false
severity: 0
PaginationSize:
enabled: true
severity: 1
Expand Down
3 changes: 3 additions & 0 deletions packages/theme-check-node/configs/theme-app-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ AssetSizeAppBlockJavaScript:
MissingSchema:
enabled: true
severity: 0
NoImportmap:
enabled: true
severity: 0
RequiredLayoutThemeObject:
enabled: false
severity: 0
Loading