Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Transform @apply(--foo) to @apply --foo
Browse files Browse the repository at this point in the history
  • Loading branch information
rictic committed Sep 26, 2016
1 parent 10496ce commit 06bdd43
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/all-passes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
// This file imports all passes, causing them to be registered.

import './html/move-style-into-template';
import './css/at-apply-not-function';
import './css/var-statement-prop-fallback';
53 changes: 53 additions & 0 deletions src/css/at-apply-not-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/

import {ParsedCssDocument} from 'polymer-analyzer/lib/css/css-document';
import * as shadyCss from 'shady-css-parser';
import * as stripIndent from 'strip-indent';

import {registry} from '../registry';

import {CssUpgradePass} from './css-pass';


class FixAtApplyWithInvalidParens extends CssUpgradePass {
code = 'at-apply-not-function';
description = stripIndent(`
Transforms the invalid expression:
@apply(--foo)
Into the valid:
@apply --foo;
`);

constructor() { super(); }

upgrade(document: ParsedCssDocument) {
document.visit([{
visit(node: shadyCss.Node, _path: shadyCss.Node[]) {
if (node.type === 'atRule' && node.name === 'apply') {
const badApplyMatcher = /^\s*\((.*)\)\s*$/;
const match = node.parameters.match(badApplyMatcher);
if (match) {
node.parameters = `${match[1]}`;
}
}
}
}]);
}
}

registry.register(new FixAtApplyWithInvalidParens());
3 changes: 3 additions & 0 deletions src/test/fixtures/at-apply-not-function/after/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
@apply --erratic-jumping;
}
3 changes: 3 additions & 0 deletions src/test/fixtures/at-apply-not-function/before/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
@apply(--erratic-jumping)
}

0 comments on commit 06bdd43

Please sign in to comment.